我的问题是如何使用呢逻辑required_with
和required_with_all
对Laravel验证的作品?
我正在申请3个不同领域
现在让我给你我的榜样,让我得到想要的
'start' => 'nullable|required_with:end',
'end' => 'nullable|required_with:start',
'repeat' => 'nullable|required_with_all:start,end',
如果我只提交仅重复字段,则不会在Laravel中执行验证。
您可以从我从中复制的代码中删除nullable is,但如果仅提供重复字段,则不会执行任何验证。
OP在这里真正想要的是:
use Illuminate\Validation\Validator;
class MyFormRequest extends FormRequest
...
public function rules()
{
return [
'start' => 'required_with:end|required_without:repeat',
'end' => 'required_with:start|required_without:repeat',
'repeat' => 'required_without_all:start,end',
];
}
public function withValidator(Validator $validator)
{
$validator->after(function ($validator) {
if ((!$this->input('start') || !$this->input('end')) && $this->input('repeat')) {
$validator->errors()->add('repeat', 'The repeat field is required when start and end are present.');
}
});
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句