Customize error message with simple_form

后端 未结 4 1884
一向
一向 2020-12-29 13:57

I\'m using the simple_form gem. I want to customize the error message displayed when a user fails validations. How can I accomplish this?

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-29 14:27

    There is another solution explained here that wasn't mentioned in the answers. You can directly override the error messages from the views, in the form itself. For example:

    <%= f.input :last_name,
                   placeholder: 'last_name',
                   error: 'This is a custom error message',
                   required: true,
                   class: 'form-field',
                   autofocus: true,
                   input_html: { autocomplete: "last_name" } %>
    

    It is however not advised as it is not DRY, you would need to override it in every field.

提交回复
热议问题