'Laravel validation rule based on other fields value

I have this radio button

<input class="form-check-input" type="radio" name="discount" value="Yes">
<input class="form-check-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',
]);                           

                                         


Solution 1:[1]

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'
             ]);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1