3

Here is my validation rules:-

$validationCondition    = array(
                            'fname'     => 'required|min:2',
                            'lname'     => 'required|min:2',
                            'email'     => 'required|email|unique:users,email',
                            'isd'       => 'required|unique:users,isd',
                            'mobile'    => 'required',
                            'password'  => 'required'
                        );
$validationMessages     = array(
                            'fname.required'    => 'Please provide first name',
                            'fname.min'         => 'First name should be atleast 3 characters long',

                            'lname.required'    => 'Please provide last name',
                            'lname.min'         => 'Last name should be atleast 3 characters long',

                            'email.required'    => 'Please provide email',
                            'email.email'       => 'Please provide a valid email id',
                            'email.unique'      => 'This email id has already been registered. Choose any other valid email id.',

                            'isd.required'      => 'The city name should be atleast 3 characters long',

                            'mobile.required'   => 'Please provide mobile number',

                            'password.required' => 'Please provide password'
                        );

In the users table, both the isd and mobile are to be checked to ensure whether the contact number is unique.

How can I check that? I know how to check uniqueness of a single field, but don't know how to check combined value as unique.

1

2 Answers 2

11
$validationCondition = array(
    ...
    'isd' => 'required',
    'mobile' => 'required|unique:users,mobile,NULL,id,isd,' . $request->isd,
    ...
);

The meaning of that unique rule for mobile will be: "value of mobile must be unique among all existing users that have isd of the same value that came in request".

$request here is the instance of incoming request that you are validating (obviously).

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

2 Comments

perfect! That was a charm. What does the NULL do?
NULL,id section is responsible for ignore arguments of unique rule. Those are just default values for those arguments since we are not using them, but need to get to where arguments of unique rule. If you specify your model id instead of NULL then unique rule will ignore row that has this id (useful when updating existing row, so that his current values aren't included in uniqueness check). You can read more about it here
1

You can use like below

'mobile'  => 'required|unique:users,mobile,NULL,id,mobile,isd'

Get more details from doc

1 Comment

your link is now dead, think this is the current replacement laravel.com/docs/9.x/validation#rule-unique

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.