Robert Bradford
2008-Jul-30 00:28 UTC
Re: :has_many :through form validation messages problem
Usman Akram wrote:> below is the result for the validation messages > > Select Date Time is required > date is required > Listing title is required > Lvalues is invalid > Lvalues is invalid > > > first three lines are ok but I dont want the default messages for every > child validation failure. how is it possible i dont want "Lvalues is > invalid" because i have multiple Lvalues in one form and its quite > confusing for me.I was wondering the same thing...Have you found a solution? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> I was wondering the same thing...Have you found a solution?No man just waiting for some help. its not a bug the validation takes place only once I just dont know how to switch off the invalid record notification. u can see the custom validation messages are being sent out but also along with the invalid child validation . if you find something kindly inform Regards! Usman -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
2008/7/30, Usman Akram <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > > > > I was wondering the same thing...Have you found a solution? > > No man just waiting for some help. its not a bug the validation takes > place only once I just dont know how to switch off the invalid record > notification. u can see the custom validation messages are being sent > out but also along with the invalid child validation . if you find > something kindly inform > Regards! > Usman > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I had to come up with a hack to get around this (duplicate and generic
validation error messages). I also wanted to include my specific error
messages from the child models. Basically, this is my custom
validator:
#custom validator so i can make error messages appear the way i want
#(instead of validates_associated)
def valid_associated_records?
associated_records.each do |ar|
if(!ar.nil? && !ar.valid?)
#get rid of the default error message
errors.delete(:attribute_name)
#take specific error messages from children, put them in this
model''s errors
ar.errors.each do |attr, msg|
errors.add(attr, msg)
end
end
end
end
The tricky part is errors.delete--the errors class doesn''t expose a
delete method, even though it uses a hash as an underlying data type,
which does have a delete method. So I created a file (lib/
error_delete.rb) with this in it to extend the Errors class and expose
it:
module ActiveRecord
class Errors
# add function to delete a particular error message
def delete(key)
@errors.delete(key.to_s)
end
end
end
Put in "require ''error_delete''" at the top of the
file where you call
the delete function. Hope that''s helpful!
On Jul 29, 9:49 pm, Usman Akram
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> > I was wondering the same thing...Have you found a solution?
>
> No man just waiting for some help. its not a bug the validation takes
> place only once I just dont know how to switch off the invalid record
> notification. u can see the custom validation messages are being sent
> out but also along with the invalid child validation . if you find
> something kindly inform
> Regards!
> Usman
>
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
thanx will check this tonight -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jul 30, 1:50 pm, walker <walker.hag...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I had to come up with a hack to get around this (duplicate and generic > validation error messages). I also wanted to include my specific error > messages from the child models. Basically, this is my custom > validator: > > #custom validator so i can make error messages appear the way i want > #(instead of validates_associated) > def valid_associated_records? > associated_records.each do |ar| > if(!ar.nil? && !ar.valid?) > #get rid of the default error message > errors.delete(:attribute_name) > > #take specific error messages from children, put them in this > model''s errors > ar.errors.each do |attr, msg| > errors.add(attr, msg) > end > end > end > end > > The tricky part is errors.delete--the errors class doesn''t expose a > delete method, even though it uses a hash as an underlying data type, > which does have a delete method. So I created a file (lib/ > error_delete.rb) with this in it to extend the Errors class and expose > it: > > module ActiveRecord > class Errors > # add function to delete a particular error message > def delete(key) > @errors.delete(key.to_s) > end > end > end > > Put in "require ''error_delete''" at the top of the file where you call > the delete function. Hope that''s helpful!I took a similar approach, not sure if it''s the best way. I added the delete method like you did for class Errors, then in my top level model (in my case it was a "Match" and the child models were "PlayerGame" objects ): def after_validation self.errors.delete(:player_games) player_games.each_with_index do |game, i| game.validate game.errors.each_full do |msg| self.errors.add_to_base "Player game record #{i+1} #{msg}" end end end It''s probably not the most performant way to do things but it works well. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann
2008-Nov-24 20:38 UTC
Re: :has_many :through form validation messages problem
I was just wondering where the "associated_records" comes from? Just tried your idea but got a: "undefined local variable or method `associated_records''" Where did you put "valid_associated_records?" exactly? Is it just a method you call from within the model''s "validate" function? thanks On Thu, Jul 31, 2008 at 3:50 AM, walker <walker.hagius-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I had to come up with a hack to get around this (duplicate and generic > validation error messages). I also wanted to include my specific error > messages from the child models. Basically, this is my custom > validator: > > #custom validator so i can make error messages appear the way i want > #(instead of validates_associated) > def valid_associated_records? > associated_records.each do |ar| > if(!ar.nil? && !ar.valid?) > #get rid of the default error message > errors.delete(:attribute_name) > > #take specific error messages from children, put them in this > model''s errors > ar.errors.each do |attr, msg| > errors.add(attr, msg) > end > end > end > end > > The tricky part is errors.delete--the errors class doesn''t expose a > delete method, even though it uses a hash as an underlying data type, > which does have a delete method. So I created a file (lib/ > error_delete.rb) with this in it to extend the Errors class and expose > it: > > module ActiveRecord > class Errors > # add function to delete a particular error message > def delete(key) > @errors.delete(key.to_s) > end > end > end > > Put in "require ''error_delete''" at the top of the file where you call > the delete function. Hope that''s helpful! > > > On Jul 29, 9:49 pm, Usman Akram <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> > I was wondering the same thing...Have you found a solution? >> >> No man just waiting for some help. its not a bug the validation takes >> place only once I just dont know how to switch off the invalid record >> notification. u can see the custom validation messages are being sent >> out but also along with the invalid child validation . if you find >> something kindly inform >> Regards! >> Usman >> >> -- >> Posted viahttp://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---