laravel - 仅当值不可用时才需要验证
问题描述
我有以下代码:
{!! Form::select('the_selection', ['' => 'Nothing', 'Foo' => 'Bar']) !!}
以及以下内容:
<input class="input" name="the_other_field" type="text"/>
仅当不是the_other_field
时才需要该字段。the_selection
''
所以我尝试了tis验证:
$this->validate($request, [
// 'recurring' => 'required|nullable',
'the_other_field' => 'required_without:the_selection,|date_format:Y-m-d|after:today',
]);
但这不起作用..
解决方案
如果您将验证移至表单请求,则可以添加“有时”验证规则:
class YourRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'the_selection' => 'required',
];
}
public function withValidator($validator)
{
$validator->sometimes('the_other_field', 'required', function ($input) {
return $input->the_selection != '';
});
}
}
推荐阅读
- html - 无法在打印预览中水平对齐图像
- javascript - Geoserver WMS 调用未返回底图
- python - Keras Dense 层的输入未展平
- swift - Pod Init 使用 Xcode 在 Swift 中为 CocoaPods 默认选项设置为 OSX 而不是 IOS?
- fiware-orion - Fiware Global 全球 cosmos 实例如何订阅我的 Orion
- scapy - Scapy - 无法发送带有 VLAN 标记的数据包
- batch-file - 批处理文件控制台不停止显示输出
- python - 在烧瓶应用程序之外继承全局变量
- javascript - Angular 6 嵌套 FormArray
- python - 使用 smtplib 发送电子邮件是否发生了变化?