[jQuery] Re: [validate] Validation Rules based on IDs rather than Names

2008-06-26 Thread Simon Whatley

Hi Jörn

Firstly, the form I have to work with is not of my creation and is
somewhat flawed!

I agree, your first example would be invalid if the forms were on the
same page. If they were on separate pages the IDs are not invalid.

However my example is a form that collects a number of instances of
the same type of data. When the instance limit is reached, the user
progresses to the next stage. Take registering multiple delegates to a
conference as an example; you need to register the same type of detail
for each delegate. (Ideally a delegate number would be saved to the
database therefore negating this whole issue, but this is not the case
here and it does serve to illustrate a problematic area).

Example:
!-- page 1 --
form id=frm
input id=e1 name=e1_1 /
input id=e2 name=e2_1 /
/form

!-- page 2 --
form id=frm
input id=e1 name=e1_2 /
input id=e2 name=e2_2 /
/form

!-- page n --
form id=frm
input id=e1 name=e1_n /
input id=e2 name=e2_n /
/form

Where e1_1, e1_2 and e1_n refer to the same type of content, which can
be referenced with the ID, and likewise for e2 etc.

By refering to the name of the field for validation you are tightly
coupling your script to the name therefore a script would have to be
created for each iteration.

Your second example would pose a problem if you wanted to show
validation errors based on, for example, an individual checkbox chosen
within a given group (unless I am very much mistaken).

