0

When registering a new user, I use a RegisterFormRequest from Laravel.

How can I use the label name translations to display correctly the error message ?

Here is my html code,

<div>
  <label for="password">
    {{ Lang::get('form.password') }} *
  </label>
  <input name="password" type="password" required>
</div>

<div>
  <label for="password_confirmation">
    {{ Lang::get('form.password.confirm') }} *
  </label>
  <input name="password_confirmation" type="password" class="form-control" required>
</div>

Here is what is displayed. Note that the input field's "password" is used as is, but not any translations.

Cedric
  • 5,135
  • 11
  • 42
  • 61
  • 1
    Do you have file `resources\lang\en\validation.php` ? or you have folder other than `en` ? – Mohamed Mo Kawsara Nov 09 '15 at 14:39
  • I do have this folder, and others. However, no validation.php file. Are you implying that validation.php is the standard file where Laravel fetches translations automatically, for each input ? – Cedric Nov 09 '15 at 15:06
  • Yes! it is the default one if you are trying to fetch the content of $erros variable, which it has been filled via `request` – Mohamed Mo Kawsara Nov 11 '15 at 07:59

2 Answers2

2

in the Http\Controllers\YourController.php

public function postRegister(RegisterFormRequest $request)
{
    $validator = $this->registrar->validator($request->all());

    $validator = $validator->setAttributeNames( [
        'password' => 'My Password Name'
    ] );

will do the trick.


As @ali as implied, it might be better to put these values into your request class (it is where you can put the specific rules, so why not the specific label translation?).

in the Http\Controllers\YourController.php

public function postRegister(RegisterFormRequest $request)
{
    $validator = $this->registrar->validator($request->all());

    $validator = $validator->setAttributeNames( $request->messages() );

in the Http\Requests\RegisterFormRequest.php

public function messages()
{
    return [
        'password'        => Lang::get('form.password'),

Laravel validation attributes "nice names"

And (as @mohamed-kawsara mentionned) in

resources/lang/de/validation.php

return [

...

"confirmed"            => "The :attribute confirmation does not match.",

...
Community
  • 1
  • 1
Cedric
  • 5,135
  • 11
  • 42
  • 61
1

What you need is custom message in you request class.

Write this function in your request class.

public function messages()
{
    return [
        'password.required' => 'YOUR CUSTOM MESSAGE FOR PASSWORD REQUIREMENT'
    ];
}
AliRNazari
  • 1,044
  • 18
  • 28