I have the following rule:
[
'address.name' => 'required_with:address.street,address.postcode|string|nullable|max:255',
'address.street' => 'required_with:address.name,address.postcode|string|nullable|max:255',
'address.postcode' => 'required_with:address.name,address.street|string|nullable|max:255',
];
But it seems required_with rules cannot handle dot notation. When only address.postcode is not present or empty, it won't give errors.
I also tried to not use array, but it is still not working:
[
'address_name' => 'required_with:address_street,address_postcode|string|nullable|max:255',
'address_street' => 'required_with:address_name,address_postcode|string|nullable|max:255',
'address_postcode' => 'required_with:address_name,address_street|string|nullable|max:255',
]
Figure out the reason is nullable cannot work with required_with rule.
What I want to is: all there fields can be empty; but if one is not empty, all mustn't be empty.
However, in Laravel 5.4 , string and integer, email rules already brings not null limitation. So if I use string, I must use nullable too.
I really miss the age of Laravel 5.2, when all fields can be nullable by default unless specify required or filled.
Hello, we've fixed this issue in https://github.com/laravel/framework/commit/d8ba1109047897072d25ba258b9b3813404f808b
After the next release required_* rules will fail if the value is null even if you use nullable, so the following should work fine:
[
'address_name' => 'required_with:address_street,address_postcode|string|nullable|max:255',
'address_street' => 'required_with:address_name,address_postcode|string|nullable|max:255',
'address_postcode' => 'required_with:address_name,address_street|string|nullable|max:255',
]
Thanks! That's great!
Most helpful comment
Hello, we've fixed this issue in https://github.com/laravel/framework/commit/d8ba1109047897072d25ba258b9b3813404f808b
After the next release
required_*rules will fail if the value isnulleven if you usenullable, so the following should work fine: