简体   繁体   English

如何使用simple_form显示完整的错误消息?

[英]How to display full error messages with simple_form?

Instead of short error messages like can't be blank I'd like to have full error messages like Url can't be blank . 相反,短错误消息的像can't be blank ,我想有这样完整的错误消息的Url can't be blank How can I do this with simple_form? 我该如何使用simple_form? (just for one field or one form only, not for all forms) (仅适用于一个字段或一种表单,不适用于所有表单)

There is error message YAML which you can update to create custom error messages for forms, any form gem you happen to be using or none at all. 有错误消息YAML,您可以更新该错误消息以为表单,碰巧正在使用的任何表单gem或根本不创建任何自定义错误消息。 Look for it in config/locales/en.yml. 在config / locales / en.yml中查找。 Caveat: The ymls are autoloaded and there's an entire set of non-obvious behaviors they have, but the form YAML setup is pretty intuitive. 注意:yml是自动加载的,并且它们具有一整套非显而易见的行为,但是YAML设置的形式非常直观。 When editing, do not use tabs and be careful not to permit any extra spaces anywhere or YAML parse will fail at runtime. 编辑时,请勿使用制表符,请注意不要在任何地方留有多余的空格,否则YAML解析将在运行时失败。

Here's a snippet from the activerecord/form-relevant area in my en.yml: 这是我的en.yml中与activerecord / form相关的区域的摘录:

en.yml en.yml

  activerecord: 
    errors: 
      models: 
        article: 
          attributes: 
            category: 
              blank: "You must select a category for this post."
            author: 
              blank: "You must enter an author name."
            title: 
              blank: "A title is required."
            content: 

There is a YAML file that is installed with simple_form, simple_form.en.yml in the same directory. 在同一目录中有一个YAML文件,其中安装了simple_form,simple_form.en.yml。 I haven't used that one, but it might be more intuitive/easier to use but will only apply to simple_form actions. 我还没有使用过,但是使用起来可能更直观/更容易,但仅适用于simple_form动作。

Default version simple_form.en.yml 默认版本simple_form.en.yml

en:
  simple_form:
    "yes": 'Yes'
    "no": 'No'
    required:
      text: 'required'
      mark: '*'
      # You can uncomment the line below if you need to overwrite the whole required html.
      # When using html, text and mark won't be used.
      # html: '<abbr title="required">*</abbr>'
    error_notification:
      default_message: "Please review the problems below:"
    # Examples
    # labels:
    #   defaults:
    #     password: 'Password'
    #   user:
    #     new:
    #       email: 'E-mail to sign in.'
    #     edit:
    #       email: 'E-mail.'
    # hints:
    #   defaults:
    #     username: 'User name to sign in.'
    #     password: 'No special characters, please.'
    # include_blanks:
    #   defaults:
    #     age: 'Rather not say'
    # prompts:
    #   defaults:
    #     age: 'Select your age'

Here's a YAML linter (you'll need it): http://www.yamllint.com/ 这是YAML皮棉机(您将需要它): http : //www.yamllint.com/

The YAML translation files are the answer, but it's a clunky implementation and real pain to get right. YAML转换文件是解决方案,但这是一个笨拙的实现,真正的痛苦在于正确。 Please follow up if you have any issues with it. 如果有任何问题,请跟进。

You can customize your locale-files correspondingly or define custom message inside model at validation section. 您可以相应地自定义区域设置文件,或在验证部分的模型内定义自定义消息。 Here are some possibilities for a solution. 这里有一些解决方案的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM