10

I'm working on a pw change form in my laravel app. I want to use the validator with custom error messages.

My code looks like this:

  $rules = [
    'username' => 'required|max:255',
    'oldpassword' => 'required|max:255',
    'newpassword' => 'required|min:6|max:255|alpha_num',
    'newpasswordagain' => 'required|same:newpassword',
  ];
  $messages = [
     'username.required' => Lang::get('userpasschange.usernamerequired'),
     'username.max:255' => Lang::get('userpasschange.usernamemax255'),
     'oldpassword.required' => Lang::get('userpasschange.oldpasswordrequired'),
     'oldpassword.max:255' => Lang::get('userpasschange.oldpasswordmax255'),
     'newpassword.required' => Lang::get('userpasschange.newpasswordrequired'),
     'newpassword.min:6' => Lang::get('userpasschange.newpasswordmin6'),
     'newpassword.max:255' => Lang::get('userpasschange.newpasswordmax255'),
     'newpassword.alpha_num' => Lang::get('userpasschange.newpasswordalpha_num'),
     'newpasswordagain.required' => Lang::get('userpasschange.newpasswordagainrequired'),
     'newpasswordagain.same:newpassword' => Lang::get('userpasschange.newpasswordagainsamenewpassword'),
 ];

  $validator = Validator::make($request->all(), $rules, $messages);
  $validator->setCustomMessages($messages);

  Log::debug("custommessages: " . json_encode($messages));
  Log::debug("messages: " . json_encode($validator->messages()));

In the log custommessages is shows my custom msgs, but in the next line there is the original messages.

I'm working from the official doc.

Have anybody meet this problem?

Thx for the answers in advance!

kometen
  • 6,536
  • 6
  • 41
  • 51
