Customize error message with simple_form

后端 未结 4 1870
一向
一向 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:20

    1. You can declare the content of the error message in your model:

      validates_length_of :name, :minimum => 5, :message => "blah blah blah"
      
    2. You can set id or class for your error tag:

      <%= f.input :name, :error_html => { :id => "name_error"} %> 
      

      Then you can use CSS for the styling.

    3. And you can use

      <%= f.error :name, :id => "name_error" %>
      

      and you'll get

      is too short (minimum is 5 characters)
      

提交回复
热议问题