1

I want to disable certain validators if another field in the form is not checked. Example:

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

In this case, I want to disable the validator of the second form element if the first element (checkbox) has not been checked.

Unfortunately this does not work together with the Javascript-Yii-ActiveForm feature, which prevents the submission of the form because it says that the text field must be filled out.

Is there any possibility to solve this without disabling the client validation? Thanks!

3
  • yiiframework.com/extension/yii-conditional-validator Commented Feb 16, 2012 at 1:16
  • Is this compatible with form validators? Or is it just compatible with model rules? And: Even if it is doing the backend validation conditionally, does it work on the client side (javascript validator), too? Because my issue was not about getting it to work in the backend, but to get the client side working with conditions. Commented Feb 16, 2012 at 11:51
  • you should ask in yii forum instead. Commented Feb 16, 2012 at 23:43

1 Answer 1

1

I found this question and I had the same issue, so I thought I would post my solution.

I needed to disable clientSide validation on specific fields when a checkbox was checked/unchecked using Yii.

Note my solution uses the $form CActiveForm widget.

So the form code:

<?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'); 
?>

Now we display the javascript functions which will disable the validation for each field you specify:

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

Once you have these functions in your JS file you can use jQuery to check if the checkbox has been checked. If it has been checked it will enable validation and if not it will disable it.

$('#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');
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.