Framework: required_with rule cannot work with nullable

Created on 17 Mar 2017  路  4Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.0
  • PHP Version: 5.6.30

Description:

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.

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 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',
]

All 4 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felixsanz picture felixsanz  路  3Comments

YannPl picture YannPl  路  3Comments

progmars picture progmars  路  3Comments

ghost picture ghost  路  3Comments

gabriellimo picture gabriellimo  路  3Comments