自定义消息laravel验证

时间:2017-07-10 09:06:04

标签: php laravel-5

我正在尝试创建用于验证的laravel自定义消息。这是我到目前为止所尝试的:

$messages = [
            'required'  => 'Harap bagian :attribute di isi.',
            'unique'    => ':attribute sudah digunakan',
        ];
        $validator = Validator::make($request->all(), [
            'username'    => array('required','unique:Userlogin,username'),
            'password' => 'required',
            'email' => array('required','unique:Userlogin,email'),$messages
        ]);

        if ($validator->fails()) { 
            return redirect('/')
                    ->withErrors($validator) // send back all errors to the login form
                    ->withInput();
        }else{
            return redirect('/')->with('status', 'Kami sudah mengirimkan email, silahkan di konfirmasi');   
        }   

但它没有用。该消息仍然与默认消息相同。我该如何解决这个问题,以便我可以使用自定义消息?

7 个答案:

答案 0 :(得分:22)

如果您使用$this->validate()最简单的那个,那么您应该编写类似这样的代码..

$rules = [
        'name' => 'required',
        'email' => 'required|email',
        'message' => 'required|max:250',
    ];

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

    $this->validate($request, $rules, $customMessages);

答案 1 :(得分:11)

您可以提供以下自定义消息:

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

您尝试过的方法,错过Validator::replacer(),以替换:variable

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

您可以阅读here中的更多内容以及here

中的替换者

答案 2 :(得分:9)

Laravel 5.7。*

您也可以尝试类似的方法。对我来说,当您要验证请求时,是在方法中创建自定义消息的最简单方法:

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!'
    ]);
}

答案 3 :(得分:6)

对于Laravel 7.x 6.x 5.x
定义了自定义规则后,您可以像这样在控制器验证中使用它:

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

对于本地化,您可以使用:

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

希望这对您有帮助...

答案 4 :(得分:1)

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

答案 5 :(得分:1)

您也可以使用 setAttributeNames()setCustomMessages() 方法, 像这样:

$validation = Validator::make($this->input, static::$rules);

$attributeNames = array(
    'email' => 'E-mail',
    'password' => 'Password'
);

$messages = [
    'email.exists' => 'No user was found with this e-mail address'
];

$validation->setAttributeNames($attributeNames);
$validation->setCustomMessages($messages);

答案 6 :(得分:-2)

float: none;
display: flex;
justify-content: center;