I am using a Validator to validate request parameters and return helpful messages to the users of a public API. If the validator fails I return a view:
if( $validator->fails() ){
$data = ['errors' => $validator->errors()->messages() ];
return response()->view('errors.412', $data, 412)
->header("HTTP/1.0 412 Precondition Failed", null);
} else {
...
}
The view...
<ul>
@foreach( $errors as $field )
@foreach( $field as $error )
<li>{{ $error }}</li>
@endforeach
@endforeach
</ul>
Because these messages are to be consumed by developers I want them to be technical and specific. Therefore it's really annoying that Laravel automatically strips the space from my parameter keys.
For the message:
'The :attribute field is required.'
Laravel returns:
The vehicle name field is required.
...but I want the more accurate:
The vehicle_name field is required.
The only fix I have found is to add the following lines to /resources/lang/en/validation.php
:
'attributes' => [
'vehicle_name' => 'vehicle_name'
],
But that just feels backwards that I would have to provide a bunch of identical key-pair values in a language translation file just to instruct the framework to undo an unwanted behaviour.
Is there a better way?