Eli*_*ias 4 php validation laravel
我有两个字段:Email和Telephone
我想创建一个验证,其中需要两个字段之一,并且如果设置了一个或两个字段,则它应该是正确的格式。
我尝试了这个,但它不起作用,但我需要两者
public static array $createValidationRules = [
'email' => 'required_without:telephone|email:rfc',
'telephone' => 'required_without:email|numeric|regex:/^\d{5,15}$/',
];
Run Code Online (Sandbox Code Playgroud)
hal*_*oei 11
如果两个字段都为空,则生成错误消息是正确的required_without。此错误消息清楚地表明,如果其他字段未填写,则必须填写该字段。如果需要,您可以更改消息:
$messages = [
'email.required_without' => 'foo',
'telephone.required_without' => 'bar',
];
Run Code Online (Sandbox Code Playgroud)
但是,您必须添加nullable规则,因此当字段为空时格式规则不适用:
$rules = [
'email' => ['required_without:telephone', 'nullable', 'email:rfc'],
'telephone' => ['required_without:email', 'nullable', 'numeric', 'regex:/^\d{5,15}$/'],
];
Run Code Online (Sandbox Code Playgroud)
此外:建议将规则编写为数组,尤其是在使用regex.
| 归档时间: |
|
| 查看次数: |
9221 次 |
| 最近记录: |