详情页

Laravel验证规则: required_without不起作用怎么回事

时间:2023年05月07日

编辑:佚名

我有两个字段:Email和Telephone
我想创建一个验证,其中两个字段中的一个是必需的,如果设置了一个或两个字段,它应该是正确的格式。
我试过了,但它不起作用,我需要这两个
 public static array $createValidationRules = [
        'email' => 'required_without:telephone|email:rfc',
        'telephone' => 'required_without:email|numeric|regex:/^\d{5,15}$/',
    ];
如果两个字段都为空,则两个字段都会生成required_without错误消息,这是正确的。此错误消息清楚地指出,如果另一个字段未填充,则必须填充该字段。如果需要,您可以使用change发送消息:
$messages = [
    'email.required_without' => 'foo',
    'telephone.required_without' => 'bar',
];
复制
但是,您必须添加nullable规则,以便在字段为空时不应用格式规则:
$rules = [
    'email' => ['required_without:telephone', 'nullable', 'email:rfc'],
    'telephone' => ['required_without:email', 'nullable', 'numeric', 'regex:/^\d{5,15}$/'],
];
复制
此外:It is recommended将规则编写为数组,特别是在使用regex时。    
相关文章
猜你需要