I want to do "unique" validation on multiple fields. I have written below validation rule but not sure how to include brand_id and company_id in it
$request->validate([
'name' => 'required|string|unique:products',
'company_id' => 'required',
'brand_id' => 'required',
]);
So what I am trying to do is, ADD PRODUCT but check if the name is unique for the given company_id and brand_id. How can I do that?
We can use Rule::unique() function to add custom conditions to unique validation.
$request->validate([
'company_id' => 'required',
'brand_id' => 'required',
'name' => [
'required',
'string',
Rule::unique('products')->where(function($query) {
$query->where('company_id', request('company_id'));
$query->where('brand_id', request('brand_id'));
}),
],
]);
Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute:
$request->validate([
'name' => 'bail|required|string|unique:products',
'company_id' => 'bail|required',
'brand_id' => 'required',
]);
You can add additional wheres after the ignore options (the NULL,id below), e.g.:
$request->validate([
'company_id' => 'required',
'brand_id' => 'required',
'name' => 'required|string|unique:products,name,NULL,id,company_id,'.request('company_id').',brand_id,'.request('brand_id'), '
]);
Related
I am building a Book Portal with Laravel. I have a unique validation rule on name to books.
$request->validate([
'name' => 'required|unique:books',
'about' => 'required',
'dsecription' => 'required',
'image' => 'required|mimes:jpeg,bmp,jpg,png|between:1, 6000',
'author_id' => 'required',
'publisher' => 'required',
'recommended' => 'required',
'epub_url' => 'required',
'year'=> 'required',
'pages' => 'required',
]);
I found out that an Author can have the same book name. Using unique on the name, will not allow me to upload.
I am thinking, if there is a way, I can check the author and the name of the book i.e
Check if the author has the same name, then apply the unique rule on name else upload.
I didn't find any solution for this. That's why I made my own custom validation like this :
$request->validate([
'name' => 'required|unique:books',
'author_id' => 'required',
// ...
]);
$data = Book::where('author_id', $request->author_id)->where('name', $request->name)->first();
if(!empty($data)) {
return redirect()->back()->withErrors('This name already taken'); // error message
}
You can try using the extra where conditions that can be passed to the unique rule to apply the unique constraint under certain conditions:
// we will make sure 'author_id' is an existing value
'author_id' => 'bail|required|integer|exists:authors,id',
'name' => [
'bail',
'required',
Rule::unique('books')
->where('author_id', (int) $request->input('author_id')),
],
Assuming author_id is on the books table.
Laravel 7.x Docs - Validation - Rules - unique
In the given scenario
request()->validate([
'type' => 'required',
'category' => 'required'
]);
and Again
request()->validate([
'name' => 'required',
'gender' => 'required
]);
Is it possible to get some sort of centralized or complied error that encompasses both the validations?
Then you should use Validator facade to handle this kind of cases.
for ex.
$validator = Validator::make($request->only('type', 'category), [
'type' => 'required',
'category' => 'required'
]);
$validator2 = Validator::make($request->only('name', 'gender'), [
'name' => 'required',
'gender' => 'required'
]);
if ($validator->fails() || $validator2->fails()) {
// return merge $validator->errors() and $validator2->errors();
}
I have added 3 fields to the Laravel OOB Registration Form, they are Birth Month, Day, and Year. I pass these fields to the validator function in the RegisterController and convert them to an age with Carbon:
$theAge = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
This part works fine, I can pass the variable to a field in the table and see the correct age.
How do I add $theAge to my Validator?
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users|confirmed',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'state' => 'required|string|max:2',
'zipcode' => 'required|string|max:10',
'brand' => 'required',
'opt_in' => 'required',
'g-recaptcha-response' => 'required|captcha',
'birthmonth' => 'required',
'birthday' => 'required',
'birthyear' => 'required',
]);
I have tried the following but it appears to be ignored on validation:
$theAge => 'bail|min:21'
I have looked into the After Validation Hook but don't understand how to use it in my situation.
you can add the $theAge variable to the data array.
$data['age'] = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
You can put the calculated value back in the $data before you call the validator like this:
$theAge = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
$data['age'] = $theAge;
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users|confirmed',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'state' => 'required|string|max:2',
'zipcode' => 'required|string|max:10',
'brand' => 'required',
'opt_in' => 'required',
'g-recaptcha-response' => 'required|captcha',
'birthmonth' => 'required',
'birthday' => 'required',
'birthyear' => 'required',
'age' => 'min:21'
]);
Alternatively, you can let the user select their date of birth using date picker (e.g. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date) like this:
<input name="birthday" type="date">
and in your validator, do this define the age check plus a custom error message:
return Validator::make($data, [
// ... snipped
'birthday' => 'required|date|before_or_equal:' . Carbon::now()->subYears(21)->toDateString()
], [
'birthday.before_or_equal' => 'You must be at least 21 years old.'
]);
I am running some basic validation inside a Laravel 5.5 controller like this...
$this->validate($request, [
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'required',
]);
Is there a way to check if 'mykey' matches a php string I have saved? I know I can do an if statement and compare them but wondered if there was a way I could do this inside the validation itself?
You can use in rule, This works for n number of values
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => [
Rule::in([env('MY_KEY'),config('app.another_key')]),
]
]);
Laravel provides a regex option for validation. Depending on the complexity of the string comparison it may be useful:
https://laravel.com/docs/5.5/validation#rule-regex
You can this rule:
$key = "my_saved_key"
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'in:' . $key,
]
]);
I use soft deletes which I then ignore/exlude in the validation rules in Laravel 5.3.
How do I enforce two ignores in a validation array for updates?
'number' => "required|unique:customers,number,NULL,id,company_id,
$company_id},deleted_at,NULL",
I need to add the exclusion of the current record as well.
I found the answer myself. For your information(field = number) :
$validator = Validator::make($request->all(),
[
'number' => "required|unique:customers,number,{$id},id,company_id,{$company_id},deleted_at,NULL",
'name' => 'required',
'street' => 'required',
'streetnumber' => 'required',
'zipcode' => 'required',
'city' => 'required',
]);