Problem With Email Validation

Hi there,

Im new to this forum but i really like it, I have already found alot of answers here.

Anyway, I must say that I just started working with the yii framework.

I’m working on this project and the problem is when I register with an email address which is already in the database, there is no error coming onto the screen, but just a blank page…

This is my registerForm.

<?php

/**

  • LoginForm class.

  • LoginForm is the data structure for keeping

  • user login form data. It is used by the ‘login’ action of ‘SiteController’.

*/

class RegistreerForm extends CFormModel

{

public &#036;firstname;


    public &#036;lastname;


    public &#036;email; 


    public &#036;riziv; 


public &#036;code_van_de_orde;


    public &#036;phone_number;





private &#036;_identity;





/**


 * Declares the validation rules.


 * The rules state that username and password are required,


 * and password needs to be authenticated.


 */


public function rules()


{


	return array(


		


		array('firstname, lastname,email,riziv, code_van_de_orde', 'required', 'message'=&gt;'Dit veld is verplicht&#33;'),


                    array('email','unique'),


                    array('email','email','message'=&gt;'Ongeldig email adres&#33;'),


                    array('email', 'length', 'max'=&gt;45),


                    array('firstname, lastname', 'length', 'max'=&gt;25),


                    array('phone_number', 'match', 'pattern'=&gt;'/^([0-9]{2,3}[/]{0,1}[0-9]{2,3}[-. ]{0,1}[0-9]{2}[-. ]{0,1}[0-9]{2})&#036;/'


                        ,'message'=&gt;'Ongeldig telefoonnr&#33;'),


                    array('email, firstname, lastname, riziv, code_van_de_orde', 'safe', 'on' =&gt; 'search'),


                    


                    


                    


	);





}





/**


 * Declares attribute labels.


 */


public function attributeLabels()


{





}





/**


 * Authenticates the password.


 * This is the 'authenticate' validator as declared in rules().


 */


public function authenticate(&#036;attribute,&#036;params)


{


	if(&#33;&#036;this-&gt;hasErrors())


	{


		&#036;this-&gt;_identity=new UserIdentity(&#036;this-&gt;email,&#036;this-&gt;code_van_de_orde);


		if(&#33;&#036;this-&gt;_identity-&gt;authenticate())


			&#036;this-&gt;addError('code_van_de_orde','Fout, sommige velden zijn niet correct ingevuld.');


	}


        


}





/**


 * Logs in the user using the given username and password in the model.


 * @return boolean whether login is successful


 */


public function registreer()


{


echo 'functie registreer';


    


    return true;


}

}

This is the method in my controller.

public function actionRegistreer()

    {


        &#036;model=new RegistreerForm;


        &#036;newUser = new User;





        


        // if it is ajax validation request


	if(isset(&#036;_POST['ajax']) &amp;&amp; &#036;_POST['ajax']==='registreer-form')


	{


		echo CActiveForm::validate(&#036;model);


		Yii::app()-&gt;end();


                    


	}


        





	if(isset(&#036;_POST['RegistreerForm']))


            {


                    &#036;model-&gt;attributes=&#036;_POST['RegistreerForm'];


                    &#036;newUser-&gt;firstname = &#036;model-&gt;firstname;


                    &#036;newUser-&gt;lastname = &#036;model-&gt;lastname;


                    &#036;newUser-&gt;email = &#036;model-&gt;email;


                    &#036;newUser-&gt;riziv = &#036;model-&gt;riziv;


                    &#036;newUser-&gt;code_van_de_orde = &#036;model-&gt;code_van_de_orde;


                    &#036;newUser-&gt;phone_number = &#036;model-&gt;phone_number;


                    


                            


                    if(&#036;newUser-&gt;save()) {


                            &#036;identity=new UserIdentity(&#036;newUser-&gt;email,&#036;model-&gt;code_van_de_orde);


                            &#036;identity-&gt;authenticate();


                            Yii::app()-&gt;user-&gt;login(&#036;identity,0);


                            


                            &#036;categories = Category::model()-&gt;findAll();


                            &#036;labResults = LabResult::model()-&gt;findAll();


                             &#036;this-&gt;render('index',array('categories'=&gt;&#036;categories, 'labResults'=&gt;&#036;labResults));


                          





                    }


                            


            }


              else


	&#036;this-&gt;render('registreer',array('model'=&gt;&#036;model));


    }

And the view.

<?php

/* @var $this SiteController */

/* @var $model LoginForm */

/* @var $form CActiveForm */

$this->layout=‘main2’;

$this->pageTitle=Yii::app()->name . ’ - Registreer’;

$this->breadcrumbs=array(

'Registreer',

);

?>

<h1>Registreer</h1>

<div class="form">

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

'id'=&gt;'registreer-form',


'enableClientValidation'=&gt;true,


'clientOptions'=&gt;array(


	'validateOnSubmit'=&gt;true,


),

)); ?>

&lt;p class=&quot;note&quot;&gt;Velden met een &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; zijn verplicht.&lt;/p&gt;


    


    


    &lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'firstname'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'firstname'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'firstname'); ?&gt;


&lt;/div&gt;


    


    &lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'lastname'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'lastname'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'lastname'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'email'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'email'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'email'); ?&gt;


&lt;/div&gt;


    


    &lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'phone_number'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'phone_number'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'phone_number'); ?&gt;


&lt;/div&gt;


    


    &lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'riziv'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'riziv'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'riziv'); ?&gt;


&lt;/div&gt;


    





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'code_van_de_orde'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'code_van_de_orde'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'code_van_de_orde'); ?&gt;





&lt;/div&gt;








&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton('Registreer'); ?&gt;


&lt;/div&gt;

<?php echo $form->errorSummary($model); ?>

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

</div><!-- form -->

Try to change the following line (it’s inside RegistreerForm::rules() method):


array('email','unique'),

To the:


array('email','unique','className'=>'User','attributeName'=>'email'),

Still does the same thing. But thanks for the reply!

Right! Didn’t noticed that you’re validating data in the User AR-model too. I’ve thought that you’re validating only RegistreerForm model since you’re displaying it. So post your User class text here or try to add the rule shown above to the User AR-model.




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

{

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

$newUser->firstname = $model->firstname;

$newUser->lastname = $model->lastname;

$newUser->email = $model->email;

$newUser->riziv = $model->riziv;

$newUser->code_van_de_orde = $model->code_van_de_orde;

$newUser->phone_number = $model->phone_number;




if($newUser->save()) {



By the way i think that distributing data validation across two models is a bit bad approach. Massive assignment happens in the RegistreerForm model while the actual validation is in the User AR-model. You’d better do both in one place.

And yes, note that there is dedicated BB-code for posting source code. Would be much easier for other members to review code in such code blocks and help you! :wink:

So how should I fix this?

Thanks for the help so far.