PHP:Yii:如果前提条件未满足,则忽略ActiveForm中的表单验证器
PHP: Yii: Ignore form validators in ActiveForm if precondition not fulfilled
如果表单中的另一个字段没有被选中,我想禁用某些验证器。示例:
$fe = new FormElement(FormElement::FIELD_checkBox, 'alternative_billing_address', $this);
$fe->setLabel(Yii::t('checkout','Pick a different billing address'));
$this->addElement($fe);
$fe = new FormElement(FormElement::FIELD_textField, 'billing_first_name', $this);
$fe->setLabel(Yii::t('checkout','First name'))->addValidator('required');
$this->addElement($fe);
在这种情况下,如果第一个元素(复选框)没有被选中,我想禁用第二个表单元素的验证器。
不幸的是,这与Javascript Yii ActiveForm功能不兼容,后者阻止提交表单,因为它规定必须填写文本字段。
是否有可能在不禁用客户端验证的情况下解决此问题?谢谢
我发现了这个问题,我也有同样的问题,所以我想我会发布我的解决方案。
当使用Yii选中/取消选中复选框时,我需要禁用特定字段的客户端验证。
请注意,我的解决方案使用$form CActiveForm小部件。
所以表单代码:
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'my-form',
'enableAjaxValidation'=>false,
));
?>
<input type="checkbox" name="YourCheckbox" id="Your-Checkbox" value="1" />
<?php
echo $form->labelEx($yourModel,'FirstName');
echo $form->textField($yourModel,'FirstName');
echo $form->error($yourModel,'FirstName');
echo $form->labelEx($yourModel,'LastName');
echo $form->textField($yourModel,'LastName');
echo $form->error($yourModel,'LastName');
?>
现在我们显示javascript函数,它将禁用您指定的每个字段的验证:
function enableFieldsValidation(form, model, fieldName) {
// Restore validation for model attributes
$.each(form.data('settings').attributes, function (i, attribute) {
if (attribute.model == model && attribute.id == (model + '_' + fieldName))
{
if (attribute.hasOwnProperty('disabledClientValidation')) {
// Restore validation function
attribute.clientValidation = attribute.disabledClientValidation;
delete attribute.disabledClientValidation;
// Restore sucess css class
attribute.successCssClass = attribute.disabledSuccessCssClass;
delete attribute.disabledSuccessCssClass;
}
}
});
}
function disableFieldsValidation(form, model, fieldName) {
$.each(form.data('settings').attributes, function (i, attribute) {
if (attribute.model == model && attribute.id == (model + '_' + fieldName))
{
if (!attribute.hasOwnProperty('disabledClientValidation')) {
// Remove validation function
attribute.disabledClientValidation = attribute.clientValidation;
delete attribute.clientValidation;
// Reset style of elements
$.fn.yiiactiveform.getInputContainer(attribute, form).removeClass(
attribute.validatingCssClass + ' ' +
attribute.errorCssClass + ' ' +
attribute.successCssClass
);
// Reset validation status
attribute.status = 2;
// Hide error messages
form.find('#' + attribute.errorID).toggle(false);
// Dont make it 'green' when validation is called
attribute.disabledSuccessCssClass = attribute.successCssClass;
attribute.successCssClass = '';
}
}
});
}
一旦你在JS文件中有了这些函数,你就可以使用jQuery来检查复选框是否被选中。如果已经检查,它将启用验证,如果没有,它将禁用它。
$('#YourCheckbox').click(function() {
if ($(this).is(':checked'))
{
enableFieldsValidation($('#my-form'), 'YourModel', 'FirstName');
//enableFieldsValidation($('#my-form'), 'YourModel', 'LastName');
}
else
{
disableFieldsValidation($('#my-form'), 'YourModel', 'FirstName');
//disableFieldsValidation($('#my-form'), 'YourModel', 'LastName');
}
});
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何使用WCF服务和javascript表单post上传.doc文件
- Javascript生成的表单未提交
- 如何使用javascript或html下载PDF格式的填写表单
- HTML表单提交时未执行外部函数
- 如何将输入(type=text)从html表单传递到javascript函数
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- Ajax发布表单序列化,发布引号'
- 多级HTML表单
- 后焦点更改为IE 11中的地址栏,而不是转移到表单中的下一个控件
- 在验证和发送邮件后更改联系人表单的 html
- jQuery表单添加不适用于下拉列表
- Rails/JSON:如何将JSON用于jquery UI自动完成表单
- 如何从pdftron webviewer获取表单数据
- JS验证ajax返回的html中的表单数据
- Javascript更新孙窗口中的表单元素
- 解析javascript表单验证器
- 阻止表单元素提交
- Yii ActiveForm:通过jQuery.load加载表单
- PHP:Yii:如果前提条件未满足,则忽略ActiveForm中的表单验证器