0
votes

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

3 Answers

2
votes

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

0
votes

$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

0
votes

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.