Home > database >  Laravel validation rule based on other fields value
Laravel validation rule based on other fields value

Time:03-29

I have this radio button

<input  type="radio" name="discount" value="Yes">
<input  type="radio" name="discount" value="No" checked>

and this hidden field

 <input type="hidden" name="discount_valid" value="true">

by default this hidden field is true

Now when i'm trying to validate if discount_valid is true then it should submit the form this code is working but i want to add another condition, if discount is No then it should submit the form irrespective of whether discount value is true or false. If discount is Yes and discount_valid is false then form should not submit.

$validator = Validator::make($request->all(), [
            'discount_valid'=>'in:true',
]);                           

                                         

CodePudding user response:

By "submit the form", I have to assume you mean "processing the form request".

I've constructed a validation rule from your specifications:

if discount_valid is true then it should submit the form

if discount is No then it should submit the form irrespective of whether discount value is true or false

If discount is Yes and discount_valid is false then form should not submit.

$validator = Validator::make($request->all(), [
                'discount_valid'=>'exclude_if:discount,No|in:true',
                'discount'=>'in:No'
             ]);
  • Related