【问题标题】:PHP: Yii: Ignore form validators in ActiveForm if precondition not fulfilledPHP:Yii:如果未满足前提条件,则忽略 ActiveForm 中的表单验证器
【发布时间】:2012-02-15 15:00:36
【问题描述】:

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

$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 功能一起使用,该功能会阻止提交表单,因为它说必须填写文本字段。

是否有可能在不禁用客户端验证的情况下解决此问题?谢谢!

【问题讨论】:

  • 这与表单验证器兼容吗?或者它只是与模型规则兼容?并且:即使它有条件地进行后端验证,它也可以在客户端(javascript 验证器)上工作吗?因为我的问题不是让它在后端工作,而是让客户端在条件下工作。
  • 你应该去yii论坛问问。

标签: php javascript validation yii


【解决方案1】:

我发现了这个问题并且遇到了同样的问题,所以我想我会发布我的解决方案。

当使用 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');
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多