How to use required_unless in laravel - php

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

Related

Is there is any except validation with unique number in laravel?

Can someone help me out to find out how to validate unique emails with some emails that are exceptional.
Here is and example:
we have two emails:
[xxx#xxxx.com, yyyy#yyy.com]
Laravel provide us validation rule:
'email' => 'required|email|unique:users',
so when we register in our web app, these two emails should be ignore while searching for unique user's email in users table.
Thank you in Advance!
I have tried this but it didn't help:
'email' => 'required|email|unique:users,email,[xxx#xxxx.com, yyyy#yyy.com]',
and
'email' => 'required|email|unique:users,email.'[xxx#xxxx.com, yyyy#yyy.com]',
But didn't get any results.
use laravel custom unique rule.You may specify additional query conditions by customizing the query using the where method.
'email' =>[
'required',
'email',
Rule::unique('users')->where(fn($query) => $query->whereNotIn('email', ['xxx#xxxx.com', 'yyyy#yyy.com']))
]

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: Apply validation rule only if the another one passed

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"

Laravel 4 field validation issues on edit

I am using a simple user auth package. It has default validation on user creation, however, it does not have any sort of user edit functionality. No problem, I can write that in. The problem I am having is on update of the username I am failing validation for the following:
The email has already been taken.
The password must be between 4 and 11 characters.
The password confirmation does not match.
When editing, the email is taken because it's the same email. I'm not evenn editing the password.
So my question: How do I turn off validation for updating/editing, or better yet, how do I apply different validation rules for those methods?
You can do that by changing default values, which are provided with package. They can be found on line 50:
public static $rules = array(
'username' => 'required|alpha_dash|unique:users',
'email' => 'required|email|unique:users',
'password' => 'required|between:4,11|confirmed',
'password_confirmation' => 'between:4,11',
);
In a class ConfideUser.
Just replace those rules with you own. In case you dont have some rules e.g. password_confirmation, then delete it. And also adopt your HTML forms and models/db with new validatiorn rules.
I hope it helps.

Categories