Below is a snippet of my validation currently:
'independent_financial_advisor' => 'required|boolean',
'understand_objective' => 'required|boolean',
'confirm_objective' => 'required|boolean',
'understand_term_held' => 'required|boolean',
'tax_relief' => 'required|boolean',
I need to validate that when independent_financial_advisor
is false, the remaining 4 fields below must be true. I could not find any Laravel rule which could do this so I thought about using a closure to create a custom rule.
The issue with this is that I don't know how to reference another field in a closure to check its value.
What is the best way to go about this? Thanks
parseBoolean(String s) − This method accepts a String variable and returns boolean. If the given string value is "true" (irrespective of its case) this method returns true else, if it is null or, false or, any other value it returns false.
Field validation rules Use a field validation rule to check the value that you enter in a field when you leave the field. For example, suppose you have a Date field, and you enter >=#01/01/2010# in the Validation Rule property of that field. Your rule now requires users to enter dates on or after January 1, 2010.
Field validation is an automated process of ascertaining that each field contains the correct value before the form is accepted.
Yes, you can have multiple validators , but in separate < f:validator> tags. Order of execution is as they are listed on page and execution of each one is not dependent upon the other.
I added a custom validation rule called true_if_reference_is_false
and passed a parameter to it which is independent_financial_advisor
.
So the validation looks like this:
$this->validate($request, [
'independent_financial_advisor' => 'required|boolean',
'understand_objective' => 'required|boolean|true_if_reference_is_false:independent_financial_advisor',
'confirm_objective' => 'required|boolean|true_if_reference_is_false:independent_financial_advisor',
'understand_term_held' => 'required|boolean|true_if_reference_is_false:independent_financial_advisor',
'tax_relief' => 'required|boolean|true_if_reference_is_false:independent_financial_advisor'
]);
You need to define this validation rule in App\Providers\AppServiceProvider.php
Import Facade Validator.
use Illuminate\Support\Facades\Validator;
And define the rule in the boot
method:
Validator::extend('true_if_reference_is_false', function ($key, $value, $parameters, $validator) {
$request = request();
$keyReference = $parameters[0];
if ($request->has($parameters[0]) && $request->$keyReference == false)
return (bool)$request->$key;
else
return true;
});
Hope this helps :)
'independent_financial_advisor' => 'required|boolean',
'understand_objective' => 'boolean|required_if:independent_financial_advisor,0,false',
'confirm_objective' => 'boolean|required_if:independent_financial_advisor,0,false',
'understand_term_held' => 'boolean|required_if:independent_financial_advisor,0,false',
'tax_relief' => 'boolean|required_if:independent_financial_advisor,0,false',
For more validation rules, check laravel documentation here https://laravel.com/docs/5.7/validation#rule-required-if
$validator = Validator::make($request->all(), [
'tax_relief'=>['required','in:true']
]);
if ($validator->fails()) {
return response()->json($validator->errors());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With