Validate uniqueness only on create - what am I typing wrong? Example provided

1k Views Asked by At

I have this code :

 class Member extends ActiveRecord\Model
  {
   static $validates_uniqueness_of = array(
      array('name', 'message' => "That name has already been registered. Please choose another", 'on' => 'create'),
      array('email', 'message' => "That email has already been registered. Please choose another", 'on' => 'create')
   );
  }

The problem is that it checks uniqueness when I update. I don't want that. Only when I create.

What am I typing wrong?

2

There are 2 best solutions below

0
On

hmm, that should work according to the manual, but if it doesn't your best try might be the custom validat function, as explained in that link:

  class User extends ActiveRecord\Model
  {
    public function validate() 
    {
      if ( your_uniqueness_check_here )
      {
        $this->errors->add('namecheck', "can't be the same as someone elses.");
      }
   }
 }

Another option would be to update using the save(false) call, so you skip the validation. Is a bit tricky as well.

2
On

Try something like

validates_uniqueness_of :name, :on => :create
validates_uniqueness_of :email, :on => :create

This should do the trick already, give it a try =)