phildawson's avatar

Customizing The Flashed Error Format - Possible doc bug

http://lumen.laravel.com/docs/validation#controller-validation

"If you would like to customize the format of the validation errors when using the validate method from route Closures, you may do so by calling the Laravel\Lumen\Routing\Closure class:"

use Laravel\Lumen\Routing\Closure;

Closure::formatErrorsUsing(function($validator) {
    return $validator->errors()->all();
});

Has anyone tried this? I think all() is the wrong as returns a flat indexed array. It makes it look like you should do this.

Closure::formatErrorsUsing(function($validator) {

    return $validator->errors()->all('<div class="alert alert-danger">:message</div>');
});

But that won't work. It would need to be something like this which returns the fields the error messages belong to.

Closure::formatErrorsUsing(function($validator) {

    $fields = $validator->errors()->getMessages();
    foreach($fields as $field => &$messages)
    {
        foreach($messages as &$message)
        {
          $message = '<div class="alert alert-danger">'.$message.'</div>';
        }
    }
    return $fields;
});
0 likes
0 replies

Please or to participate in this conversation.