LakiGeri
  • 2,046
  • 6
  • 30
  • 56
  • It's in the manual https://laravel.com/docs/5.6/validation#customizing-the-error-messages I don't really understand the way you use them. Can you update the code with classes please? I don't quite get how you invoke the validator. It seams to me you call the wring validator there – Indra Mar 22 '18 at 15:06
  • @Indra I made a function (in the controller) as it says but nothing changes. Should I move that function to an another file? – LakiGeri Mar 22 '18 at 15:10
  • @Indra I updated the question. – LakiGeri Mar 22 '18 at 15:13
  • 1
    You need to make a request file by running php artisan make:request RequestName and add all the logic there. Then in the controller add the request file with use and in the method do something like function update(YourRequestName $resquest){ // function logic} – Indra Mar 22 '18 at 15:14
  • @Indra I got access denied error. `AccessDeniedHttpException This action is unauthorized.` Where should I register this new request? – LakiGeri Mar 22 '18 at 15:29
  • `messages()` returns the value of the `messages` property but custom messages are stored in `customMessages`. Try `$validator->customMessages` instead. – Camilo Mar 22 '18 at 15:33
  • Its funny, because if I log this `$validator->customMessages`. It has my custom msgs, but for some reason did not use that.. – LakiGeri Mar 22 '18 at 15:39

4 Answers4

25

A rewrite and the recommended way of doing it. Manual for reference https://laravel.com/docs/5.5/validation#creating-form-requests

Use requests files.

  1. run php artisan make:request UpdateUserPasswordRequest
  2. Write the request file

Edit feb 2020: in the latest version of Laravel in the authorize method the global auth() object can be used instead of \Auth so \Auth::check() will become auth()->check(). Both still work and will update if something is removed from the framework

 <?php
     
     namespace App\Http\Requests;
 
     class UpdateUserPasswordRequest extends FormRequest
     {
         /**
          * Determine if the user is authorized to make this request.
          *
          * @return bool
          */
         public function authorize()
         {
             // only allow updates if the user is logged in
             return \Auth::check();
              // In laravel 8 use Auth::check() 
             // edit you can now replace this with return auth()->check();
         }
     
         /**
          * Get the validation rules that apply to the request.
          *
          * @return array
          */
         public function rules()
         {
             return [
                 'username' => 'required|max:255',
                 'oldpassword' => 'required|max:255',
                 'newpassword' => 'required|min:6|max:255|alpha_num',
                 'newpasswordagain' => 'required|same:newpassword',
             ];
         }
     
         /**
          * Get the validation attributes that apply to the request.
          *
          * @return array
          */
         public function attributes()
         {
             return [
                 'username'            => trans('userpasschange.username'),
                 'oldpassword'             => trans('userpasschange.oldpassword'),
                 'newpassword'             => trans('userpasschange.newpassword'),
                 'newpasswordagain'       => trans('userpasschange.newpasswordagain'),
             ];
         }
     
         /**
          * Get the validation messages that apply to the request.
          *
          * @return array
          */
         public function messages()
         {
     // use trans instead on Lang 
             return [
          'username.required' => Lang::get('userpasschange.usernamerequired'),
          'oldpassword.required' => Lang::get('userpasschange.oldpasswordrequired'),
          'oldpassword.max' => Lang::get('userpasschange.oldpasswordmax255'),
          'newpassword.required' => Lang::get('userpasschange.newpasswordrequired'),
          'newpassword.min' => Lang::get('userpasschange.newpasswordmin6'),
          'newpassword.max' => Lang::get('userpasschange.newpasswordmax255'),
          'newpassword.alpha_num' =>Lang::get('userpasschange.newpasswordalpha_num'),
          'newpasswordagain.required' => Lang::get('userpasschange.newpasswordagainrequired'),
          'newpasswordagain.same:newpassword' => Lang::get('userpasschange.newpasswordagainsamenewpassword'),
           'username.max' => 'The :attribute field must  have under 255 chars',
             ];
         }
  1. In UserController
<?php namespace App\Http\Controllers;


// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\UpdateUserPasswordRequest as ChangePassRequest;
//etc

class UserCrudController extends Controller
{
public function chnagePassword(ChangePassRequest $request)
{
 // save new pass since it passed validation if we got here
}
}
Indra
  • 692
  • 6
  • 18
  • If it isn't an answer it shouldn't be added as one. Also, the OP linked to the Laravel 5.5 documentation. – Camilo Mar 22 '18 at 15:49
  • Thanks! I give it a chance – LakiGeri Mar 22 '18 at 15:51
  • @Camilo it does solve the issue in a really nice way. And it's exactly the same in 5.5 and 5.6 – Indra Mar 22 '18 at 15:51
  • @LakiGeri let me know if you still have issues – Indra Mar 22 '18 at 15:54
  • @Indra I really appreciate your help! I made a request as you show. The attributes works well, but the messages dont.. So I deleted the lang/en/validation.php so the result 'validation.min.string' :D .. so the messages is 'stuborn'. I will translate the validation.php and it will solve my problem. I want to thank you again, I learnt from it. :) – LakiGeri Mar 22 '18 at 16:02
  • The thing is that validator.php is generated by laravel so adding things there should work out. Also you need to duplicate that file for each language and if things are changed for just a validator then if should be added there. Sorry I wasn't more specific about this. Happy to help – Indra Mar 22 '18 at 16:12
8

For Laravel 7.x, 6.x, 5.x
With the custom rule defined, you might use it in your controller validation like :

$validatedData = $request->validate([
       'f_name' => 'required|min:8',
       'l_name' => 'required',
   ],
   [
      'f_name.required'=> 'Your First Name is Required', // custom message
      'f_name.min'=> 'First Name Should be Minimum of 8 Character', // custom message
      'l_name.required'=> 'Your Last Name is Required' // custom message
   ]
);

For localization you can use :

['f_name.required'=> trans('user.your first name is required'],

Hope this helps...

STA
  • 30,729
  • 8
  • 45
  • 59
0

After you've indicated the messages in Validator::make

$validator = Validator::make($request->all(), $rules, $messages);

you shouldn't indicate them again

$validator->setCustomMessages($messages); // don't do that

NOTE

The better way to use Request validation is to move them to another file

-2
$messages = [
     'username.required' => Lang::get('userpasschange.usernamerequired'),
     'username.max' => Lang::get('userpasschange.usernamemax255'),
     'oldpassword.required' => Lang::get('userpasschange.oldpasswordrequired'),
     'oldpassword.max' => Lang::get('userpasschange.oldpasswordmax255'),
     'newpassword.required' => Lang::get('userpasschange.newpasswordrequired'),
     'newpassword.min' => Lang::get('userpasschange.newpasswordmin6'),
     'newpassword.max' => Lang::get('userpasschange.newpasswordmax255'),
     'newpassword.alpha_num' => Lang::get('userpasschange.newpasswordalpha_num'),
     'newpasswordagain.required' => Lang::get('userpasschange.newpasswordagainrequired'),
     'newpasswordagain.same:newpassword' => Lang::get('userpasschange.newpasswordagainsamenewpassword'),
 ];

Try to dont use this :255 and :6 endings.


Wrong:

'username.max:255' => Lang::get('userpasschange.usernamemax255'),

Correct:

'username.max' => Lang::get('userpasschange.usernamemax255'),
  • 3
    Please add an explanation to your answers. Code only answers are often not useful, as they don't teach what the code does, why it should be used etc. Also, how is this different from [Indra](https://stackoverflow.com/users/1491008/indra)'s [answer](https://stackoverflow.com/a/49432986/9199167)? It looks almost like a copypaste. – Max Vollmer Jul 13 '19 at 22:54