Custom Laravel validation messages

后端 未结 6 1142
北恋
北恋 2020-12-23 13:07

I\'m trying to create customized messages for validation in Laravel 5. Here is what I have tried so far:

$messages = [
    \'required\'  => \'Harap bagian         


        
相关标签:
6条回答
  • 2020-12-23 13:27

    You can provide custom message like :

    $rules = array(
                'URL' => 'required|url'
            );    
    $messages = array(
                    'URL.required' => 'URL is required.'
                );
    $validator = Validator::make( $request->all(), $rules, $messages );
    
    if ( $validator->fails() ) 
    {
        return [
            'success' => 0, 
            'message' => $validator->errors()->first()
        ];
    }
    

    or

    The way you have tried, you missed Validator::replacer(), to replace the :variable

    Validator::replacer('custom_validation_rule', function($message, $attribute, $rule, $parameters){
        return str_replace(':foo', $parameters[0], $message);
    });
    

    You can read more from here and replacer from here

    0 讨论(0)
  • 2020-12-23 13:30
    $rules = [
      'username' => 'required,unique:Userlogin,username',
      'password' => 'required',
      'email'    => 'required,unique:Userlogin,email'
    ];
    
    $messages = [
      'required'  => 'The :attribute field is required.',
      'unique'    => ':attribute is already used'
    ];
    
    $request->validate($rules,$messages);
    //only if validation success code below will be executed
    
    0 讨论(0)
  • 2020-12-23 13:32

    If you use $this->validate() simplest one, then you should write code something like this..

    $rules = [
            'name' => 'required',
            'email' => 'required|email',
            'message' => 'required|max:250',
        ];
    
        $customMessages = [
            'required' => 'The :attribute field is required.'
        ];
    
        $this->validate($request, $rules, $customMessages);
    
    0 讨论(0)
  • 2020-12-23 13:36

    Laravel 5.7.*

    Also You can try something like this. For me is the easiest way to make custom messages in methods when you want to validate requests:

    public function store()
    {
        request()->validate([
            'file' => 'required',
            'type' => 'required'
        ],
        [
            'file.required' => 'You have to choose the file!',
            'type.required' => 'You have to choose type of the file!'
        ]);
    }
    
    0 讨论(0)
  • 2020-12-23 13:36

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

    $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...

    0 讨论(0)
  • 2020-12-23 13:36
    //Here is the shortest way of doing it.
     $request->validate([
         'username' => 'required|unique:Userlogin,username',
         'password' => 'required',
         'email'    => 'required|unique:Userlogin,email'
     ],
     [
         'required'  => 'The :attribute field is required.',
         'unique'    => ':attribute is already used'
     ]);
    //The code below will be executed only if validation is correct.
    
    0 讨论(0)
提交回复
热议问题