我输入了$data =['identifier' = 'xxxxxxxxxx'];,并希望将encrypt($data['identifier'])保存到表info主id列中。
在保存之前我要验证一下。规则unique:info, id在这里不合适,所以我想编写一个自定义验证规则。在自定义验证规则中,我首先encrypt()值,然后使用unique验证规则。
我知道如何编写自定义验证规则,但是如何在我的自定义验证规则中使用unique验证规则?
发布于 2017-07-04 13:24:22
规则“唯一”和“存在”使用DatabasePresenceVerifier类。所以,您不需要真正扩展唯一的规则,只需访问这个存在验证器。例如:
Validator::extend('encrypted_unique', function ($attribute, $value, $parameters, $validator) {
    list ($table, $column, $ignore_id) = $parameters; // or hard-coded if fixed
    $count = $validator->getPresenceVerifier()->getCount($table, $column, encrypt($value), $ignore_id);
    return $count === 0;
});然后你可以像往常一样使用它:
'identifier' => "encrypted_unique:table,column,$this_id"https://stackoverflow.com/questions/44793365
复制相似问题