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?




You can always useCustom Validators

An example for your use case:

Validator::extend('unconfirmed', function($attribute, $value, $parameters)
    return (boolean)DB::table('users')->where('confirmed', 'is not', null)->count();

More information in the official documentation:

People are also looking for solutions to the problem: php - Replacing and removing parts of a string


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.