I am using Laravel's form request to validate some complex input. If there is an error, Laravel returns the user to the previous page (which is fine), but I also want it to log the errors for me. How do I do that?
I attempted to use the form's withValidator
method as follows, but got an error re 'maximum nesting level reached'.
public function withValidator($validator)
{
$validator->after(function ($validator) {
if ($validator->fails()) {
foreach($validator->errors()->all() as $error) {
logger($error);
}
}
});
}
Calling $validator->fails()
in the after
callback will end in an endless loop as it calls $validator->after()
in the process. It re-evaluates and does not use a previous result.
Use $validator->errors()
instead and check if it is empty.