Php 如果字段Y==”则Laravel验证字段X;“什么?”;
我在Laravel有一个表单,我需要帮助验证字段。 表单有3个文本字段和1个下拉按钮。 现在,只有当下拉列表设置为某个值时,才需要3个文本字段中的2个 以下是我的3个文本字段和下拉按钮代码:Php 如果字段Y==”则Laravel验证字段X;“什么?”;,php,laravel,validation,Php,Laravel,Validation,我在Laravel有一个表单,我需要帮助验证字段。 表单有3个文本字段和1个下拉按钮。 现在,只有当下拉列表设置为某个值时,才需要3个文本字段中的2个 以下是我的3个文本字段和下拉按钮代码: <textarea class="form-control w-100 flex-fill instructions" id="instructions-1" name="instructions[]" rows="4"
<textarea class="form-control w-100 flex-fill instructions" id="instructions-1" name="instructions[]" rows="4" placeholder="Instructions"></textarea>
<button class="btn btn-secondary btn-md bg-white dropdown-toggle" type="button" data-toggle="dropdown" name="button[]" id="button-1" aria-haspopup="true" aria-expanded="false">Model</button>
<div class="dropdown-menu" aria-labelledby="button-1"><a class="dropdown-item" id="dropdown-item-model-1" href="#">Model</a><a class="dropdown-item" id="dropdown-item-engenius-1" href="#">Engenius</a>
<input type="text" class="text-body px-2 py-1 text" name="model[]" id="model-1" value="" placeholder="Enter Model" />
<input type="text" class="text-body px-2 py-1 text" name="pincode[]" id="pincode-1" value="" placeholder="Enter Pincode" />
仅当下拉按钮具有值模型时,我才需要验证模型和pincode文本字段。
请帮忙。我能解决这个问题。 我试图使用输入类型按钮的值进行验证,但表单post不会发送按钮的值。 因此,我添加了一个隐藏的文本字段,可以保存下拉列表的值。 这样,我就可以检查下拉列表是否为Model,然后才需要Model和pincode文本字段 下面是我的更新HTML代码:
$validated = $request->validate([
'button.*' => 'required',
'instructions.*' => 'required|min:5',
'model.*' => 'required_if:button.*,Model|min:5',
'pincode.*' => 'required_if:button.*,Model|min:5',
]);
textarea class="form-control w-100 flex-fill instructions" id="instructions-1" name="instructions[]" rows="4" placeholder="Instructions"></textarea>
<button class="btn btn-secondary btn-md bg-white dropdown-toggle" type="button" data-toggle="dropdown" name="button[]" id="button-1" aria-haspopup="true" aria-expanded="false">Model</button>
<input type="hidden" name="job_type[]" id="job_type-1" value="Model" />
<div class="dropdown-menu" aria-labelledby="button-1"><a class="dropdown-item" id="dropdown-item-model-1" href="#">Model</a><a class="dropdown-item" id="dropdown-item-engenius-1" href="#">Engenius</a>
<input type="text" class="text-body px-2 py-1 text" name="model[]" id="model-1" value="" placeholder="Enter Model" />
<input type="text" class="text-body px-2 py-1 text" name="pincode[]" id="pincode-1" value="" placeholder="Enter Pincode" />
$validated = $request->validate([
'job_type.*' => 'required',
'instructions.*' => 'required|min:5',
'model.*' => 'required_if:job_type.*,Model',
'pincode.*' => 'required_if:job_type.*,Model',
]);