php - How to put 'is not null' parameter in Laravel 4 validator for uniqueness
For an extended use case for a user signup process, I need to allow emails to be taken which are not confirmed yet. That means the emails that are not yet confirmed are considered open.
Inusers
table I useconfirmed_at
field to denote if an email is confirmed or not. If the field isnull
then it's not confirmed. Otherwise there'll be a date.
I am trying out with this:
$v = Validator::make(Input::all(), array(
'email' => 'required|email|unique:users,email,NULL,confirmed_at',
'password' => 'required|confirmed|min:6|max:32',
));
This results in the following query:
select count(*) as aggregate from `users` where `email` = '[email protected]'
Surprisingly it's not even taking theconfirmed_at
field in.
Although the doc doesn't say about a predefined id field as second parameter, I tried my luck with the following:
$v = Validator::make(Input::all(), array(
'email' => 'required|email|unique:users,email,0,id,confirmed_at,NULL',
'password' => 'required|confirmed|min:6|max:32',
));
Now it includes the other parameters in the query and results into:
select count(*) as aggregate from `users` where `email` = '[email protected]' and `id` <> '0' and `confirmed_at` is null
But it employs not equal for the first id field but equal for the confirmed_at field.
So, my first question is, how can I getconfirmed_at is not null
in to the query?
And secondly, how possibly I could get rid ofid <> 0
which I had to add although I don't need it?
Answer
Solution:
You can always use
Custom Validators
An example for your use case:
More information in the official documentation: http://laravel.com/docs/validation#custom-validation-rules