对于用户注册过程的扩展用例,我需要允许进行尚未确认的电子邮件。这意味着尚未确认的电子邮件被视为已打开。
在users
表中,我使用confirmed_at
字段来表示是否确认了电子邮件。如果该字段为null
,则表示未确认。否则就会有约会。
我正在尝试这个:
$v = Validator::make(Input::all(), array(
'email' => 'required|email|unique:users,email,NULL,confirmed_at',
'password' => 'required|confirmed|min:6|max:32',
));
这导致以下查询:
select count(*) as aggregate from `users` where `email` = 'email@gmail.com'
令人惊讶的是,它甚至没有使用confirmed_at
字段。
虽然doc没有说明预定义的id字段作为第二个参数,但我尝试了以下运气:
$v = Validator::make(Input::all(), array(
'email' => 'required|email|unique:users,email,0,id,confirmed_at,NULL',
'password' => 'required|confirmed|min:6|max:32',
));
现在它将查询中的其他参数包括在内,结果为:
select count(*) as aggregate from `users` where `email` = 'email@gmail.com' and `id` <> '0' and `confirmed_at` is null
但它对于第一个id字段使用不相等,但对confirm_at字段使用相等。
所以,我的第一个问题是,我如何才能获得confirmed_at is not null
查询?
其次,我怎么可能摆脱id <> 0
我必须添加但我不需要它?
答案 0 :(得分:2)
您始终可以使用Custom Validators
您的用例示例:
Validator::extend('unconfirmed', function($attribute, $value, $parameters)
{
return (boolean)DB::table('users')->where('confirmed', 'is not', null)->count();
});
官方文档中的更多信息:http://laravel.com/docs/validation#custom-validation-rules