Using a validation rule of type: 'field1' => 'required_with:field2' fails even if field2 is present but empty. Is this the expected behavior? I'm quite sure this did not happen in 5.2
I've tried with this rules in my CustomRequest file:
public function rules() {
$rules = [
'volume_min' => 'required_with:volume_max|integer|min:1',
'volume_max' => 'required_with:volume_min|integer|greater_than_field:volume_min'
];
return rules;
}
but I receive the error volume_min must be an integer, volume_min must be at least 1 and volume_max must be an integer, volume_max must be greater than volume_min, even if both fields are POSTed (actually with _method: PUT) empty.
I've currently fixed using a method based on https://github.com/laravel/framework/issues/17032#issuecomment-270667201 but I'm not sure if this is the desired behavior of the validator.
This is my fix:
public function rules() {
$rules = [
'volume_min' => 'integer|min:1',
'volume_max' => 'integer|greater_than_field:volume_min'
];
$rules["volume_min"] .= $this->request->get("volume_max") ? "|required_with:volume_max" : "|nullable";
$rules["volume_max"] .= $this->request->get("volume_min") ? "|required_with:volume_min" : "|nullable";
return rules;
}
Please provide the data that fails to pass validation.
It's an ajax call in which I pass many fields, these are the ones of interest:
_method=PUT&[...]&volume_min=&volume_max=&other_var=[...]&_token=VsGtVG9OZNr1tb3bsloF2BcmDQJYHdt4SCA4p4Mq
The problem is that you assume the rest of the rules won't be applied if the field is empty but that's not true, the condition only falls on required_with, so for example:
"someFields" => "required_with:anotherField|min:1"
If anotherField is empty the required check will be skipped, but the min check will run regularly, you need to use the sometimes() to conditionally add rules based on the conditions you want.
$validator->sometimes('volume_min', 'integer', function(){ return request('volume_max') })
this will cause the integer check to run only if volume_max is not empty.
This is not an issue with the framework so i'll close the issue, but please feel free to ping me for further clarification.
Ok, I see, and will look into it later to make it work with my app. But, as you said:
If anotherField is empty the required check will be skipped, but the min check will run regularly
isn't this behaviour in contradiction with this merged pull request? https://github.com/laravel/framework/pull/15089
It'll skip validation if a required rule fails, not skipped. This is a confusing point yes, might worth mentioning in the docs, will take care of that :)
Has this issue been updated in the documentation?
I'm seeing this same issue using required_without_all, is there a fix?
'gnar'=>'required_without_all,foo,bar|min:1between:0.01,99999999.99',
foo and bar are both set, gnar is null, but gnar is failing as "gnar min must be at least 1"
This behavior might have good technical reasons, but it is IMHO counter-intuitive. Just spent at least an hour to debug, find this issue and workaround this issue.
@stevevg's example reads:
// if volume_min is given, then this field is required. It should be an integer, greater than volume_min
'volume_max' => 'required_with:volume_min|integer|greater_than_field:volume_min'
It does not read
// if volume_min is given, then this field is required.
// It is nullable. And even if it's null then it should be an integer, greater than volume_min
'volume_max' => 'required_with:volume_min|integer|greater_than_field:volume_min'
Hm.. thinking about it..
Would it be possible to specify it like (adding nullable) this to get the intended behavior?
'volume_max' => 'required_with:volume_min|nullable|integer|greater_than_field:volume_min'
Any updates?
Most helpful comment
This behavior might have good technical reasons, but it is IMHO counter-intuitive. Just spent at least an hour to debug, find this issue and workaround this issue.
@stevevg's example reads:
It does not read
Hm.. thinking about it..
Would it be possible to specify it like (adding
nullable) this to get the intended behavior?