I have two fields that are optional only if both aren't present:
$rules = [
'initial_page' => 'required_with:end_page|integer|min:1|digits_between: 1,5',
'end_page' => 'required_with:initial_page|integer|min:2|digits_between:1,5'
];
Now, end_page
needs to be greater than initial_page
. How include this filter?
Why not just define
$min_number = $min + 1
number and use validatormin:$min_number
, example:And you can then return custom error message to explain the error to user.
For Laravel 5.4 it will be:
There is no built-in validation that would let you compare field values like that in Laravel, so you'll need to implement a custom validator, that will let you reuse validation where needed. Luckily, Laravel makes writing custom validator really easy.
Start with defining new validator in yor AppServiceProvider:
Now you can use your brand new validation rule in your $rules:
You'll find more info about creating custom validators here: http://laravel.com/docs/5.1/validation#custom-validation-rules. They are easy to define and can then be used everywhere you validate your data.
As of Laravel 5.6
gt
,gte
,lt
andlte
rules are added.I think you can try something like this,