beforeSave函数之后的CakePHP 2.x验证


CakePHP 2.x validation after beforeSave function

我正试图在CakePHP中的Model中添加一个验证规则,以检查ip地址是否唯一。问题是,我将数据库中的ip地址保存为无符号int,但用户将其作为字符串输入。为此,我使用了一个beforeSave函数,该函数将ip地址更改为将要保存的int值。有没有办法让isUnique规则在beforeSave函数之后运行?目前我的验证规则是这样的。

    public $validate = array(
    'ip_address' => array(
        'notEmpty' => array(
            'rule' => 'notEmpty',
            'message' => 'You must enter an IP address'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'required' => 'create',
            'message' => 'This IP address already exists'
        )
    )
);

beforeValidate()中执行此操作:

$this->data['alias']['ip_address'] = str_replace('.', '', $this->data['alias']['ip_address'];

它会很好用的。为什么是int而不是string?int可能会给你重复项。我不知道你把ip变成int的代码,所以我可能错了。