Furthermore, whether using a JavaScript framework or not, it is more
robust refering to an element by it's ID (i.e. getElementById or $
(#ID)). Infact, you do in certain cases use the ID as reference
(e.g. when refering to dependencies, for example #fieldID:checked).

If you could build in a solution that also allows a developer to
decide whether to refer to name or ID when creating rules/messages,
you will be able to serve everyones needs. I hope it isn't to
complicated to achieve :-/

Thanks,
Simon


On Jun 25, 7:53 pm, Jörn Zaefferer [EMAIL PROTECTED]
wrote:
 Let just get this straight, I consider this invalid and useless html:

 form
 input id=e1 name=e1_1 /
 input id=e2 name=e1_2 /
 /form
 form
 input id=e1 name=e2_1 /
 input id=e2 name=e2_2 /
 /form

 While this is valid and useful, and works fine with the validation plugin:

 form
 input id=e1_1 name=e1 /
 input id=e1_2 name=e2 /
 /form
 form
 input id=e2_1 name=e1 /
 input id=e2_2 name=e2 /
 /form

 So either of us is missing something, which we need to clear up at first.

 Let me know what you think, I sure hope to find a solution to this
 that works for both of us.

 Jörn

 On Wed, Jun 25, 2008 at 12:58 PM,SimonWhatley[EMAIL PROTECTED] wrote:

  Hi Jörn,

  IDs are unique to the page rather than across forms, names do not have
  to be. The forms I refer to are not on the same page, but on a new
  instance of the page when a user clicks the submit button. The rule
  should/could therefore apply to the ID and not the name, since the
  name can be dynamic.

  There are two ways I can get around this. Use a server-side scripting
  language to dynamically output the correct JavaScript and therefore
  rules and messages or hack the validation plugin. It's not great doing
  either.

  I will try looking at the plugin option since it is a more sensible
  approach. I would suggest that the plugin support element IDs as this
  is the only way you can guarantee a rule be applied to a specific
  element (assuming people adhere to the uniqueness of an ID). If you
  can somehow allow the plugin to be based on either name or ID, to be
  specified by the developer and defaulting to name, it will allow for
  backwards compatibility.

  If I get something working with element.id, I'll send the code over to
  you.

  Thanks,
 Simon

  On Jun 25, 11:24 am, Jörn Zaefferer [EMAIL PROTECTED]
  wrote:
  Repeating the same IDs across forms won't work, IDs have to be unique.
  That is why the plugin relies on names, as those have to be unique
  (from the plugin point of view) only within their form.

  You can try to hack the plugin, replacing all element.name snippets
  with element.id, but I won't support that.

  Jörn

  On Wed, Jun 25, 2008 at 12:10 PM,SimonWhatley[EMAIL PROTECTED] wrote:

   Hi Joern,

   I have come across an interesting problem regarding your jQuery
   Validation script that I am sure you already have a solution to, but I
   can't find it!

   I need the validation rules to look at the form input IDs rather than
   the form input names. I have a form that iterates 10 times (when you
   click next), so the the names of the inputs increment by 1 each time,
   whilst the IDs stay the same. (This is not my doing, so I have to live
   with the naming convention!)

   For example:

   Form1 has input names field1_1, field2_1, field3_1 and input ids
   field1, field2, field3
   Form2 has input names field1_2, field2_2, field3_2 and input ids
   field1, field2, field3

   I originally thought your jQuery Validation script looked at the IDs
   

[jQuery] Re: [validate] Validation Rules based on IDs rather than Names

2008-06-26 Thread Jörn Zaefferer

Previous version of the plugin used IDs solely, and there are way to
many drawbacks to give that another try. But I think that isn't even
necessary, there are other solutions.

To start with, you can add classes to inputs to define their
validation rules. If you have no control over the serverside, add the
classes before applying the validation on the clientside. So basically
just this:

$(#e1).addClass(required); $(#e2).addClass(required minlength);
$(#frm).validate();

The likely next stopgap would be custom messages for those fields.
I've recently added a chapter to the documentation for refactoring
rules and messages, which could help here:
http://docs.jquery.com/Plugins/Validation#Refactoring_rules

Let me know if that helps. Just keep in mind that specifiying rules
must be based on names or happen inline, IDs are not an option.

Jörn

On Thu, Jun 26, 2008 at 2:08 PM, Simon Whatley [EMAIL PROTECTED] wrote:

 Hi Jörn

 Firstly, the form I have to work with is not of my creation and is
 somewhat flawed!

 I agree, your first example would be invalid if the forms were on the
 same page. If they were on separate pages the IDs are not invalid.

 However my example is a form that collects a number of instances of
 the same type of data. When the instance limit is reached, the user
 progresses to the next stage. Take registering multiple delegates to a
 conference as an example; you need to register the same type of detail
 for each delegate. (Ideally a delegate number would be saved to the
 database therefore negating this whole issue, but this is not the case
 here and it does serve to illustrate a problematic area).

 Example:
 !-- page 1 --
 form id=frm
 input id=e1 name=e1_1 /
 input id=e2 name=e2_1 /
 /form

 !-- page 2 --
 form id=frm
 input id=e1 name=e1_2 /
 input id=e2 name=e2_2 /
 /form

 !-- page n --
 form id=frm
 input id=e1 name=e1_n /
 input id=e2 name=e2_n /
 /form

 Where e1_1, e1_2 and e1_n refer to the same type of content, which can
 be referenced with the ID, and likewise for e2 etc.

 By refering to the name of the field for validation you are tightly
 coupling your script to the name therefore a script would have to be
 created for each iteration.

 Your second example would pose a problem if you wanted to show
 validation errors based on, for example, an individual checkbox chosen
 within a given group (unless I am very much mistaken).

 Furthermore, whether using a JavaScript framework or not, it is more
 robust refering to an element by it's ID (i.e. getElementById or $
 (#ID)). Infact, you do in certain cases use the ID as reference
 (e.g. when refering to dependencies, for example #fieldID:checked).

 If you could build in a solution that also allows a developer to
 decide whether to refer to name or ID when creating rules/messages,
 you will be able to serve everyones needs. I hope it isn't to
 complicated to achieve :-/

 Thanks,
 Simon


 On Jun 25, 7:53 pm, Jörn Zaefferer [EMAIL PROTECTED]
 wrote:
 Let just get this straight, I consider this invalid and useless html:

 form
 input id=e1 name=e1_1 /
 input id=e2 name=e1_2 /
 /form
 form
 input id=e1 name=e2_1 /
 input id=e2 name=e2_2 /
 /form

 While this is valid and useful, and works fine with the validation plugin:

 form
 input id=e1_1 name=e1 /
 input id=e1_2 name=e2 /
 /form
 form
 input id=e2_1 name=e1 /
 input id=e2_2 name=e2 /
 /form

 So either of us is missing something, which we need to clear up at first.

 Let me know what you think, I sure hope to find a solution to this
 that works for both of us.

 Jörn

 On Wed, Jun 25, 2008 at 12:58 PM,SimonWhatley[EMAIL PROTECTED] wrote:

  Hi Jörn,

  IDs are unique to the page rather than across forms, names do not have
  to be. The forms I refer to are not on the same page, but on a new
  instance of the page when a user clicks the submit button. The rule
  should/could therefore apply to the ID and not the name, since the
  name can be dynamic.

  There are two ways I can get around this. Use a server-side scripting
  language to dynamically output the correct JavaScript and therefore
  rules and messages or hack the validation plugin. It's not great doing
  either.

  I will try looking at the plugin option since it is a more sensible
  approach. I would suggest that the plugin support element IDs as this
  is the only way you can guarantee a rule be applied to a specific
  element (assuming people adhere to the uniqueness of an ID). If you
  can somehow allow the plugin to be based on either name or ID, to be
  specified by the developer and defaulting to name, it will allow for
  backwards compatibility.

  If I get something working with element.id, I'll send the code over to
  you.

  Thanks,
 Simon

  On Jun 25, 11:24 am, Jörn Zaefferer [EMAIL PROTECTED]
  wrote:
  Repeating the same IDs across forms won't work, IDs have to be unique.
  That is why the plugin relies on names, as those have to be unique
  (from the plugin point of view) only within their form.

  

[jQuery] Re: [validate] Validation Rules based on IDs rather than Names

2008-06-25 Thread Jörn Zaefferer

Repeating the same IDs across forms won't work, IDs have to be unique.
That is why the plugin relies on names, as those have to be unique
(from the plugin point of view) only within their form.

You can try to hack the plugin, replacing all element.name snippets
with element.id, but I won't support that.

Jörn

On Wed, Jun 25, 2008 at 12:10 PM, Simon Whatley [EMAIL PROTECTED] wrote:

 Hi Joern,

 I have come across an interesting problem regarding your jQuery
 Validation script that I am sure you already have a solution to, but I
 can't find it!

 I need the validation rules to look at the form input IDs rather than
 the form input names. I have a form that iterates 10 times (when you
 click next), so the the names of the inputs increment by 1 each time,
 whilst the IDs stay the same. (This is not my doing, so I have to live
 with the naming convention!)

 For example:

 Form1 has input names field1_1, field2_1, field3_1 and input ids
 field1, field2, field3
 Form2 has input names field1_2, field2_2, field3_2 and input ids
 field1, field2, field3

 I originally thought your jQuery Validation script looked at the IDs
 on the page, so the same validation rules would apply for field1
 whether on form1 or form2. However this is not the case.

 How do I change your script to look at the ID?

 Many thanks in advance for your help.

 Simon



[jQuery] Re: [validate] Validation Rules based on IDs rather than Names

2008-06-25 Thread Simon Whatley

Hi Jörn,

Element IDs are unique to a page, whereas names are not. By this very
fact, it is problematic applying validation rules to a specific
element if that element's name exists multiple times (as will often be
the case, especially with checkboxes, radio buttons and mutliple-
selects).

The problem I have encountered doesn't refer to multiple forms on a
page, but rather multiple iterations of the form across pages, i.e.
when a user submits the form. The validation rules and messages are
the same for each form so the form's input IDs should be used as the
reference.

E.g. This is valid for two distinct pages with identical forms
Form1 has input IDs field1, field2, field3
Form2 has input IDs field1, field2, field3

Where field1, field..n IDs could be referenced by your validation
script and re-used for each form.

However, since my form has dynamic names, I would need to create
multiple iterations of the validation script, since after the first
iteration of the form, the names of the form fields are changed (with
a numeric increment).

There are two solutions to this problem, both of which are far from
ideal:

1. Dynamically create the JavaScript used to validate a page using a
server-side script
2. As you suggest, hack your plugin to use element IDs rather than
names.

I've decided the latter is out of the question since it is your plugin
and as soon as an upgrade is available, I will not be able to use it.
The former will have to be used for the time-being.

I would kindly suggest that you consider implementing IDs as
references in your plugin. This could be an attribute a developer has
to implement in their script, the default being name, the ideal
scenario being ID.

Since IDs by their nature are unique this will allow users of your
plugin to clearly identify which element the rule is acting upon.

Many thanks,
Simon.


On Jun 25, 11:24 am, Jörn Zaefferer [EMAIL PROTECTED]
wrote:
 Repeating the same IDs across forms won't work, IDs have to be unique.
 That is why the plugin relies on names, as those have to be unique
 (from the plugin point of view) only within their form.

 You can try to hack the plugin, replacing all element.name snippets
 with element.id, but I won't support that.

 Jörn

 On Wed, Jun 25, 2008 at 12:10 PM, Simon Whatley [EMAIL PROTECTED] wrote:

  Hi Joern,

  I have come across an interesting problem regarding your jQuery
  Validation script that I am sure you already have a solution to, but I
  can't find it!

  I need the validation rules to look at the form input IDs rather than
  the form input names. I have a form that iterates 10 times (when you
  click next), so the the names of the inputs increment by 1 each time,
  whilst the IDs stay the same. (This is not my doing, so I have to live
  with the naming convention!)

  For example:

  Form1 has input names field1_1, field2_1, field3_1 and input ids
  field1, field2, field3
  Form2 has input names field1_2, field2_2, field3_2 and input ids
  field1, field2, field3

  I originally thought your jQuery Validation script looked at the IDs
  on the page, so the same validation rules would apply for field1
  whether on form1 or form2. However this is not the case.

  How do I change your script to look at the ID?

  Many thanks in advance for your help.

  Simon


[jQuery] Re: [validate] Validation Rules based on IDs rather than Names

2008-06-25 Thread Simon Whatley

Hi Jörn,

IDs are unique to the page rather than across forms, names do not have
to be. The forms I refer to are not on the same page, but on a new
instance of the page when a user clicks the submit button. The rule
should/could therefore apply to the ID and not the name, since the
name can be dynamic.

There are two ways I can get around this. Use a server-side scripting
language to dynamically output the correct JavaScript and therefore
rules and messages or hack the validation plugin. It's not great doing
either.

I will try looking at the plugin option since it is a more sensible
approach. I would suggest that the plugin support element IDs as this
is the only way you can guarantee a rule be applied to a specific
element (assuming people adhere to the uniqueness of an ID). If you
can somehow allow the plugin to be based on either name or ID, to be
specified by the developer and defaulting to name, it will allow for
backwards compatibility.

If I get something working with element.id, I'll send the code over to
you.

Thanks,
Simon

On Jun 25, 11:24 am, Jörn Zaefferer [EMAIL PROTECTED]
wrote:
 Repeating the same IDs across forms won't work, IDs have to be unique.
 That is why the plugin relies on names, as those have to be unique
 (from the plugin point of view) only within their form.

 You can try to hack the plugin, replacing all element.name snippets
 with element.id, but I won't support that.

 Jörn

 On Wed, Jun 25, 2008 at 12:10 PM, Simon Whatley [EMAIL PROTECTED] wrote:

  Hi Joern,

  I have come across an interesting problem regarding your jQuery
  Validation script that I am sure you already have a solution to, but I
  can't find it!

  I need the validation rules to look at the form input IDs rather than
  the form input names. I have a form that iterates 10 times (when you
  click next), so the the names of the inputs increment by 1 each time,
  whilst the IDs stay the same. (This is not my doing, so I have to live
  with the naming convention!)

  For example:

  Form1 has input names field1_1, field2_1, field3_1 and input ids
  field1, field2, field3
  Form2 has input names field1_2, field2_2, field3_2 and input ids
  field1, field2, field3

  I originally thought your jQuery Validation script looked at the IDs
  on the page, so the same validation rules would apply for field1
  whether on form1 or form2. However this is not the case.

  How do I change your script to look at the ID?

  Many thanks in advance for your help.

  Simon


[jQuery] Re: [validate] Validation Rules based on IDs rather than Names

2008-06-25 Thread Jörn Zaefferer

Let just get this straight, I consider this invalid and useless html:

form
input id=e1 name=e1_1 /
input id=e2 name=e1_2 /
/form
form
input id=e1 name=e2_1 /
input id=e2 name=e2_2 /
/form

While this is valid and useful, and works fine with the validation plugin:

form
input id=e1_1 name=e1 /
input id=e1_2 name=e2 /
/form
form
input id=e2_1 name=e1 /
input id=e2_2 name=e2 /
/form

So either of us is missing something, which we need to clear up at first.

Let me know what you think, I sure hope to find a solution to this
that works for both of us.

Jörn

On Wed, Jun 25, 2008 at 12:58 PM, Simon Whatley [EMAIL PROTECTED] wrote:

 Hi Jörn,

 IDs are unique to the page rather than across forms, names do not have
 to be. The forms I refer to are not on the same page, but on a new
 instance of the page when a user clicks the submit button. The rule
 should/could therefore apply to the ID and not the name, since the
 name can be dynamic.

 There are two ways I can get around this. Use a server-side scripting
 language to dynamically output the correct JavaScript and therefore
 rules and messages or hack the validation plugin. It's not great doing
 either.

 I will try looking at the plugin option since it is a more sensible
 approach. I would suggest that the plugin support element IDs as this
 is the only way you can guarantee a rule be applied to a specific
 element (assuming people adhere to the uniqueness of an ID). If you
 can somehow allow the plugin to be based on either name or ID, to be
 specified by the developer and defaulting to name, it will allow for
 backwards compatibility.

 If I get something working with element.id, I'll send the code over to
 you.

 Thanks,
 Simon

 On Jun 25, 11:24 am, Jörn Zaefferer [EMAIL PROTECTED]
 wrote:
 Repeating the same IDs across forms won't work, IDs have to be unique.
 That is why the plugin relies on names, as those have to be unique
 (from the plugin point of view) only within their form.

 You can try to hack the plugin, replacing all element.name snippets
 with element.id, but I won't support that.

 Jörn

 On Wed, Jun 25, 2008 at 12:10 PM, Simon Whatley [EMAIL PROTECTED] wrote:

  Hi Joern,

  I have come across an interesting problem regarding your jQuery
  Validation script that I am sure you already have a solution to, but I
  can't find it!

  I need the validation rules to look at the form input IDs rather than
  the form input names. I have a form that iterates 10 times (when you
  click next), so the the names of the inputs increment by 1 each time,
  whilst the IDs stay the same. (This is not my doing, so I have to live
  with the naming convention!)

  For example:

  Form1 has input names field1_1, field2_1, field3_1 and input ids
  field1, field2, field3
  Form2 has input names field1_2, field2_2, field3_2 and input ids
  field1, field2, field3

  I originally thought your jQuery Validation script looked at the IDs
  on the page, so the same validation rules would apply for field1
  whether on form1 or form2. However this is not the case.

  How do I change your script to look at the ID?

  Many thanks in advance for your help.

  Simon