I'm having a weird problem, using Laravel 5.3
Here's a small snippet of my validation
$validator = Validator::make($request->all(), [
'item_rows.*.imei' => 'unique:t_inventory_imei,imei_barcode,NULL,id,status,'."A",
'item_rows.*.quantity' => 'required|numeric|not_in:0',
'item_rows.*.cost_price' => 'required|numeric|not_in:0',
'item_rows.*.id_inventory' => 'required|numeric|not_in:0',
'item_rows.*.id_branch' => 'required|numeric|not_in:0',
'item_rows.*.id_branch_location' => 'required|numeric|not_in:0',
'ref_document_no' => 'required',
'ref_doc_date' => 'required|date',
]);
Problem comes when unique:t_inventory_imei,imei_barcode,NULL,id,status,."A" this rule is available.
This rule doesn't seem to be validated, it will effect the last rule to be validation error, say returning
{"message":"validation_fail","validation_error":{"currentRule":"date"}}
If I remove the last row, it will cascade up and so on
{"message":"validation_fail","validation_error":{"currentRule":"required"}}
If I remove every rule leaving only the first unique rule, the validation works, it is able to detect unique row with rule status A.
Is this a bug from Laravel or is there something wrong with the code?
Related
I want to set require validation for array of objects in laravel and used following rule:
[
'translations.*.languageId' => ['required', 'numeric', Rule::in(Language::all()->pluck('id'))],
'translations.*.data.title' => 'required|string',
]
but there is problem when i send request without translations key the validate function does not throw require error for translation key.
so i add translations key separately too.
[
'translations' => ['required', 'array'],
'translations.*.languageId' => ['required', 'numeric', Rule::in(Language::all()->pluck('id'))],
'translations.*.data.title' => 'required|string',
]
But there is a problem if an extra key is sent that should not be in the translations array (like locale), it can still be seen in the output of the validate function.
how can i prevent this unexpected result?
As taylorotwell answered my issue and close it there is no way to validator do this filter.
Filter the array to only have the items you actually want after validation.
I have this simple code:
$this->validate($request, [
'email' => 'required|email|unique:subscriptions'
]);
However I can keep putting in the same email and it doesn't send me back and error and keeps creating the row in the database.
However if I change the code to ..
$this->validate($request, [
'email' => 'required|email|unique:users'
]);
It sends me back an error properly and displays in the div I have setup for it.
I have tried many different ways to fix this, but I feel like it should be easier than this. I looked at everything and it doesn't seem to address this issue. Any help would be great.
You need to specify the column name
$this->validate($request, [
'email' => 'required|email|unique:subscriptions,email'
]);
$this->validate($request, [
'email' => 'required|email|unique:users,email'
]);
While updating you need to force a unique rule to Ignore Given ID
'email' => 'unique:subscriptions,email,'.$user->id
https://laravel.com/docs/5.2/validation#rule-unique
I am trying to submit a form and validate the content.
In one of the requests I need to make a special rule.
I followed the documentation and it says to use unique and declare a Rule.
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
I am trying with the example from the documentation, but all I get it this error:
Class 'Illuminate\Validation\Rule' not found
I declared the line
use Illuminate\Validation\Rule;
In my controller, but the error is still there.
The Rule class in the example you posted is for validate an unique field. For examplo if you have an email you will want to be unique in the table, when you are going to edit the record, at saving, will get a validator error because you are saving the same email it is already in the db.
The example you posted:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
is related to this case, editing a record and validating the email (unique in table users). This example avoid to validate the email against the same user-
For using it you have to add the class (not included in the laravel installator). Here you have it.
In your question you say about using the unique rule. The unique rule is for fields that has to be unique in a table, an email, an personal identification (law), and more. Verify if the unique rule is what you need.
You dont have to use the Rule class for this.
Simply achieve the same with following rule:
'email' => 'required|unique:users,email,' . $user->id
The Illuminate\Validation\Rule class has been added on Laravel 5.3+.
I almot sure you have tested with an old laravel version.
See issue (PR) 15809: [5.3] Object based unique and exists rules for validation.
I had that problem on version v5.3. I just updated to the latest patch version (v5.3.31 in my case) and all worked correctly!
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
You have done everything ok so far until you add the line
"Rule::unique('users')->ignore($user->id)"
The only reason why this is not working is because you are not specifying the column name you want to be unique. Let's assume that, in your database, the column name of the table users that you want to be unique is "email". Then, your code should be as follows:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users', 'email')->ignore($user->id),
],
]);
Thus,
//Right Way
"Rule::unique('users', 'email')->ignore($user->id)"
//Wrong Way
"Rule::unique('users')->ignore($user->id)"
Why don't you try in you method something like this:
$validator = Validator::make($request->all(), [
'email' => 'required|unique:<table_name>',
]);
if ($validator->fails()) {
...
}
If you column if also named email laravel will know automaticly, else do
required|unique:<table_name>,<column_name>
Here is a link to the documentation validation section: https://laravel.com/docs/5.3/validation
I have gone through the validations in Laravel. I have taken so many validation rules from Laravel Validations Rules
I want to user required_unless rule for following conditions.
$rules = array(
'facebookID' => 'alpha_num',
'googleID' => 'alpha_num',
'email' => 'required_unless:facebookID,null|required_unless:facebookID,null|email|max:32',
'password' => 'required_unless:facebookID,""|required_unless:googleID,""|max:20',
);
I want to add validation rule of email and password is only required if signup with facebook or google is not attempted.
I believe we can use required_unless or required_if
Please help me to solve this problem.
The laravel validation rule only accept value, that's mean required_unless:facebookID,null is evaluated as: the field is required unless facebookID='null'. So it's not a case you would need.
My suggest solution is using require_without_all:
'email' => 'required_without_all:facebookID,googleID',
'password' => 'required_without_all:facebookID,googleID'
The reference link for you: https://laravel.com/docs/5.1/validation#rule-required-without-all
Regards
In my Laravel 5.2 app, I have the following validation rules to check a field:
$rules = array(
'file' => 'mimes:jpg,png,pdf,doc,docx',
'file' => 'validate_file'
);
Now, the validate_file is a custom validation rule. I want to run it only only if the first mimes validation passes.
I know I could simply validate/redirect with errors - in two batches, first the mimes, then the validate_file, but it seems like an overkill.
Any way I can do it the smart way in Laravel 5.2?
Use "bail" attribute. It will stop validation after first failure.
$this->validate($request, [
'title' => 'bail|mimes:jpg,png,pdf,doc,docx|validate_file',
]);
https://laravel.com/docs/5.2/validation#validation-quickstart search for "bail"