仅在创建时验证唯一性 - 我输错了什么?示例提供

时间:2013-09-03 16:47:02

标签: activerecord phpactiverecord

我有这段代码:

 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')
   );
  }

问题是它在更新时检查唯一性。我不希望这样。仅当我创建

我输错了什么?

2 个答案:

答案 0 :(得分:1)

尝试类似

的内容
validates_uniqueness_of :name, :on => :create
validates_uniqueness_of :email, :on => :create

这应该可以做到这一点,试一试=)

答案 1 :(得分:0)

嗯,这应该按照the manual工作,但如果不是你最好的尝试可能是自定义验证功能,如该链接中所述:

  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.");
      }
   }
 }

另一种选择是使用save(false)调用进行更新,因此您跳过了验证。有点棘手。