# validates_as_attachment
#validates_presence_of :value, :if=>:is_required?
def is_required?
custom_field.required?
end
def validate
errors.add_to_base "#{custom_field.name} is required" if value.blank?
and is_required?
end
end
im showing errors like this
<%= error_messages_for(:object=>[@listing.lvalues, @listing]) %>
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.
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?
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
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
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.