We recently upgaded Laravel from 5.5 to 5.6 I have validation rules:
return [
'min_price' => ['numeric', 'nullable', 'min:0'],
'max_price' => ['numeric', 'nullable', 'min:0', 'gt:min_price'],
]
It throws an error in case if
- min_price = null, max_price = 100
- min_price = 0, max_price = 99.99
- min_price = 12.50, max_price = 100
- min_price = 12.50, max_price = null It says:
ERROR: The values under comparison must be of the same type "exception":"[object] (InvalidArgumentException(code: 0): The values under comparison must be of the same type at vendor/laravel/framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php:1659)
[stacktrace]
#0 vendor/laravel/framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php(849): Illuminate\\Validation\\Validator->requireSameType(12.50, 100)
It says, that both fields should have the same type, so it can't compare integer and float and can't ignore nullable fields.
The issue is with methods validateGt
, validateLt
, validateGte
, validateLte
in trait ValidatesAttributes
. Is there some how I can extand or override that trait?
Since there is no obvious solution, I decided to create my own validator and don't use the one that Laravel offers:
And used it in validations rules like this