dwightwatson / validating

Automatically validating Eloquent models for Laravel
MIT License
968 stars 76 forks source link

Column names in message bag spaces #210

Closed NoxxieNl closed 5 years ago

NoxxieNl commented 5 years ago

Hiya,

Love the work you are doing, I am currently implementing the package for my own project.

However I have an existing database with column names all in uppercase. When an error occurs the message will be shown as:

0 => "The i m p h r e f e r e n c e has already been taken."

The column name is IMPH_REFERENCE :-)

Is there a way to to turn of the spacing (because of the capital letters)?

dwightwatson commented 5 years ago

Hi there - great to hear.

All the validation stuff under the hood is handled by Laravel's validator. It's this component that will format the column's name as it sees fit. Your best bet around it is to provide custom validation messages for the field in question which will allow you to format it as you need.

Hope this helps.

NoxxieNl commented 5 years ago

For users having the same problem I had, I dug into the source and found out that on your model add a protected variable $validationAttributeNames with an array of original names and the naming you want it do be :-).

Example:

protected $validationAttributeNames = [
        'IMPH_REFERENCE' => 'reference',
    ];

There is currently (as of Laravel 5.7) not a option to allow overwriting the formatting option on the :attribute value of the messages.