我正在尝试在不需要用户完整地址的表单上输入验证,除非填写了其中一个地址字段。
示例:如果我填写州或城市输入字段,我将被要求同时填写邮政编码和街道地址。
我尝试使用 Laravel 的,required_with
但它们似乎都没有工作。有小费吗?required_with_all
required_if
我正在尝试在不需要用户完整地址的表单上输入验证,除非填写了其中一个地址字段。
示例:如果我填写州或城市输入字段,我将被要求同时填写邮政编码和街道地址。
我尝试使用 Laravel 的,required_with
但它们似乎都没有工作。有小费吗?required_with_all
required_if
我遇到了同样的问题,但现在我想出了如何解决我的问题,这是错误的代码:
$rules = [
...
/** address optional */
'address-id' => 'exists:addresses, id',
'address-complement' => '',
/** address required */
'address-state-id' => 'required_with:address-id, address-city-id, address-postal_code, address-street, address-number, address-complement, address-district',
'address-city-id' => 'required_with:address-id, address-state-id, address-postal_code, address-street, address-number, address-complement, address-district',
'address-postal_code' => 'required_with:address-id, address-city-id, address-state-id, address-street, address-number, address-complement, address-district',
'address-street' => 'required_with:address-id, address-city-id, address-postal_code, address-state-id, address-number, address-complement, address-district',
'address-number' => 'required_with:address-id, address-city-id, address-postal_code, address-street, address-state-id, address-complement, address-district',
'address-district' => 'required_with:address-id, address-city-id, address-postal_code, address-street, address-number, address-complement, address-state-id',
...
];
这是正确的:
$rules = [
...
/** address optional */
'address-id' => 'exists:addresses,id',
'address-complement' => '',
/** address required */
'address-state-id' => 'required_with:address-id,address-city-id,address-postal_code,address-street,address-number,address-complement,address-district',
'address-city-id' => 'required_with:address-id,address-state-id,address-postal_code,address-street,address-number,address-complement,address-district',
'address-postal_code' => 'required_with:address-id,address-city-id,address-state-id,address-street,address-number,address-complement,address-district',
'address-street' => 'required_with:address-id,address-city-id,address-postal_code,address-state-id,address-number,address-complement,address-district',
'address-number' => 'required_with:address-id,address-city-id,address-postal_code,address-street,address-state-id,address-complement,address-district',
'address-district' => 'required_with:address-id,address-city-id,address-postal_code,address-street,address-number,address-complement,address-state-id',
...
];
我必须删除逗号后的空格才能使其正常工作。