Forum: Ruby on Rails Removing "is invalid" msgs in validates_associated

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-03-21 22:07
All,

When using validates_associated to cause cascading validation from my
parent AR class into it's children, by default a "_____ is invalid"
message is added to the errors collection for the parent for each child
object that isn't valid.

I don't want to display these messages, since I am generating new
messages to stand in their place.

I am using the standard scaffolding error reporting scheme
(errorExplanation div and fieldWithErrors around each field).

I can suppress these messages by setting :message => '' or :message =>
nil on the validates_associated, but what I really want to do is to
remove the errors from the errors collection that are associated with
the "is invalid" messages.

I tried this:

#Get rid of all of the annoying "is invalid" error messages and generate
unique errors for the class codes
      old_errors = @artisan_quote_input.errors.clone
      @artisan_quote_input.errors.clear
      old_errors.each_full do |msg|
        @artisan_quote_input.errors.add_to_base(msg) unless msg =~ /is
invalid/ || @artisan_quote_input.errors.full_messages.include?(msg)
      end

But then several of my fields no longer show up with red borders in my
form.

Anybody have any experience with manipulating the ActiveRecord::Errors
object in this way?

Thanks,
Wes
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-03-21 22:23
This works much better:

old_errors = @artisan_quote_input.errors.clone
@artisan_quote_input.errors.clear
  old_errors.each do |attr, msg|
  @artisan_quote_input.errors.add(attr, msg) unless msg =~ /is invalid/
|| @artisan_quote_input.errors.full_messages.include?(msg)
end

Thanks,
Wes
This topic is locked and can not be replied to.