Rails 3 Add Error Message To Model
Contents |
helpers. A minimal implementation could be: class Person # Required dependency for ActiveModel::Errors extend ActiveModel::Naming def initialize @errors = ActiveModel::Errors.new(self) end attr_accessor :name attr_reader rails validation message :errors def validate! errors.add(:name, :blank, message: "cannot be nil") if name.nil? rails custom validations end # The following methods are needed to be minimally implemented def read_attribute_for_validation(attr) send(attr) end def self.human_attribute_name(attr, rails error messages in view options = {}) attr end def self.lookup_ancestors [self] end end The last three methods are required in your object for Errors to be able to generate error
Unknown Validator: 'messagevalidator'
messages correctly and also handle multiple languages. Of course, if you extend your object with ActiveModel::Translation you will not need to implement the last two. Likewise, using ActiveModel::Validations will handle the validation related methods for you. The above allows you to do: person = Person.new person.validate! # => ["cannot be nil"] person.errors.full_messages # => ["name cannot activerecord errors full messages be nil"] # etc.. Methods # [], []= A add, add_on_blank, add_on_empty, added?, as_json B blank? C clear, count D delete E each, empty? F full_message, full_messages, full_messages_for G generate_message, get H has_key? I include? K key?, keys M marshal_dump, marshal_load N new S set, size T to_a, to_hash, to_xml V values Included Modules Enumerable Constants CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank, :strict] MESSAGE_OPTIONS = [:message] Attributes [R] details [R] messages Class Public methods new(base) Link Pass in the instance of the object that is using the errors object. class Person def initialize @errors = ActiveModel::Errors.new(self) end end Source: show | on GitHub # File activemodel/lib/active_model/errors.rb, line 72 def initialize(base) @base = base @messages = apply_default_array({}) @details = apply_default_array({}) end Instance Public methods [](attribute) Link When passed a symbol or a name of a method, returns an array of errors for the method. person.errors[:name] # => ["cannot be nil"] person.errors['name'] # => ["cannot be nil"] Note that, if you try to
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 rails errors add custom message About Us Learn more about Stack Overflow the company Business Learn more about
Activerecord::recordinvalid
hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join
Rails Validates_presence_of
the 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: Sign up Fully custom validation error http://api.rubyonrails.org/classes/ActiveModel/Errors.html message with Rails up vote 191 down vote favorite 108 Using Rails I'm trying to get an error message like "The song field can't be empty" on save. Doing the following: validates_presence_of :song_rep_xyz, :message => "can't be empty" ... only displays "Song Rep XYW can't be empty", which is not good because the title of the field is not user friendly. How can I change the http://stackoverflow.com/questions/808547/fully-custom-validation-error-message-with-rails title of the field itself ? I could change the actual name of the field in the database, but I have multiple "song" fields and I do need to have specific field names. I don't want to hack around rails' validation process and I feel there should be a way of fixing that. ruby-on-rails share|improve this question edited Jun 14 '10 at 10:08 asked Apr 30 '09 at 19:05 marcgg 33.9k39153210 add a comment| 10 Answers 10 active oldest votes up vote 334 down vote accepted Now, the accepted way to set the humanized names and custom error messages is to use locales. # config/locales/en.yml en: activerecord: attributes: user: email: "E-mail address" errors: models: user: attributes: email: blank: "is required" Now the humanized name and the presence validation message for the "email" attribute have been changed. Validation messages can be set for a specific model+attribute, model, attribute, or globally. share|improve this answer edited Oct 17 '12 at 16:30 answered May 18 '10 at 16:43 graywh 6,30021923 16 If you are using mongoid, replace activerecord: with mongoid: –Intentss Nov 6 '11 at 14:07 83 @graywh: Where should questions about an answer be post
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and http://stackoverflow.com/questions/4752106/what-is-the-rails3-version-of-errors-add-to-base policies of this site About Us Learn more about Stack Overflow the http://stackoverflow.com/questions/10488302/rails-3-how-to-generate-custom-error-message-from-failed-validation company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes error message a minute: Sign up What is the Rails3 version of errors.add_to_base? up vote 37 down vote favorite 6 I want to write a custom validation in Rails 3, but following this example I get: 'ActiveModel::DeprecatedErrorMethods.add_to_base' call is deprecated in Rails 3.0 What is the rails3 version of: def validate errors.add_to_base "If you are attaching a file you must enter a label for it" if !attachment.blank? and attachment_label.blank? rails 3 add end validation ruby-on-rails-3 model share|improve this question asked Jan 20 '11 at 20:17 Marius Butuc 6,8011458100 add a comment| 4 Answers 4 active oldest votes up vote 44 down vote accepted This should work in rails 3.1.3: errors.add :base, "message" share|improve this answer answered Dec 22 '11 at 17:28 Drew Johnson 5,78662434 add a comment| up vote 37 down vote From http://apidock.com/rails/ActiveRecord/Errors/add_to_base: Use model_instance.errors[:base] << "Msg" instead of deprecated model_instance.errors.add_to_base("Msg") for Rails 3. share|improve this answer answered Jan 20 '11 at 20:20 Brian Donovan 5,00711722 37 Or model.errors.add(:base, '...') –noodl Jan 20 '11 at 20:21 2 If you want Rails to look up the message from a locale, you can use errors.add(:base, :your_error_symbol) and store it in [locale].activerecord.errors.[model].your_error_symbol –graywh Jan 15 '13 at 21:14 add a comment| up vote 3 down vote For me this "hack" worked best: instance.errors.add("", "Msg") When I tried to specify "base" as the first argument, the word base kept getting inserted into my validation messages. share|improve this answer edited Aug 27 '11 at 8:00 JVerstry 20.6k48164300 answered Apr 22 '11 at 19:28 Lance Carlso
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 with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the 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: Sign up Rails 3: how to generate custom error message from failed validation up vote 7 down vote favorite 3 I'm using validates :feed_id, presence: true, uniqueness: true How should I be generating a custom error message to specify that the user has already subscribed to this feed (the feed_id) field is a duplicate I know I can just do validate_uniqueness_of but it would clutter up the code unnecessarily. How do I pass a specific error message if uniqueness validation fails?? ruby-on-rails ruby ruby-on-rails-3 validation share|improve this question asked May 7 '12 at 19:59 Ken W 4022718 add a comment| 1 Answer 1 active oldest votes up vote 16 down vote accepted Put a hash with the key message and desired message as the value instead of true: validates :feed_id, presence: true, uniqueness: {message: "already subscribed"} share|improve this answer answered May 7 '12 at 20:07 jdoe 12.9k12741 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged ruby-on-rails ruby ruby-on-rails-3 validation or ask your own question. asked 4 years ago viewed 6680 times active 4 years ago Blog Stack Overflow Podcast #92 - The Guerilla Guide to Interviewing Related 191Fully custom validation error message with Rails1013How can I rename a database column in a Ruby on Rails migration?0How do I make sure an association's attribute is correct to validate the association?80validation custom message for rails 37Customize error message with simple_form0How can I validate only specific fields when doing update_attributes in Rails?0rails 3 custom validation error messages in a join table, how?1Rails Validations and custom error messages0Custom Validations Rails Model1Rails validate model attribute based on value of another attribute Hot Network Questions When math and english collide! Ca