Compare attribute

How do I use the compare validation?

I set up the validation rule in my model, but when it comes to creating the form field it states undefined attribute etc. I thought I could fix this by defining the attribute in the model but it still does not work.

Does anyone have an example of how to use this, model as well as view.

Say comparing two email fields for example?

Compare validator can works by comparing with another attribute or with a value, in your case you have to compare with another attribute.

For example:




     array('email1', 'compare', 'compareAttribute'=>'email2'),



This configuration will compare the two emails and display an evenutal error near email1.

More information here.

Have you implemented this before and it worked?

Yes, it works.

The snipped is not tested, but according to the doc is correct.

Do you have problem implementing it?

It works, thanks for your help.

Re-correction, it does not work: -

Here is the view:





<label>Email</label><br />

<?php echo $form->textField($view->user, "email") ?><br />


<label>Confirm your email</label><br />

<?php echo $form->textField($view->user, "email_confirmation") ?><br />




Here is the model:





<?php


	class User extends CActiveRecord {

		

		public $email_confirmation;

		public $password_confirmation;

		

		public static function model($className=__CLASS__) {

			

			return parent::model($className);

			

		}

		

		public function rules() {

			

			return array(

				

				array("email, first_name, last_name, password", "required"),

				array("email", "compare", "compareAttribute" => "email_confirmation"),

				array("password", "compare", "compareAttribute" => "password_confirmation")

				

			);

		

		}

		

	}


?>




What happens is, is that the error shows up "Email must be repeated exactly.", However it always states this even if the two email fields are exactly correct. Almost like it generates the error no matter what.

Also just a further note, if I leave out the public $email_confirmation variable, it states it is an undefined property of the model.

It looks like that you have no rules for email_confirmation and password_confirmation, that means that this field are not massively assigned.

Add a safe rule in order to have this field loaded:




public function rules() {

                        

                        return array(

                                

                                array("email, first_name, last_name, password", "required"),

                                array("email", "compare", "compareAttribute" => "email_confirmation"),

                                array("password", "compare", "compareAttribute" => "password_confirmation"),

                                array("password_confirmation, email_confirmation", "safe")

                        );

                

                }






Thanks, it works now.

You can also give custom error message as ,

array(‘email1’, ‘compare’, ‘compareAttribute’=>‘email2’ , ‘message’=>‘email and email12 Password do not match’),

Hi Forum!

how to compare two field in the same table and mark a message error?




array('id_moneda1', 'compare', 'compareAttribute'=>'id_moneda2', 'message'=>'Ya existe un tipo de cambio con esas monedas', 'on'=>'create'),



not sure fine this line code.

Beforehand Thanks.