Email already exists does not work

Hello,

I’m trying to add a validation code to check whether or not the Email is exists in DB.

My code is as follow -

Model - Eventadminusers.php




public function rules()

{

     return array(

	array('firstName, lastName, email, password, confirmPassword, contactNumber, eventTitle, active', 'required'),

.

.

.

	array('email', 'unique', 'on'=>'insert', 'message'=>'This Email already exists!')

     );

}



Controller -




public function actionAdd()

{

     if(Yii::app()->user->isGuest)

     {

	$this->redirect(array('site/login'));

     }

     else

     {

	$model=new Eventadminusers;

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

	{

	     $formdata = Yii::app()->request->getPost('Eventadminusers');

	     $flag=Eventadminusers::add_adminuser_details($formdata);

             $this->redirect(array('eventadminusers/index'));

	}

	else

	{

	     $this->render('eventadminuserAdd',array(

		 'model'=>$model

	     ));

	}

     }

}



View -




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

	'id'=>'add-user-form',

	'enableClientValidation'=>true,

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

	),

	'htmlOptions' => array(

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

                'class' => 'form-horizontal form-banded form-bordered'

	),

)); ?>


<?php echo $form->labelEx($model, 'email <span class="mandatory">*</span> &nbsp;', array('class' => 'control-label')); ?>

<?php echo $form->textField($model, 'email', array('class' => 'form-control')); ?>

<?php echo $form->error($model, 'email'); ?>


<div class="form-footer col-lg-offset-2 col-md-offset-2 col-sm-offset-3">

     <?php echo CHtml::submitButton('Save', array('class' => 'btn btn-primary', 'style'=>'margin-left:10px;')); ?>

</div>

<?php $this->endWidget(); ?>



This code is not working for me. The ‘email’ column in table is defined as ‘unique’.

Please help to make this work. Thank you in advance. Any help will be appreciated.

Try


$model=new Eventadminusers('insert');

instead of


$model=new Eventadminusers;

Edit:

Oh, ‘insert’ is default one so this will not help. You have to paste here code for Eventadminusers::add_adminuser_details

Hi Bizley,

This is the code that you asked -




public function add_adminuser_details($data)

{

     $eid=$data['eventId'];

                

     $params = array('firstName'=>$data['firstName'],'lastName'=>$data['lastName'],'email'=>$data['email'],'password'=>md5($data['password']),'contactNumber'=>$data['contactNumber'],'userType'=>2,'referenceId' => $eid,'active'=>$data['active'],'createdOn'=>date("Y-m-d H:i:s"),'updatedOn'=>date("Y-m-d H:i:s"));

		

     $add = Yii::app()->db->createCommand()

		->insert('admin_users', $params);

     return $add;

}



Rules are not used when you’re using the query builder. I would switch to activerecord and do everyhting in your controller.




public function actionAdd()

	{

		$model = new Eventadminusers('insert');

		

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

		{

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

			

			if($model->save())

			{

				$this->redirect(array('/'));

			}

		}

		

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

	}



If you want to keep your current structure you can add a second query along the lines of:




$email_exists = Yii::app()->db->createCommand()

                ->select('email')

                ->from('admin_users')

                ->where('email = :email', array(':email'=>$data['email']))

                ->queryScalar(); // Get the first column of the first result


if(!empty($email_exists))

{

     $eid=$data['eventId'];

                

     $params = array('firstName'=>$data['firstName'],'lastName'=>$data['lastName'],'email'=>$data['email'],'password'=>md5($data['password']),'contactNumber'=>$data['contactNumber'],'userType'=>2,'referenceId' =>    $eid,'active'=>$data['active'],'createdOn'=>date("Y-m-d H:i:s"),'updatedOn'=>date("Y-m-d H:i:s"));

                

     $add = Yii::app()->db->createCommand()

                ->insert('admin_users', $params);

     return $add;

}



Hello Ryan Z,

 Thanks for your feedback. From your 2 suggestions, 2nd suggestions is not usefull for my scenario. Because it does check the email exist but doesnt show any error message (which is my requirement).





 For your 1st suggestion, it worked as i wanted. It shows the error message after submit. But when i put a new non-exist email, it does not save anything. How can i find out what is the exact problem in &#036;model-&gt;save() ?

You can get all errors by reading $model->getErrors().

Thats great !! All worked perfect. Thank you guys. :rolleyes: