Error in model validation save()

Hi! I have a model "Usuarios" with some attributes like nombre, email, clave, activado…

I put a new record with the attributte "activado" to 0 by default.




public function rules()

	{		

		return array(

			array('nombre, email, clave, activado', 'required'),

			array('activado', 'numerical', 'integerOnly'=>true),

			array('email', 'email'),

			array('email','unique'),			

			array('nombre, email, clave', 'length', 'max'=>255),

			

			array('id_usuario, nombre, email, clave, activado', 'safe', 'on'=>'search'),

		);

	}



I have a controller for update "activado" to 1.





$usuario=Usuarios::model()->findByAttributes(array('clave'=>$clave));

if($usuario!=null)

{	

	$usuario->activado=1;

	if($usuario->save()) echo "updated";

}






I don’t know why this doesn’t works, it’s just a simple update.

  1. If changue de save() funcion to save(false) works fine. No validation.

  2. If I comment the line “array(‘email’,‘unique’)” of rules, works too.

Help please! sorry for my english.

Hi,

Which exact error do you get?

Nothing, it doesn’t update the record.

Of course it doesn’t update, because it already has the email address, therefore the unique validation fails.

You need to specify a condition for your unique validator, like:




array('email', 'unique', 'attributeName'=>'email', 'className'=>'Usuarios', 'criteria'=>array('condition'=>'id_usuario <> :id','params'=>array(':id'=>(int)$this->id_usuario))),

            



hi, thanks for answering.

I think that’s too advanced for me…

But, its only an update, not creating a new record with the same email. For example id_usuario it’s a primary key and unique, why this not fail?

Thanks.

solved, the unique value isn’t the problem. Was a public attribute in my model.

I check $usuarios->errors after save();

Hi,

What did you do to solve this problem. Please let me know as I am having the similar problem.

Regards