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')
])
?>
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' : ''
How do I get a value and make it appear on a dropDownlist on yii2
For example I have 2 models which is hotel guest and service request.
I wanna get the hotelguest_id value from its model and store it on a dropdown on the service request.
Similar like this.
<?= $form->field($model, 'hotelguest_id')>dropDownList(yii\helpers\ArrayHelper::map(hotelguest_id::find(->all(),
'id', 'id')) ?>
I already figured it out
By using
use app\models\Hotelguest;
use yii\helpers\ArrayHelper;
<?= $form->field($model, 'hotelguest_id')->dropDownList(ArrayHelper::map(Hotelguest::find()->all(),
'id', 'id'), ['prompt' => 'Select Hotel Guest ID']); ?>
I have a _form:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'result')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'test1')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'test2')->textInput(['maxlength' => true]) ?>
<?php ActiveForm::end(); ?>
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
I need to set a formula: result = test1*test2
Using raw sql I can put it in controller:
public function actionIndex(){
Yii::$app->db->createCommand("UPDATE table1 SET result = test1*test2")->execute();
etc...
}
And it's ok. But I think it's not correct. So the best way to do this is:
public function actionTest($id)
{
$model = $this->findModel($id);
$model->result = $model->test1 * model->test2
}
after:
<?= Html::a('gotest', ['test', 'id' => $model->id], ['class' => 'btn btn-success']) ?>
But how can I get $model->id from form create to actionIndex ? Or maybe other option?
I think a clean way to do this in Yii2 should be using afterFind callback to fill your result field with your calculated value, like this:
In your Model:
public function afterFind()
{
$this->result = $this->test1 * $this->test2;
}
All logic you put in your afterFind will be executed just after Yii2 get the result from DB. It's nice when you need to calculate something before put in a form or a DataProvider, because everything will happens right in the model.
If I get this right, you won't even need the result field in your database. (in this way, just declare it in your model, creating an virtual field like this: public $result; and you'll be able to use it just like if it exists in table.
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();
}
}
If I have the following form field (code below) how can I set the field as required within my model so that like other fields the form cannot be submitted without it containing information.
<?= $form->field($model, 'seo[seo_title]')->textInput(['maxlength' => 60])->label('SEO Title') ?>
Add the value for te textinput in the options eg:
<?= $form->field($model, 'seo[seo_title]')->textInput(['maxlength' => 60], 'value'=> $yourValue )->label('SEO Title') ?>
or if you only need these fields required mark required in the model