0
votes

I'm using the jquery validation plugin to validate a form for my rails application. The form allows the user to add items dynamically to the form's main object's has_many relationship. My problem is, how can I validate each of those items using the jQuery validation plugin. For instance a description text field for the associated object.

The field's names in the form that are dynamically added look like: project[tickets_attributes][0][description]

where project is the main object, tickets_attributes are the attributes association with my has_many association, the 0 (or association object id) unique and description is the name of the attribute in my has_many association.

2

2 Answers

0
votes

I think what you are looking for is .delegate(), this will let you attach event handlers to any items that maybe dynamically generated. in my example below, any item with the class "dynamic-item"

example something like

$form.delegate(".dynamic-item", "onChange", validate)

this will fire off the validate function anytime one of your dynamic elements is changed, and you can use this to call the validator

0
votes

when you add the event on click button "send" for example you need to use .live() instead of .click() or .bind().

.live() will accept all your dynamic inputs.

Update

.live() is now deprecated as of jQuery 1.7, and has been removed on 1.9. Use .on() instead.