Rails Validates_presence_of Custom Error Message
Contents |
Validations Active Record Callbacks Active Record Associations Active Record Query Interface Views Layouts and Rendering in Rails Action View Form
Rails Custom Validation Method
Helpers Controllers Action Controller Overview Rails Routing from the Outside rails 4 validates presence message In Digging Deeper Active Support Core Extensions Rails Internationalization API Action Mailer Basics Active Job Basics Testing
Rails 4 Validation Error Message
Rails Applications Securing Rails Applications Debugging Rails Applications Configuring Rails Applications Rails Command Line Tools and Rake Tasks Asset Pipeline Working with JavaScript in Rails Autoloading unknown validator: 'messagevalidator' and Reloading Constants Caching with Rails: An Overview Using Rails for API-only Applications Action Cable Overview Extending Rails Rails on Rack Creating and Customizing Rails Generators Contributing to Ruby on Rails Contributing to Ruby on Rails API Documentation Guidelines Ruby on Rails Guides Guidelines Maintenance Policy Maintenance Policy Release Notes Upgrading Ruby on Rails rails validation message without field name Ruby on Rails 5.0 Release Notes Ruby on Rails 4.2 Release Notes Ruby on Rails 4.1 Release Notes Ruby on Rails 4.0 Release Notes Ruby on Rails 3.2 Release Notes Ruby on Rails 3.1 Release Notes Ruby on Rails 3.0 Release Notes Ruby on Rails 2.3 Release Notes Ruby on Rails 2.2 Release Notes Contribute Credits Guides Index Getting Started with Rails Active Record Basics Active Record Migrations Active Record Validations Active Record Callbacks Active Record Associations Active Record Query Interface Layouts and Rendering in Rails Action View Form Helpers Action Controller Overview Rails Routing from the Outside In Active Support Core Extensions Rails Internationalization API Action Mailer Basics Active Job Basics Testing Rails Applications Securing Rails Applications Debugging Rails Applications Configuring Rails Applications Rails Command Line Tools and Rake Tasks Asset Pipeline Working with JavaScript in Rails Autoloading and Reloading Constants Caching with Rails: An Overview Using Rails for API-only Applications Action Cable Overview Rails on Rack Creating
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads
Rails Validate Message
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the rails error messages Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute:
Rails Validation Error Message Not Displaying
Sign up validates_presence_of + :message shows the name of the field up vote 10 down vote favorite 5 I'm creating a Rails app, and I have a model called User. In this model I have a boolean value called isagirl. A http://guides.rubyonrails.org/active_record_validations.html user must specify if it is a girl or not, which is done by two radio buttons. In my model I have this: validates_presence_of :isagirl, :message => "You must be either a Boy or a Girl. If not, please contact us." However, when I don't specify a sex, I'm seeing this: Isagirl You must be either a Boy or a Girl. as an error message. The problem is that 'Isagirl' must not be there in the error message. How can I disable that? http://stackoverflow.com/questions/2269183/validates-presence-of-message-shows-the-name-of-the-field And no, using CSS to hide it is no option. Thanks ruby-on-rails validation model share|improve this question edited Jun 25 '12 at 19:38 asked Feb 15 '10 at 21:52 user142019 3 for what it is worth, it's a good idea to support non-binary genders in forms if possible –Alex Moore-Niemi Sep 9 '15 at 14:35 add a comment| 4 Answers 4 active oldest votes up vote 8 down vote accepted The way that I do this is to output the message without the field name. For example, I have a partial that outputs the error messages after validation fails.
- <% errors.each do |attribute, message| -%> <% if message.is_a?(String)%>
- <%= message %> <% end %> <% end -%> Notice that this does not output the attribute. You just need to make sure that all your messages makes sense without an attribute name. share|improve this answer answered Feb 15 '10 at 22:08 Randy Simon 2,7731318 Okay, but how does that code know which form you want? –user142019 Feb 15 '10 at 22:11 add a comment| up vote 5 down vote In one of my projects I was using custom-err-msg plugin. With it when you specify error message this way: :message => "^You must be either a Boy or a Girl. If not, please contact us." (notice ^ at the begining) it won't print attribute name when printing errors. And you can use standard error_messages or error_messages_for helpers. share|improve this answer answered Feb 15
API , the i18n wiki is available here . From rails 2.2 onward the I18n API is bundled as a dependency gem for rails. It is always a good practice to https://codedecoder.wordpress.com/2013/09/11/customize-error-message-with-locales-rails/ separate out, constant stuffs exposed to user , like validation error messages , flash notices etc in locales. If you structure your code like this from beginning, it will be easy for you to display the message in any language, the default is English. So by default rails will load all the messages from en.yml file. If say your site is in Germany and you want error message to show the validation message in German language. Then you will write all the validation message in de.yml file and set the default language to :de in application.rb file with the below line config.i18n.default_locale = :de So now rails will try to look for the message in de.yml file. At any point, you can make rails to load a specific language file any time by validation error message passing locales parameter in URL Example : http://my_xyz.com/sigin?locales=en # it will load all the locales from en.yml extensions like devise.en.yml, rest.en.yml etc http://my_xyz.com/sigin?locales=de # it will load all the locales from de.yml extensions like devise.de.yml, rest.de.yml etc The best way is to provide the extension in domain name itself Example: http://my_xyz.com.en http://my_xyz.com.de Above explanation is just a Introduction of locales and I will not get into more detail here. You may read the reference provided below. Let us move our validation error message to locals. Below, is the current validation class User < ActiveRecord::Base validates :source_system, :source_system_id validates_uniqueness_of :source_system_id, :message => "ID has already been taken" validates :entity_type, :inclusion => {:in => %w(Customer User), :message => "should be Customer or User"} validates :target_system, :inclusion => {:in => %w(LOCAL OPENAM), :message => "should be LOCAL or OPENAM"} end We can move these validation message to en.yml file locales. I have also shown providing custom name to the model and its attributes. But let us first remove the message from the user model, we do not need them as we are now setting the message in the locals class User < ActiveRecord::Base validates :source_system, :source_system_id validates_uniqueness_of :sour