laravellaravel-5jsvalidation

Custom Validation Attributes for Array Type Form Fields using proengsoft/laravel-jsvalidation in Laravel 5.2


I am creating a CRUD using dimsav translatable and proengsoft/laravel-jsvalidation packages for Laravel. The form field names have to follow an array structure like this...

<div class="form-group">
    {!! Form::label("es[title]", trans("messages.title"), ["class" => "control-label"]) !!}
    {!! Form::text("es[title]", getFormInput($entry, "title", $locale), ["class" => "form-control", "id" => "es[title]"]) !!}
</div>

To be able to use mass asignment easily on the controller.

The create form is being validated with a CreateRequest as follows...

{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
         return [
             'es.title' => 'required|max:255',           
         ];
    }
}

I don't know how to change the attribute placeholders with more friendly texts at resources/lang/validation.php file. I have tried the following options...

'attributes' => [      
    'title' => 'título',
    //'es.title' => 'título',       
    //'es[title]' => 'título',       
],

...but any of them is working. The form is being validated ok naming the field 'es.title' but the error message showed is not replacing the field name correctly even if I name the attributes array key also 'es.title'. Any ideas?


Solution

  • Inside App/Http/Requests/YourRequest

    public function messages()
        {
            return [
                'es.title.required' => 'You forgot título!',
            ];
        }
    

    You can set different messages for different validation rules(required,max etc)

    For setting up custom name for an attribute

    public function attributes()
    {
        return [
            'es.title' => 'título'
        ];
    }
    

    EDITED For setting up attribute name globally go to lang/en/validation and you'll see an attribute array. Change it according to you requirement.

    'attributes' => [
        'es' => [
            'title' => 'título'
        ]
    ],