Laravel - 更改身份验证用户的错误消息

Posted

技术标签:

【中文标题】Laravel - 更改身份验证用户的错误消息【英文标题】:Laravel - Change error message for Authenticates user 【发布时间】:2021-04-25 14:50:54 【问题描述】:

我根据AuthenticatesUsers的要求更改了登录条件

protected function credentials(Request $request)

    return array_merge($request->only($this->username(), 'password'), ['is_approved' => 1]);

但对于 Not Approved 错误消息是 这些凭据与我们的记录不匹配。

如何更改此消息?

【问题讨论】:

如果您对该字符串“这些凭据...”进行文本搜索,您将在语言文件中找到它 是的,但如果凭据不匹配,则会出错。 “如何更改此消息?”,您在该文件中更改它 【参考方案1】:

你的意思是验证吗?您可以使用 novalidation 对其进行更改。只需将其插入表格末尾即可。如果不行,可以用js。

这是一个示例 js:

var $ = jQuery;
$(document).ready(function($) 
  $("form[name='Cust_Form']").validate(
      errorElement: 'div',
    rules: 
        postal_code: 
            number: true,
            minlength: 4,
            maxlength: 4,
            required: true
            ,
        email: 
            email: true
              ,    
        unit: 
            number: true
                ,   
         building: 
            number: true
                ,   
        contact_2: 
            number: true,
            minlength: 11,
            maxlength: 11
        ,
        contact_1: 
            number: true,
            minlength: 11,
            maxlength: 11
        
      ,
    // Specify validation error messages
    messages: 
        postal_code: 
            minlength: "Your postal code must be at least 4 characters long",
            number: "Please enter a valid postal code",
            maxlength: "Your postal code must be at least 4 characters long",
            required:  "Required field"
            ,
        email: 
            email: "Please enter a valid email address"
            ,  
        unit: 
            number: "Please enter a valid unit number"
                ,   
        building: 
            number: "Please enter a valid building/house number"
                ,  
        contact_1: 
            minlength: "Your contact number must be at least 11 characters long",
            number: "Please enter a valid contact number",
            maxlength: "Your contact number must be at least 11 characters long"
        ,
        contact_2: 
            minlength: "Your contact number must be at least 11 characters long",
            number: "Please enter a valid fax/phone number",
            maxlength: "Your contact number must be at least 11 characters long"
        ,
    ,
    // Make sure the form is submitted to the destination defined
    // in the "action" attribute of the form when valid
    submitHandler: function(form) 
      form.submit();
    
  );
);

【讨论】:

以上是关于Laravel - 更改身份验证用户的错误消息的主要内容,如果未能解决你的问题,请参考以下文章

获取用户数据时出现未经身份验证的错误

Laravel 5.2 - 经过身份验证的用户更改密码 - 更新后的密码匹配问题

用户未经授权时的 Laravel 护照自定义错误消息和状态码

如何解决 Laravel SMTP 中的 2 个可能的身份验证器错误

我可以更改在 laravel 的身份验证系统中使用的模型用户吗?

每次我输入错误密码时,使用 JWT 身份验证的 Laravel 多重身份验证