I have got difficult form in yii2 view, where some fields show or hide. It decide from user field choises, select options in the form. I write this frontend logic with custom jquery file. All is ok. But when I submit form - hidden fields stay without validation and nothing is happend.How I can kill ofrontend validation, when fields are hiiden and switch on it, when fields are visible?
$form->field($model, 'youAttribute', ['enableClientValidation' => false])->textInput();
The ActiveField
class has a property enableClientValidation
, you can simply set this property to false
if you want to disable clientValidation form some fields.
To disable client side validation. Begin your active form like this.
ActiveForm::begin(['enableClientValidation'=>false]);
To remove a field from validation:
$('#yourFormID').yiiActiveForm('remove', 'yourinputID');
To add a field to validation list:
$('#yourFormID').yiiActiveForm('add', {
id: 'country',
name: 'yourinputID',
container: '.field-inputID', //or your cllass container
input: '#yourinputID',
error: '.help-block', //or your class error
validate: function (attribute, value, messages, deferred, $form) {
yii.validation.required(value, messages, {message: "Validation Message Here"});
}
});
And don't forget conditional validation in your model. More info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With