Answer the question
In order to leave comments, you need to log in
Why doesn't self-validation pass?
I tried to organize my own validation here is the model
class SearchForm extends Model
{
public $ingredients;
public function rules(){
return[
['ingredients', function ($attribute, $params) {
if (count($this->attribute)<2) {
$this->addError($attribute, 'Выберите больше ингредиентов.');
}
if (count($this->attribute)>5) {
$this->addError($attribute, 'Нельзя выбрать больше 5 ингредиентов.');
}
}]
];
}
}
public function actionIndexSite()
{
$model=new SearchForm();
return $this->render('indexsite', [
'model' => $model
]);
}
<?php $form = ActiveForm::begin(); ?>
<?php echo $form->field($model, 'ingredients')->widget(Select2::classname(), [
'language' => 'en',
'data' => $ingredients,
'options' => ['multiple' => true, 'placeholder' => 'Select ingredients'],
]); ?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
SearchForm [
'ingredients' => [
0 => '1'
]
]
Answer the question
In order to leave comments, you need to log in
validation passes and the page reloads
$model-load()
? You not only do not start validation, in general the data is not loaded into the model.
Good evening.
First, try to inherit from the main model, Ingridients, if I understand correctly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question