0

I am trying to validate an email address in my Customer table. There is a column in the Customer table called Brand where effectively the same email address could be registered to multiple brands for example:

 Email               Brand
 [email protected]    firstsite.com
 [email protected]    secondsite.com

When validating the email address I need to check the email is unique in the Customer table for the current Brand. Currently my validation rule looks like this

 $rules = array('email' => 'required|email|unique:Customer,Email');

however Brand must appear in there someone to say the email must be unique to the customer for the current brand

Any help much appreciated

3 Answers 3

3

You can use the column and value parameter of the unique validation rule to add a condition to the rule.

'email' => 'required|email|unique:Customer,Email,null,null,column,value'

The problem is you need your Brand input in order to create your rule like this :

$rules['email'] = 'required|email|unique:Customer,Email,null,null,Brand,' . Input::get('Brand');

This rule will check if the email is unique where the brand has the given value.

More information here : http://laravel.com/docs/validation#rule-unique

There is also a package that could help you : https://github.com/felixkiss/uniquewith-validator

Sign up to request clarification or add additional context in comments.

3 Comments

Good. Just would be helpful to also explain why and how, and what this null,null part means.
My bad, there is actualy only one null, this parameter is to ignore a given id, so you can set to null if you don't need it.
Not really, your code was right before. You need null, null, now after your edit it's incorrect. I was just saying that you should add that explanation to you answer so it has more valuable info.
0

$rules = array('email' => 'required|email|unique:Customer,Email'); this will query your model for finding the entered email if the email is already present then an error message is raised. so this will not work with your app design flow. you have to run sql query to find out if the email is present with your current brand if yes return error message if not save the data. something like that

Comments

0

I would go for writing my own custom validation rule. Custom validation rule will be more common solution and you can name it as it suites you best. It can be created using the Validator::extend() and a closure, like this :

Validator::extend('awesome', function($field, $value, $params)
{
    return $value == 'awesome';
});

or via validator class. Have a research at this topic, maybe here (scroll to custom rules section) or any other source you find.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.