9

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?

2
  • 1
    do you want to customise the error message or the style of error message disolayed? Commented Apr 27, 2011 at 18:34
  • i initially meant the content of error message, but I'd also be interested in customizing the style as well. Sorry for not being clear. Commented Apr 27, 2011 at 18:47

4 Answers 4

18
  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

    <span class="error" id="name_error">is too short (minimum is 5 characters)</span>
    
Sign up to request clarification or add additional context in comments.

Comments

8

I dont know if it is any different for simple_form gem.

For content of error messages to be changed, you can use the :message attribute in the model.

class User < ActiveRecord::Base
  validates :email, {:presence => true, :message => "is not filled up."}
end

Now the validation message will be Email is not filled up. If you want the field name also to be changed(Email to E-mail address something like that ), the approach now is to define it in locales.rb file like this

# config/locales/en.yml
en:
  activerecord:
    attributes:
      user:
        email: "E-mail address"

See link for details on locales. Another approach is to define in the model, humanized attributes like this:

class User < ActiveRecord::Base
  validates :email, {:presence => true, :message => "is not filled up."}
  HUMANIZED_ATTRIBUTES = {
    :email => "E-mail address",
    ...(other fields and their humanized names)
    ...
  }

  def self.human_attribute_name(attr, options={})
    HUMANIZED_ATTRIBUTES[attr.to_sym] || super
  end

end

For customizing style of validation message we will have to edit the style for #errorExplanation and .fieldWithErrors,in the scaffold.css stylesheet.

Comments

4

You can easily change the default error message comes in the translation file, which is found in config/locales/simple_form.en.yml.

In the specific initializer, config/initializers/simple_form.rb you can overrule the default options how the html is generated.

Hope this helps.

For completeness, I would like to add that formtastic is an easier choice to start with, because it has a default layout. I like simple_form a lot, but it does not offer any formatting out of the box, but that is their intention. With Formtastic it is very hard (impossible) to change the generated html, and with simple_form can you can completely mold the generated html to your liking. This is especially useful if you have a designer, and the forms you generate have to generate the same html. So if you are getting started, formtastic will give you nicer-looking results quicker. Also note that it is quite easy to switch, because the syntax is almost identical.

4 Comments

what's the syntax for changing the default error messages in simple_form.en.yml? what if you have two validations for an attribute, like :presence => true and :format => ...? how would you provide different messages for each?
The standard validation messages has nothing to do with simple_form, but is standard ActiveRecord. You either explicitly declare the message in the validation, or you can check github.com/svenfuchs/rails-i18n where the standard messages are defined (and how to overwrite them).
Is there a way to configure simple_form to link labels and error messages to the default activerecord i18n yaml?
Your question is not enitrely clear, please ask it in a separate question; btw i have also answered a question how to use i18n for labels in simple-form, their documentation on github covers this very well imho.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.