Open dicrtarasov opened 4 years ago
The temporary workaround is to turn off enableClientValidation
for all radioList
, checkboxList
, file
and other fields, which using bootstrap4 custom-control
template.
echo $form->field($model, 'myfield', [
'enableClientValidation' => false
])->radioList([
0 => 'myval 0',
1 => 'myval 1',
]);
Remove the trim
rule from your model and it'll work.
Wow, you are right. I see only trim
and file
validation functions of yii.validations.js
use id to find input
. Thank..... But this bug must be fixed, because this is bug.
Is it a bug though? Why do you need to trim the value of a radio/checkbox input?
Why need - this is other question from other story.
Because model propagate data not only from web form posing, but other sources.
You can use a scenario then.
When POSTing with the form don't use the trim
rule.
https://www.yiiframework.com/doc/guide/2.0/en/structure-models#scenarios
I think, that a lot of workarounds - is not a preferred programming way of Qiang Xue.
Lets's see yii.activeForm.js
- it already has this fix in function findInput
:
var findInput = function ($form, attribute) {
var $input = $form.find(attribute.input);
if ($input.length && $input[0].tagName.toLowerCase() === 'div') {
// checkbox list or radio list
return $input.find('input');
} else {
return $input;
}
};
So, I think that trim
in yii.validation.js
must also be fixed, than advise everyone to look for workarounds.
Created pull request: https://github.com/yiisoft/yii2/pull/17894
The trim
function in client validation will not work with checkbox/radio inputs.
Even on a single checkbox/radio it'll return the value unchanged.
It will, because of trim code:
if ($input.is(':checkbox, :radio')) {
return value;
}
Currencty it produce validation error on correct field, so this is bug.
The fix is the save as in activeForm
findInput
function:
if ($input.length && $input[0].tagName.toLowerCase() === 'div') {
// checkbox list or radio list
$input = $input.find('input');
}
Also, the input
selector is incorrect in generated plugin script:
https://github.com/yiisoft/yii2/issues/17895
TestModel.php
view.php
After pressing submit button, client validation is run, get error:
Myfield cannot be blank.
and submitting canceled.I found the problem is connected with yii2-bootstrap4
custom-control
functional, which layout is incompatiple with yii.validation.js. The generated HTML-code for field is:Here we see, that
id="testmodel-myfield"
assigned not toinput
element, butdiv
, which is complex input group. Theinput
s itself get strageid="i0"
ids ...yii.validation.js
Validation function search input element by attribute.input and instead of
input
it get thediv
withid="testmodel-myfield"
, so next operations will fail, because$input.is(':checkbox, :radio')
can't be applied todiv
.