Problem With Form Validation In Model

In view,


<?php 

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

		'id'=>'forgot-form',

		'action'=>'/Index.php/site/forgot',

		'enableClientValidation'=>true,

		'clientOptions'=>array('validateOnSubmit'=>true),		

	));   ?>


<div class="frmFields" style="margin: 0 0 0 40px;">

<?php echo $form2->labelEx($model,'forgotpassword', array("style"=>"color:#C29A4B; font-weight:bold")); ?>

<?php echo $form2->emailField($model,'forgotpassword', array("style"=>"border:1px solid #DCDCDD")); ?>

<?php echo $form2->error($model,'forgotpassword'); ?>

</div>

In Controller,




public function actionForgot($pswdsent='no')

	{

		$model=new LoginForm('sendpaswd'); 

		if($pswdsent = 'no'){

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

		  { 

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

			if($model->validate()) 

			{ die("123");

.............

...............

In LoginForm model,




array('forgotpassword', 'checkemail', 'on'=>'sendpaswd'),




public function checkemail($attribute,$params)

	{

			$command = Yii::app()->db->createCommand();

        $result = $command->select('userregistration.*')

				->from('userregistration')

				->where('email = "'.$this->forgotpassword.'"')

				->queryAll();

				

		

		if(count($result)==0)

			$this->addError('forgotpassword','Email is not registered. Enter correct email address');

		else			

			return true;

	}

If I enter an unregistered email and submit the form I get the correct error message. But if I enter a registered email the model doesn’t validate and I don’t get the die output 123

As you have only two validation rules there is no sense to list those two fields to be validated in the validates(). Try this way:

function enquire(){

if(&#036;this-&gt;data){


    &#036;this-&gt;Enquiry-&gt;set( this-&gt;data);


    if(&#036;this-&gt;Enquiry-&gt;validates()){


        // it validated logic


    }else{


        // didn't validate logic


    }


}

}

Your validation array should be (follow syntax consistency):

var $validate = array(

'name' =&gt; array(


    'notEmpty' =&gt; array(


        'rule' =&gt; 'notEmpty',


        'message' =&gt; 'Name is required'


    )


),


'email' =&gt; array(


    'emailFormat' =&gt; array(


        'rule' =&gt; 'notEmpty',


        'message' =&gt; 'Email is required'


    ),


    'emailNeeded' =&gt; array(


        'rule' =&gt; array('email', true),


        'message' =&gt; 'Must be a valid email address'


    )


)

);


if($pswdsent = 'no'){

should be


if($pswdsent == 'no'){

It’s been a while since I wrote a custom validation rule so maybe it’s changed; but neither the code in this article, nor this one, returns true.

Matt