5使用自定义消息进行验证

5使用自定义消息进行验证

本文介绍了Laravel 5.5使用自定义消息进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel应用程序中的Pw更改表单.我想将验证器与自定义错误消息一起使用.

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

我的代码如下:

  $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()));

custommessages 日志中显示了我的自定义msg,但是在下一行中,则是原始的 messages .

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

我正在使用官方文档.

有人遇到这个问题吗?

提前回答!

推荐答案

重写和推荐的实现方法.参考手册 https://laravel.com/docs/5.5/validation#creating-表单请求

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

使用请求文件.

  1. 运行 php artisan make:request UpdateUserPasswordRequest
  2. 写请求文件

编辑2020年2月:在最新版本的Laravel中,使用authorize方法可使用全局auth()对象代替\ Auth,因此\ Auth :: check()将变为auth()-> check().如果从框架中删除了某些内容,两者都将继续工作并会更新

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

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();
        // 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. 在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
}
}

这篇关于Laravel 5.5使用自定义消息进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:09