Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate that a field must be true if another field is false

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

like image 290
Gazz Avatar asked Dec 14 '18 12:12

Gazz


People also ask

How do you validate a Boolean value?

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.

How do you define a field validation rule?

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.

What is a field validator?

Field validation is an automated process of ascertaining that each field contains the correct value before the form is accepted.

Can we have more than one validator for a single field?

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.


3 Answers

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 :)

like image 195
Sadegh Ameri Avatar answered Oct 07 '22 13:10

Sadegh Ameri


'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

like image 43
Rajeev Radhakrishnan Avatar answered Oct 07 '22 11:10

Rajeev Radhakrishnan


$validator = Validator::make($request->all(), [
                'tax_relief'=>['required','in:true']
            ]);
if ($validator->fails()) {
                return response()->json($validator->errors());
}
like image 4
Ausra Avatar answered Oct 07 '22 11:10

Ausra