Fabio Santos
Fabio Santos

Reputation: 253

laravel 5.2 set attribute name

How i set custom the attributte names in laravel 5.2 I already try this code, but doesn't work:

$attNames = array(
        'code' => 'Número',
        'contributor' => 'Nº Contribuinte',
        'create_date' => 'Data criação',
        'address' => 'Morada',
        'zip_code' => 'Cod. Postal',
        'city' => 'Localidade',
        'email' => 'E-mail',
        'phone_number' => 'Telefone',
        'note' => 'Observações',
    );

    $validator = Validator::make($client, $this->rules,[],$attNames);
    $validator->setAttributeNames($attNames); 
    if ($validator->fails()) {
        // send back to the page with the input data and errors
        $errors = $validator->messages();
        return Redirect::to('/client/create')->withInput()->withErrors($errors);
    }

Upvotes: 0

Views: 939

Answers (2)

Fabio Santos
Fabio Santos

Reputation: 253

I figure out. controller:

use Validator;
(...)
$attName=array(
            'code' => trans('validation.code'),
            'contributor' => trans('validation.contributor'),
            'create_date' => trans('validation.create_date'),
            'address' => trans('validation.address'),
            'zip_code' => trans('validation.zip_code'),
            'city' => trans('validation.city'),
            'email' => trans('validation.email'),
            'phone_number' => trans('validation.phone_number'),
            'note' => trans('validation.note'),
        );
 $validator = Validator::make($client, $this->rules, [], $attNames);

validation.php:

'attributes' => [
    'code' => 'número',
    'contributor' => 'nº contribuinte',
    'create_date' => 'data criação',
    'address' => 'morada',
    'zip_code' => 'cod. postal',
    'city' => 'localidade',
    'email' => 'e-mail',
    'phone_number' => 'telefone',
    'note' => 'observações',
],

Upvotes: 0

Ravi Hirani
Ravi Hirani

Reputation: 6539

You have passed wrong arguments to Validator::make.

You can pass only three arguments.

As per Documentation,

If needed, you may use custom error messages for validation instead of the defaults. There are several ways to specify custom messages. First, you may pass the custom messages as the third argument to the Validator::make method.

$messages = [
    'required' => 'The :attribute field is required.',
];

$validator = Validator::make($input, $rules, $messages);

Upvotes: 1

Related Questions