My Custom Validation Function Not Being Called By Rules()

For some reason my custom validation function that I have in my rules array are not being called. I have a model for a form with the following rules




    public function rules()

    {

        return array(

            // username and password are required

            array('business_name,first_name,last_name,phone_number,re_email,street_address,city,state,zip_code,sub_domain,terms', 'required'),

            // check email format

            array('email', 'email', 'checkMX'=>true),

            // email needs to be authenticated

            array('email', 'authenticate_email'),

            // subdomain needs to be authenticated

            array('sub_domain', 'authenticate_subdomain'),

            // email and re_email must be identical

            array('re_email', 'compare', 'compareAttribute'=>'email', 'operator'=>'==', 'message'=>'The email addresses you entered do not match.'),

        );

    }



All the rules run and work fine except the two CUSTOM rules




            array('email', 'authenticate_email'),

            array('sub_domain', 'authenticate_subdomain'),



authenticate_email and authenticate_subdomain don’t really do anything yet except just add an error




    public function authenticate_email($attribute,$params)

    {

Yii::app()->session['here']=1;

        if(!$this->hasErrors())

        {

            $this->addError('email','yep error');

        }


    }


    public function authenticate_subdomain($attribute,$params)

    {

Yii::app()->session['heretoo']=1;

        if(!$this->hasErrors())

        {

            $this->addError('sub_domain','wow error');

        }

    }



Here is a look at the call to my form widget in my view




        <?php $form=$this->beginWidget('CActiveForm', array(

            'id'=>'start-form',

            'enableClientValidation'=>true,

            'clientOptions'=>array(

                'validateOnSubmit'=>true,

                'afterValidate' => 'js:function(form, data, hasError) { 

                    if(hasError) {

                        for(var i in data) {

                            $("#"+i).addClass("error_input");

                        }

                        $("#error_div").show();

                        $(window).scrollTop($("#error_div").position());

                        return false;

                    }

                    else {

                        form.children().removeClass("error_input");

                        $("#error_div").hide();

                        return true;

                    }

                }',

                'afterValidateAttribute' => 'js:function(form, attribute, data, hasError) {

                    if(hasError) 

                    {

                        $("#"+attribute.id).addClass("error_input");

                    } else {

                        $("#"+attribute.id).removeClass("error_input");

                    }

                }'

            ),

            'htmlOptions'=>array(

                'class'=>'reg-page',

            ),

        )); ?>



All of the other validation rules will create the appropriate errors, but the custom validation functions are not even being ran. I can tell because they are not adding the variables “here” and “heretoo” to the session. I also put a die() call in both just to confirm the functions were not being called. I looked at the built in LoginForm.php for inspiration but I can’t see any significant difference to my setup. I hope there is something obvious to you all that I am missing.

Hi Gilberg

  1. did you check your code by default _form submit ? (by crud gii generator)

  2. where are your fields in CActiveForm ?

  3. how render your view file? please post your controller/action

Some validators don’t support the client validation.

Custom validators are among them because they can’t generate the javascript which is needed for the client validation.

Try enabling the ajax validation.




        <?php $form=$this->beginWidget('CActiveForm', array(

            'id'=>'start-form',

            'enableClientValidation'=>true,

            'enableAjaxValidation'=>true, // this is it

            'clientOptions'=>array(



hi

please check this link

http://help.discretelogix.com/php/yii/how-to-create-custom-validation-rule.htm

it may be helpful

Got it. It was a combination of two problems. I didn’t have enableAjaxValidation set to true. I did so, but it still wasn’t working. Then I found a typo in my controller/action. I was looking in the post for “startnow-form”, but it should have been “start-form”.




    public function actionStartNow()

    {

        $model=new startForm;

        // if it is ajax validation request

        if(isset($_POST['ajax']) && $_POST['ajax']==='start-form')

        {   

            echo CActiveForm::validate($model);

            Yii::app()->end();

        }   


        if(isset($_POST['StartNowForm']))

        {

            $model->attributes=$_POST['StartNowForm'];

            if ($model->validate()) {


            }

        }

        $this->render('start', array('model'=>$model));

    }