Laravel 基于其他字段值的验证规则

Laravel validation rule based on other fields value

我有这个单选按钮

<input class="form-check-input" type="radio" name="discount" value="Yes">
<input class="form-check-input" type="radio" name="discount" value="No" checked>

和这个隐藏字段

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

默认情况下此隐藏字段为真

现在,当我尝试验证 discount_valid 是否为 true 时,它应该提交此代码有效的表单,但我想添加另一个条件,如果 discountNo 那么无论 discount valuetrue 还是 false 都应该提交表单。如果折扣为 Yesdiscount_valid 为假,则不应提交表单。

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

                                         

“提交表单”,我必须假设你的意思是“处理表单请求”。

我已经根据您的规范构建了验证规则:

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