【问题标题】:All possible model validation errors所有可能的模型验证错误
【发布时间】:2023-04-09 10:31:01
【问题描述】:

我有一个包含大量字段和模型验证的表单。

如何返回所有可能引发的验证错误?

我需要它来为所有这些语言编写语言环境。

我想要一个这样的列表:

password blank
password too_short
password confirmation
login blank
login invalid
email blank
email too_short
email invalid

【问题讨论】:

    标签: ruby-on-rails ruby validation forms


    【解决方案1】:

    最新的 Rails 翻译位于:rails-i18n

    ActiveRecord 错误在每个 .yaml 中的 lang:errors 和 lang:active_record 下。

    同样在你的应用程序中 config/locales/en.yml 是默认的。

    【讨论】:

    • 很好:) 我喜欢。但在这里我想返回特定模型的所有可能错误
    • 这很棘手。每个模型类都有验证器属性,您可以获得一些基本的验证自省。但更简单的方法是简单地创建模型实例,对其调用 validate,然后检查 errors 属性的结果。
    • 我忘了问,为什么不为您的语言添加正确的语言环境文件并更改 config/application.rb 中的 config.i18n.default_locale 以使用它?
    • 我可以为空白实例调用validate,但不会收到所有错误(不会引发无效或确认错误)。而且我不想使用全局语言环境,因为我想为每个错误为许多模型设置单独的错误
    【解决方案2】:

    基本上是 Pablo 所说的,只是 rails 文档上的页面没有显示如何覆盖特定模型和字段的消息。这是我的一个应用程序中的一个示例:

    activerecord:
      errors:
        full_messages:
          format: "{{message}}"    
        #define standard error messages, which we can overide on per model/per attribute basis further down
        messages:
          inclusion: "{{attribute}} is not included in the list"
          exclusion: "{{attribute}} is reserved"
          invalid: "{{attribute}} is invalid"
          confirmation: "{{attribute}} doesn't match confirmation"
          accepted: "{{attribute}} must be accepted"
          empty: "{{attribute}} can't be empty"
          blank: "{{attribute}} can't be blank"
          too_long: "{{attribute}} is too long (maximum is {{count}} characters)"
          too_short: "{{attribute}} is too short (minimum is {{count}} characters)"
          wrong_length: "{{attribute}} is the wrong length (should be {{count}} characters)"
          taken: "{{attribute}} has already been taken"
          not_a_number: "{{attribute}} is not a number"
          greater_than: "{{attribute}} must be greater than {{count}}"
          greater_than_or_equal_to: "{{attribute}} must be greater than or equal to {{count}}"
          equal_to: "{{attribute}} must be equal to {{count}}"
          less_than: "{{attribute}} must be less than {{count}}"
          less_than_or_equal_to: "{{attribute}} must be less than or equal to {{count}}"
          odd: "{{attribute}} must be odd"
          even: "{{attribute}} must be even"
          record_invalid: "Validation failed: {{errors}}"    
        models:
          quiz:
            blank: "{{attribute}} can not be blank"
          user_session:
            blank: "{{attribute}} can not be blank"
            attributes:
              login:
                invalid: "Please enter your user name"   
              password:
                invalid: "Please note that passwords are case sensitive"  
    

    我还更改了错误消息的基本格式,因为有时我不希望将字段名称放在消息的开头。所以,我改变了

    errors:
      format: "{{attribute}} {{message}}"
    

    errors:
      format: "{{message}}"    
    

    这就是为什么我在随后的错误中指定{{attribute}},以便在大多数但不是所有情况下将其放回原处。

    还要注意,我使用的是 {{var}} 的旧语法,而不是 %{var}。但同样的原则也适用。

    【讨论】:

    • 谢谢,我知道所有这些东西,问题是如何获取特定模型的错误列表,以便为我的模型特定消息重写其错误
    • 由于每个验证通常都有一个错误消息,因此您可以使用模型类的验证器属性来说明将验证应用于您的模型,然后在验证类及其错误消息之间使用映射。另一种方法是用 I18n.translate 替换 I18n.translate!在您进行测试时,针对您需要支持的每种语言运行测试,并手动修复所有缺失的翻译。
    • @Pablo Castellazz,在我的情况下,每个验证都有不同的错误消息:) 这就是问题
    • 你的意思是,不同字段的相同验证在同一个模型中有不同的错误信息?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多