I tried to change the label of a Submit button with no luck:
<?= $this->Form->button(__('Submit'), array('name' => 'Create')) ?>
How I can change it?
Just change the text from Submit to Create:
<?= $this->Form->button(__('Create')) ?>
It will generate a button that looks like below:
Simply do this :
$options = [];
<?= $this->Form->button(__('Create'), $options) ?>
In CakePHP 4.x, you can use
<?= $this->Form->submit("Login",['class'=>'any-css-class-if-needed']); ?>
For reference: Creating Submit Elements
Related
I have an activeform field.
<?= $form->field($model, 'featured_listings')->dropDownList([1=>'Yes',0=>'No']); ?>
I need this field hidden on create and shown on update.
Add if() statement which uses $model->isNewRecord:
if($model->isNewRecord) {
//do your stuff here
}
Or if you want to have just HTML hidden, add proper class by $model->isNewRecord ? 'hidden' : ''
i have this link
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $o->id]) ?>.
Instead of Edit ,i want to use edit html character (✎).
please help.
Use this code
✎
instead of Edit text
will produce ✎ icon
Just add "escape" parameter and set it to false.
You may wanna refer to this: CakePHP 3 creating links
<?php
echo Html->link(__('✎'),
['action' => 'edit', $o->id],
['escape' => false]
) ?>
I'm a begginer in Yii2 framework.
I work on a forum :
In the forum/posts method, there is a list of posts about the topic.
Each Post have a score which I want up and down in AJAX.
In my view posts.php, I open a Pjax block :
<?php Pjax::begin(); ?>
Votes : <?= $val['score'] ?>
<?= Html::a('+', ['/post/voteup','id'=>$val['id']]) ?>
<?= Html::a('-', ['/post/votedown','id'=>$val['id']]) ?>
<?php Pjax::end(); ?>
In my PostController :
public function actionVoteup($id){
//Update request
$postRepo=new PostRepository();
$postRepo->vote('plus', "id=$id");
$post=$postRepo->getAll("id=$id");
return $this->renderAjax('vote', ['id'=>$id, 'score'=>$post[0]['score']]);
}
You can see I return the Vote.php view in Ajax, same code Pjax.
<?php Pjax::begin(); ?>
Votes : <?= $score ?>
<?= Html::a('+', ['/post/voteup','id'=>$id]) ?>
<?= Html::a('-', ['/post/votedown','id'=>$id]) ?>
<?php Pjax::end(); ?>
The update request is OK but I have some problems/questions :
Ex : I want to up the 2nd post score, I click, OK, I click a second time, the part of view which is refresh is the 1st Post score (but in database, it's the good score updated).
I think the problem is about my part of view that I return in my actionVoteup().
Should I return forum/posts or post/vote ?
When I click in the link, my URL is : post/voteup ; how can I return in the original URL forum/posts ?
I don't really understand how works Pjax, and I didn't find good examples about its utilisation.
Thanks for your replies :)
You need to add 'enablePushState' => false in Pjax attribute.
Like as
<?php Pjax::begin(['enablePushState' => false]); ?>
For more info. Visit this Demo
I am creating a blog using Yii2. I have basic DB structure having tables:
Posts
Categories
Posts_Categories
I am using Yii2 ActiveForm to create post creation form. There are input fields for Title (text field), Content (text area), Categories (list box for multiple category selection).
I am not able to populate listBox with db values.
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'Title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'Content')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'CategoryId')->listBox(\yii\helpers\ArrayHelper::map(\backend\models\Category::find()->all(),'CategoryId','CategoryName',['multiple' => true])); ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
It is throwing following error:
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: backend\models\Category::1
There is error in listbox line. Secondly, after populating the data in listbox, how can I handle data insertion with respect to posts and multiple categories relationship.
You have syntax error in listBox. so, complete end parenthesis ) before multiple property.
Like as,
<?= $form->field($model, 'CategoryId')->listBox(\yii\helpers\ArrayHelper::map(\backend\models\Category::find()->all(),'CategoryId','CategoryName'),['multiple' => true]); ?>
For the second part of your question on data insertion. You may need to traverse the array of the selected (posted) items in the list box. Here is an example that may be included in the actionCreate() or actionUpdate() functions of your controller file (I am assuming PostController.php):
$selectedList = $_POST['model_name']['CategoryId'];
if(isset($selectedList)) {
foreach($selectedList as $value){
$pcmodel = new Post_Categories(); //assumption on model name
//do neccessary check here
$pcmodel->post_id = $postmodel->id;
//assumption that value is being stored
$pcmodel->category_id = $value;
$pcmodel->save();
}
}
I want to do some AJAX validation on my ActiveForm, but I want to make AJAX validation false for a single field.
How should I do that in Yii2?
My code for single field:
<?= $form->field($modelChangePassword, 'CurrentPassword',
[
'options'=>['enableAjaxValidation' => false])->textInput(['placeholder'=>Yii::t('frontend/changePassword','CurrentPasswordPlaceholderText')
])
?>
Try this.
<?= $form->field($modelChangePassword, 'CurrentPassword',
['enableAjaxValidation' => false])->textInput(['placeholder'=>Yii::t('frontend/changePassword','CurrentPasswordPlaceholderText')
])
?>