Search code examples
phpyiiyii1.x

How to use YiiConditionalValidator to work on client side?


I'm using YiiConditionalValidator.php extension to Yii 1.1.20. I want to have required field (master_id) when i switch button (is_master) from 1 to 0 ...

is_master - 1 or 0 master_id - if "is_master" = 0 make "master_id required...

So... my model rule looks like this:

public function rules()
{
    return [
        ['is_master', 'required'],
        ['is_master', 'validators.YiiConditionalValidator',
            'if' => [
                ['is_master', 'compare', 'compareValue'=> "0"],
            ],
            'then' => [
                ['master_id', 'required'],
            ],
        ],
        ['is_master, master_id', 'safe', 'on' => 'search'],
    ];
}

And in my form i have this options set:

'enableAjaxValidation' => false,
'enableClientValidation' => true,
'clientOptions' => array(
    'validateOnChange' => true,
    'validateOnSubmit' => true,
),

It seems like my $form cant see this conditional rule...

Thanks for any Help!


Solution

  • While no one knows ^____^ ... I made little research and the answer is that in YiiConditionalValidator.php one function is missing...

    When you look at yii 1.1.x framework/validators most of them have 2 functions:

    1. protected function validateAttribute($object,$attribute) and
    2. public function clientValidateAttribute($object,$attribute)

    And that's why YiiConditionalValidator.php isn't working on client side because it lacks public function clientValidateAttribute($object,$attribute) which "Returns the JavaScript needed for performing client-side validation"...

    If you want to know how does it look like check your project folder/framework/validators

    Best Regards! Tom