Custom Validation Rules In User Module

Hi,

In Yii User Module registration I need to restrict the users who enters the public email id’s such as rathnam@gmail.com or rathnam@yahoo.com etc …allow only corporate mail id’s.

…To do this I added new custom validation rule in rules under models protected/modules/user/model/User.php…




array('email', 'email'),                      

array('email', 'validateEmail','message'=>UserModule::t("Only Corporate mail id's allowed.")),



Added the function to validate that… in protected/modules/user/model/User.php





public function validateEmail($attribute,$params)

        {

             $string1=$this->email;               

             

             $invalid_formats = array("gmail.com", "yahoo.com", "hotmail.com", "rediff.com","aol.com");

             list($txt, $ext) = explode("@", $string1);

            if(in_array($ext,$invalid_formats))

             {

		

              $this->addError($attribute, 'Only Corporate mails allowed');

             } else {

		return true;

            }

            

        }




But when I submit a registration form with public email domains …validations does not trigger …any solution for this?

Hi,

Have you add this enableAjaxValidation and clientOptions on form page?




<?php

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

                                    'id' => 'vendor-form',

                                    //'action' => 'AddVenue',

                                    'enableAjaxValidation' => false,

                                    'enableClientValidation' => true,

                                    'clientOptions' => array(

                                        'validateOnSubmit' => true,

                                        'afterValidate' => 'js:checkErrors',

                                    ),

                                    //'stateful' => false,

                                    'htmlOptions' => array(

                                        //'onsubmit' => "return false;",

                                        'enctype' => 'multipart/form-data'

                                    )

                                ));

                                ?>





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

	'id'=>'registration-form',

	'enableAjaxValidation'=>true,

	'disableAjaxValidationAttributes'=>array('RegistrationForm_verifyCode'),

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

	),

	'htmlOptions' => array('enctype'=>'multipart/form-data'),

)); ?>






Yes those are enabled …other validations works as usual only the custom validation rule does not working?

Hello rathnam,

try this hear,




array('email', 'email'),                      

array('email', 'validateEmail'),



and put the message in addError.





public function validateEmail($attribute,$params)

        {

             $string1=$this->email;               

             

             $invalid_formats = array("gmail.com", "yahoo.com", "hotmail.com", "rediff.com","aol.com");

             list($txt, $ext) = explode("@", $string1);

            if(in_array($ext,$invalid_formats))

             {

                

              $this->addError($attribute, UserModule::t("Only Corporate mail id's allowed."));

             } else {

                return true;

            }

            

        }




cheers

In your controller, in create or update, after you load the model, but before you load it from POST, call this




if(Yii::app()->getRequest()->getIsAjaxRequest()) {


echo CActiveForm::validate( array( $model)); 


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


}

please see it…

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

i hope it;s some help…

Yes I do call this in Controller …




// ajax validator

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

			{

				echo UActiveForm::validate(array($model,$profile));

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

			}



I have two models here 1.User.php and 2.RegistrationForm.php

Do I need to add the rules in 2.RegistrationForm.php also …now I added only in User.php only…If I add the code in RegistrationForm.php model …my entire form does not submit?

have you not created custom model ?

Custom rule I created in User model. Why do we need custom model here?

because if you want to use this model on admin or any module so how can specify the rule function

so that’s why i ask create the custom model

please see this my post how to create the custom model and apply the validation rule

http://www.yiiframework.com/forum/index.php/topic/45890-ajax-validation-is-not-performed-for-second-model/page__p__216098__fromsearch__1#entry216098

hope it’s help…

Guys got the solution:

  1. We have to define the rules in both the models i.e User.php and RegistrationForm.php.

 array('email', 'validateEmail'),

Define the Validate function only in primary model …here User.php.

It works Great!

Friends thank you for your inputs! Especially @Ankit.

Hi,

can you post the rules function on both model?