framework: Failed form request is allowed to proceed if required is not the first rule
- Laravel Version: 5.7.19
- PHP Version: 7.1.4
Description:
I had noted that a few records managed to reach the DB which should not have passed validation. In my form request I have the following withValidator function.
public function withValidator($validator)
{
//All rules have passed, convert our spatial reference to multiple formats and merge to the request for persisting.
if (!$validator->fails()) {
$this->merge(calculateSpatialReferences($this->spatial_reference));
}
}
However I noted that in very rare instances calculateSpatialReferences() was never run but the request still proceeded to the persistence layer in my model which should not have been possible.
After much investigation I believe I have found the issue. One of my validation rules was configured as follows: date_format:H:i|required
For this particular request, the parameter in question was not submitted with the request.
When run using this rule, I can see the required failure from $validator->errors() but the request proceeds to the persistence layer even though it should fail based on the presence of the failed rule.
But if you change the rule to required|date_format:H:i as would be the normal case, the request fails correctly and does not proceed.
This I presume is definitely not intended functionality since it allows a FormRequest to proceed even though it has failed validation.
I appreciate any input you might have on it. Thanks.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 22 (13 by maintainers)
This is a fundamental issue:
I tested the PR with my production app this morning and its working perfect. Thanks guys for the assistance. All the best!!
@staudenmeir you’re doing a
get
request there. Form request validation doesn’t works with GET requests because no form data is passed.