Laravel: Apply validation rule only if the another one passed - php

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"

Related

Form validation in Asgard CMS

I am working on a custom module in AsgardCMS and have discovered the Laravel form validation I know and love does not work. Instead, if I submit the form in violation of the rules, it does not throw an error, it just accepts it and carries on.
Is there some 3rd party validation at work that I'm not seeing?
Even just a link to some documentation would be much appreciated...docs for Asgard seem scarce.
$request->validate([
'name' => 'required|max:60',
'location_id' => 'required|integer',
'comment' => 'required|min:10|max:500'
];
Figured it out after some digging. In Modules/Yourmodule/Http/Requests folder you will find Create###Request.php and Update###Request.php that contain a rules() and translationRules() method where you can apply your validation rules.

Laravel Unique Form Validation

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?

Laravel Request Validation Regex without Required

I have a problem with Request validation. Everything works but one pattern. And its if I dont want the field to be required but once it is filled I want it to match a regex.
But it throws regex error when I leave the field empty.
Any tips on how I should handle it?
BTW: I made a custom Request class where I take care of the validation so if the solution could be also in the Request and not directly in Controller that would be great.
return [
'dic' => 'max:12|regex:/^[a-zA-Z]{2}[0-9]{8}[0-9]*$/',
];
return [
'dic' => 'nullable|max:12|regex:/^[a-zA-Z]{2}[0-9]{8}[0-9]*$/',
];
nullable won't check the other rules when the field is empty.

Laravel api route validation not validating

I did move from Lumen to Laravel and now converting my project over. Everything is working except the validation. For some reason, if I try to validate, it just redirects to the welcome.blade.php view. What could cause this?
I am using only the API part of routes, not the view. I am not dealing with views. I am using the stateless part of Laravel.
According to documentation, I can validate like this:
$this->validate($request, [
'title' => 'required|unique|max:255',
'body' => 'required',
]);
If validation passes, your code will keep executing normally. However,
if validation fails, an
Illuminate\Contracts\Validation\ValidationException will be thrown.
I also tried to force it to return JSON response without success:
$validator = $this->validate($request, ['email' => 'required']);
if ($validator->fails()) {
$messages = $validator->errors();
return new JsonResponse(['status' => 'error', 'messages' => $messages]);
}
However, mine doesn't fail but just returns the welcome view with response code of 200. I have tried pretty much all the possible validation methods from the documentation and from google. Non of them are working.
I even tried with clean laravel install, declared one test route and test controller which had the validation and the result is the exact same.
Is the validation even meant to be compatible with the restful/stateless part of Laravel?
Any suggestion is much appreciated.
1- first the unique key needs a table, per example if you want the email to be unique in the users table you do as follows:
'email' => 'required|unique:users',
I think may be you have placed your route in route/web.php file. Replace that code from web.php to api.php
Try to place your API endpoints in route/api.php file.
And remember you need to add prefix /api in your route.
Ex : test.com/api/users.

How to use required_unless in laravel

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

Categories