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?
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- How to specify memcache server to Rack::Session::M
- Date with SimpleDateFormat in Java
相关文章
- Angular Material Stepper causes mat-formfield to v
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- Why doesn't Django enforce my unique_together
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
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.Now the validation message will be
Email is not filled up
. If you want the field name also to be changed(Email
toE-mail address
something like that ), the approach now is to define it inlocales.rb
file like thisSee link for details on
locales
. Another approach is to define in the model, humanized attributes like this:For customizing style of validation message we will have to edit the style for
#errorExplanation
and.fieldWithErrors
,in thescaffold.css
stylesheet.You can declare the content of the error message in your model:
You can set
id
orclass
for your error tag:Then you can use CSS for the styling.
And you can use
and you'll get
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.