我在yii2视图中得到了困难的表单,其中一些字段显示或隐藏。它由用户字段选择决定,选择表单中的选项。我用自定义的jquery文件编写了这个前端逻辑。一切都很好。但是当我提交表单时,隐藏的字段没有验证,什么也没有发生。当字段隐藏并打开它时,当字段可见时,我如何杀死前端验证?
lymgl2op1#
$form->field($model, 'youAttribute', ['enableClientValidation' => false])->textInput();
字符串ActiveField类有一个属性enableClientValidation,如果你想在某些字段中禁用clientValidation,你可以简单地将这个属性设置为false。
ActiveField
enableClientValidation
false
7y4bm7vi2#
禁用客户端验证。像这样开始你的活动形式。
ActiveForm::begin(['enableClientValidation'=>false]);
字符串
isr3a4wc3#
要从验证中删除字段,请执行以下操作:
$('#yourFormID').yiiActiveForm('remove', 'yourinputID');
字符串要将字段添加到验证列表,请执行以下操作:
$('#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"}); } });
$('#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"});
}
});
型不要忘记在你的模型中使用条件验证。More info
np8igboo4#
您可以使用以下代码设置活动字段:(不是active record,确切地说是activefield)
active record
activefield
$activeField = $form->field($model, 'someField');$activeField->enableClientValidation=false;$activeField ->enableAjaxValidation=false;
$activeField = $form->field($model, 'someField');
$activeField->enableClientValidation=false;
$activeField ->enableAjaxValidation=false;
os8fio9y5#
您可以尝试为未设置的属性设置默认值:
[ // set "username" and "email" as null if they are empty [['username', 'email'], 'default'], // set "level" to be 1 if it is empty ['level', 'default', 'value' => 1],]
[
// set "username" and "email" as null if they are empty
[['username', 'email'], 'default'],
// set "level" to be 1 if it is empty
['level', 'default', 'value' => 1],
]
字符串更多信息在这里您也可以在定义验证器时使用条件客户端验证,并带有"whenClient"选项:来自手册:如果还需要支持客户端条件验证,则应配置whenClient属性,该属性接受表示JavaScript函数的字符串,该函数的返回值决定是否应用规则。例如,在一个示例中,
"whenClient"
[ ['state', 'required', 'when' => function ($model) { return $model->country == 'USA'; }, 'whenClient' => "function (attribute, value) { return $('#country').val() == 'USA'; }"],]
['state', 'required', 'when' => function ($model) {
return $model->country == 'USA';
}, 'whenClient' => "function (attribute, value) {
return $('#country').val() == 'USA';
}"],
型
mhd8tkvw6#
对于您的表单,请使用whenClient:
['name', 'required', 'when' => {serverSide Condition), 'whenClient' => "ut_utils.isAttributeVisible", ], ['name', 'string', 'min' => 2, 'max' => 28], ['name', 'trim'],
['name', 'required', 'when' => {serverSide Condition),
'whenClient' => "ut_utils.isAttributeVisible",
],
['name', 'string', 'min' => 2, 'max' => 28],
['name', 'trim'],
字符串在ut_utils(JS)中:
/** * Useful for FE validation (whenClient) to validate only if visible (ie valid input) * * @param attribute Obj containing all sorts of info about attr including container name :-) * @param value */ isAttributeVisible: function (attribute, value) { return $(attribute.container).is(':visible'); },
/**
* Useful for FE validation (whenClient) to validate only if visible (ie valid input)
*
* @param attribute Obj containing all sorts of info about attr including container name :-)
* @param value
*/
isAttributeVisible: function (attribute, value) {
return $(attribute.container).is(':visible');
},
型您还需要添加'when'来验证服务器端,您可以在这里添加特定的逻辑或使用场景来排除属性进行验证。
quhf5bfb7#
@hesselek描述的方法只有在你用表单在文件中注册一个js文件时才有效,这一点很重要
7条答案
按热度按时间lymgl2op1#
字符串
ActiveField
类有一个属性enableClientValidation
,如果你想在某些字段中禁用clientValidation,你可以简单地将这个属性设置为false
。7y4bm7vi2#
禁用客户端验证。像这样开始你的活动形式。
字符串
isr3a4wc3#
要从验证中删除字段,请执行以下操作:
字符串
要将字段添加到验证列表,请执行以下操作:
型
不要忘记在你的模型中使用条件验证。More info
np8igboo4#
您可以使用以下代码设置活动字段:(不是
active record
,确切地说是activefield
)字符串
os8fio9y5#
您可以尝试为未设置的属性设置默认值:
字符串
更多信息在这里
您也可以在定义验证器时使用条件客户端验证,并带有
"whenClient"
选项:来自手册:
如果还需要支持客户端条件验证,则应配置whenClient属性,该属性接受表示JavaScript函数的字符串,该函数的返回值决定是否应用规则。例如,在一个示例中,
型
mhd8tkvw6#
对于您的表单,请使用whenClient:
字符串
在ut_utils(JS)中:
型
您还需要添加'when'来验证服务器端,您可以在这里添加特定的逻辑或使用场景来排除属性进行验证。
quhf5bfb7#
@hesselek描述的方法只有在你用表单在文件中注册一个js文件时才有效,这一点很重要