I want to unique validation a input field from multiple table. But i don't know how to do this. I already try some way but nothing work. Here is my code.
$validate = Validator::make($request->all(), [
'gender' => 'required|max:5',
'email' => 'required|unique:customers,email|unique:subscribers,email|max:128',
]);
You can use the unique rule twice, like so:
$validate = Validator::make($request->all(), [
'gender' => 'required|max:5',
'email' => 'required|unique:customers|unique:subscribers|max:128',
]);
You also don't have to provide the column name, if you leave it unspecified laravel will assume the field name, which is also email in this case.
Related
I have a table states and fields are id,country_id,state_code,state_name.Now I want to validate that same country doesn't have a same state_code and same state_name in a table.
I tried but it's not working.
In my Controller :
$validator = State::validator($request->all(), $id);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator->getMessageBag())
->withInput($request->all());
}
Here is my validation function in model :
protected function validator(array $data, $id)
{
return Validator::make($data, [
'country_id' => 'required',
'state_code' => 'required',
'state_name' => 'required',
]);
}
How can I solved this without custom validation ?
First of all, your validation rules are only checking if these fields are present in the request. There is no validation happening for a value exists in database. First of all, you might wanna go through the documentation for the rule_exists. Secondly, you may have to update the query for this rule as per the documentation
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::exists('staff')->where(function ($query) {
$query->where('account_id', 1);
}),
],
]);
Here you can pass additional query parameters.
You do not need a custom validation for that one, laravel has a unique column validation
$this->validate($request, [
'country_id' => 'required',
'state_code' => 'required|unique:states,state_name',
'state_name' => 'required|unique:states,state_code',
]);
This way you know for sure that column values in state_code will be unique compare to state_name for more check the docs
laravel 5
In laravel 5 form validation I need email to be only unique for a certain field's id in the db table.
$this->validate($request,[
'name'=>'required',
'email' => 'unique:users,email,',
'password'=>'required',
]);
You can try this way:
$companyId = $request->get('company_id'); // selected company
$this->validate($request,[
'name' => 'required',
'email' => [
'required',
"unique:users,email,NULL,id,company_id,$companyId"
],
'password'=>'required',
]);
If you want to ignore a specific id in the database, you can use ignore. For example, when a user is updating his profile, we want to allow him to send his email address, it should not be considered a not unique.
Here is the docs details. And here is your code updated to match that:
$this->validate($request,[
'name'=>'required',
'email' => [
\Illuminate\Validation\Rule::unique('users', 'email')->ignore($user->id),
],
'password'=>'required',
]);
However, if want to ignore more than one id or want to customize the query, you need to add a where clause:
$this->validate($request,[
'name' => 'required',
'email' => [
'required',
\Illuminate\Validation\Rule::unique('users')->where(function ($query) {
// Add any constraints you want for the query here.
return $query->whereNotIn('id', [1, 2, 3, 4]);
}),
],
'password'=>'required',
]);
Use Third and Fourth param to ignore for company_id
$company_id = 1;// Company will be here
And in validation
'email' => 'unique:users,email,'.$company_id.',company_id',
I have a laravel validation rules array that I have defined like this.
$rules = array(
'name' => 'required|regex:/(^[A-Za-z0-9 ]+$)+/',
'email' => 'required|email',
'mobile' => 'regex:/^\+?\d+$/',
);
$validation = Validator::make($input, $rules);
now depending on another if condition I want to add another validation rule into this array.
address => 'required'
how do I do it? I have tried the using array_push() function with the $rules array, but it doesn't work.
$rules['address'] = 'required';
I have a field called title in table languages.
While creating a new user using register form, user has a option to either chose existing or create a new language. How do I make sure the language created is unique?
users table is different and languages table is different.
my current validation code in create method (Registercontroller)
'firstname' => 'required|max:255',
'lastname' => 'required|max:255',
'email' => ['required','max:255','unique:users','regex:/(#org.uk)$/',],
'password' => 'required|min:6|confirmed',
'title' => 'required',
I want to add something like: unqiue:languages but how do I do that? do I set langauge field to unique?
laravel validation for unique
here is the solution what you needed . In unique validation it check new entry was unique or not what you do is this just simply given the desired table name and field from which you want to check field is unique or not
'firstname' => 'required|max:255',
'lastname' => 'required|max:255',
'email' => ['required','max:255','unique:users','regex:/(#org.uk)$/',],
'password' => 'required|min:6|confirmed',
'title' => 'required|unique:languages,title',
hope this will help you
How can I change the value used in the error feedback, lets say I have this rule:
$rules = array(
'valid_country_code' => 'required',
);
But instead of 'valid_country_code' I want the user to see 'country' in the error message. The message at the moment.
valid_country_code is required.
what I want
country is required.
But I dont want to change the name in the form when posting because I want to bind the form to a model.
You may pass the custom messages as the third argument to the Validator::make method:
$rules = array(
'valid_country_code' => 'required',
);
$messages = [
'valid_country_code.required' => 'country is required.',
];
$validator = Validator::make($input, $rules, $messages);