Bug: when errorElement isn't label and name contains "[" "]" errorsFor doesn't return element (fix included) #1269

Closed
phazei opened this Issue Sep 12, 2014 · 1 comment

Comments

Projects
None yet
2 participants
@phazei

phazei commented Sep 12, 2014

If I have an element with this name and id:

<input id="user[name]" name="user[name]">

And I'm not using a "label" as the errorElement, then it doesn't find the existing errorElement and keeps on creating new ones.

The problem arises because the describer is set as the id, and then in the errorsFor function it uses that directly as the jquery selector. The brackets need to be escaped.

Fix:

Line 824 fix for errorsFor:

- selector = selector + ", #" + describer.replace( /\s+/g, ", #" );
+ selector = selector + ", #" + describer.replace( /\s+/g, ", #" ).replace( /(\[|\])/g, "\\$1");

Line 789 fix for describedBy not matching either and getting duplicated:

- } else if ( !describedBy.match( new RegExp( "\\b" + errorID + "\\b" ) ) ) {
+ } else if ( !describedBy.match( new RegExp( "\\b" + errorID.replace( /(\[|\])/g, "\\$1") + "\\b" ) ) ) {

That will properly escape the brackets.

Edit:
Alternatively, just changing the single line where the id is pulled fixes it as well
Line 785:

- errorID = error.attr( "id");
+ errorID = error.attr( "id").replace( /(\[|\])/g, "\\$1");

@phazei phazei changed the title from Bug: when errorElement isn't label and name contains "[" "]" errorsFor doesn't return element to Bug: when errorElement isn't label and name contains "[" "]" errorsFor doesn't return element (fix included) Sep 13, 2014

@vaibhavguptaIITD

This comment has been minimized.

Show comment
Hide comment
@vaibhavguptaIITD

vaibhavguptaIITD Sep 13, 2014

This issue will arise for ".", ":" as well. Please use the regex /(:|.|[|])/g.

This issue will arise for ".", ":" as well. Please use the regex /(:|.|[|])/g.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment