我有这段代码:
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')
);
}
问题是它在更新时检查唯一性。我不希望这样。仅当我创建。
时我输错了什么?
答案 0 :(得分:1)
尝试类似
的内容validates_uniqueness_of :name, :on => :create
validates_uniqueness_of :email, :on => :create
这应该可以做到这一点,试一试=)
答案 1 :(得分:0)
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)
调用进行更新,因此您跳过了验证。有点棘手。