2

I've been working with Validation for a couple days, and I've learned that the main attribute it is validating on is the name = "" attribute in the tag. I like the plugin fine and would like to be able to use it for my purpose.

My problem stems from having non-unique name attributes in my form. When Validation uses a name attribute to set rules, and if there are multiple elements with that name, it just uses the first of the set. There are some other things it does with default settings such as onblur = true which will make it look at other elements in the matched set once you start clicking around in the form.

I'm using Rails 3 to generate my form, and the number of form items depends on an array of model objects. The Rails line that generates my form fields is:

<%= f.text_field :forwho, :size => 15, :title => "Enter person's name", :id => "referrees_#{j}_forwho" %>  

j is a counter which sets all the ids to be unique. I added a prefix "referrees[]" before this call. This is to create an array in the params hash sent to the controller on submit. The rendered html from the above line is:

<input type="text" title="Enter person's name" size="15" name="referrees[][forwho]" id="referrees_0_forwho">  

Notice the name attribute. The number of such fields I get depends on the number of model objects sent to the view from the controller.

I've tried using the .rules method for the Validation plugin like so:

$("#referrees_0_forwho").rules("add", {required: true});

but it seems to be using this to then depend on the name="referrees[][forwho]" field for the element with that id, thus still not working for me.

My goal is to ignore the name attributes entirely (as they are not unique) and use only the IDs. Can I do this, or is there a better way to solve my problem?

EDIT: According to the plugin author, Jorn, the plugin absolutely requires that name attributes be unique: Jorn's email to another person with same problem Found that in this question: Using JQuery Validate Plugin to validate multiple form fields with identical names

I guess I will work on making Rails give me unique names, or some sort of JQuery fixup hack once the DOM is loaded.

Community
  • 1
  • 1
Bodhi
  • 1,437
  • 1
  • 13
  • 21

0 Answers0