Help with validation

11 views
Skip to first unread message

Gm

unread,
Apr 25, 2015, 8:47:40 AM4/25/15
to rubyonra...@googlegroups.com
Hi,

I'm trying to validate an array attribute "accepts_nested_attributes_for".
I've created a method like this:

  validate :validate_nested_attributes

  def validate_nested_attributes
    items.each do |item|
     errors.add("item_#{item.number}_#{self.id}", "error on #{item.number}") if item.value.blank?
    end
  end

But this method validates all items of array.
I just want do display erros one at a time instead displaying all erros inside the form.

Tips on how to solve this problem ?

Thanks.

BuyzLots

unread,
Apr 25, 2015, 12:18:51 PM4/25/15
to rubyonra...@googlegroups.com
I don’t think you want your model validating another models attributes like this but to answer the question you could put in a return statement. 
return errors.add(…)


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/29e4caea-637f-4072-9c06-cb630361f268%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gm

unread,
Apr 25, 2015, 2:20:25 PM4/25/15
to rubyonra...@googlegroups.com
Thanks.
Reply all
Reply to author
Forward
0 new messages