Laravel 5 : Validate a field from 2 tables with different message

2k views Asked by At

I have a table 'user_temps' which contains the new user until they confirmed their subscription. Once done, i copy the user to the 'users' table.

In the registration form i have this validation params :

'email' => 'required|email|max:255|unique:user_temps|unique:users',

The problem is there I need to have 2 different messages : if the user is in the 'user_temps' table and another one if the user is in the 'users' table.

Thank you

3

There are 3 answers

3
AudioBubble On

To simplify things, could you not do the following:

In your user's table, have a boolean column named 'subscribed' defaulted to 0. Then when they've confirmed their subscription you could set it to 1?

Then, upon authentication, you can run this check:

if (Auth::attempt(['email' => $email, 'password' => $password, 'subscription' => 1]))
{
    // The user is active, subscribed and exists.
}
1
Imtiaz Pabel On

you can set custom validation message

$rules = array(
        'email' => 'required|email|max:255|unique:user_temps|unique:users'
    );
    $messages = array(
        'email.required' => 'Email is required',
        'email.email' => 'Email need to be email type',
        'email.unique:user_temps' => 'This email is already exists in user temps table',            
        'email.unique:users' => 'This email is already exists in users table',           
    );
    $validator = Validator::make(Input::all(), $rules, $messages);
0
Lolo On

After trying some bad solutions someone (tank you pmall) gave me the answer in another place :

I defined my own validator for the temporary user

public function validateTempUserEmail($attribute, $value, $parameters)
{
    return UserTemp::where($attribute, $value)->count() == 0;
}

And now the rule for validate the email is

'email' => 'required|email|max:255|TempUserEmail|unique:users',