PHP:Yii:如果前提条件未满足,则忽略ActiveForm中的表单验证器

PHP: Yii: Ignore form validators in ActiveForm if precondition not fulfilled

本文关键字:ActiveForm 表单 验证 如果 Yii 前提 条件 未满足 PHP      更新时间:2023-09-26

如果表单中的另一个字段没有被选中,我想禁用某些验证器。示例:

$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');
    }
});