Problem With Form Validation In Model

In view,


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





	));   ?>

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


In Controller,

public function actionForgot($pswdsent='no')


		$model=new LoginForm('sendpaswd'); 

		if($pswdsent = 'no'){





			{ die("123");



In LoginForm model,

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

public function checkemail($attribute,$params)


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

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


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





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


			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(){


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


        // it validated logic


        // 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.