Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when form name with [] array #3618

Closed
nurmanhabib opened this issue Jul 28, 2015 · 7 comments
Closed

Error when form name with [] array #3618

nurmanhabib opened this issue Jul 28, 2015 · 7 comments

Comments

@nurmanhabib
Copy link

For example I created a form select using [] array.

<select name="kunjungan[temp-0][details][1][details]" class="form-control select2">
    <option value="somevalue">Some Value</option>
    <option value="othervalue">OtherValue</option>
</select>

Then I initialization select()

$('.select2').select()

then when I observed on the web browser console, an error occurs.

SyntaxError: invalid range in character class

I tried to eliminate the name without the array. The results are normal.

<select name="kunjungan_temp-0_details_1_details" class="form-control select2">
    <option value="somevalue">Some Value</option>
    <option value="othervalue">OtherValue</option>
</select>
@kevin-brown
Copy link
Member

Can you expand upon the issue you are facing by adding a few more pieces of information to your bug report?

  • What steps can be used to reproduce the issue?
  • What is the bug and what is the expected outcome?
  • What browser(s) and Operating System have you tested with?
  • Does the bug happen consistently across all tested browsers?
  • What version of jQuery are you using? And what version of Select2?
  • Are you using Select2 with other plugins?

The contributing guide contains a useful section with a few questions which can help us track down the bug and speed up the process of finding a fix for it. The creation of an isolated test case would also be useful.

@nurmanhabib
Copy link
Author

Like the example I reported above, the actual plugin select2() is running normally.

<select name="kunjungan[temp-0][details][1][details]" class="form-control select2">

Bug:
select2 selector can not seem parsing dashes in "temp-0", because I've just tried without dashes kunjungan[temp0][details][1][details] running normally

I've tried with Mozilla Firefox 39.0

I also use some plugins, such as TinyMCE, typeahead, and others.

jQuery v2.1.4
Select2 4.0.0

@Max-Might
Copy link

I get the same error with Select2 4.0.1 and jQuery.min.js 2.1.4
The error happens always when selecting a value from a drop-down.

Tested with browsers Firefox 42 and Opera 34.

Edit:
Opera gives a bit more information about the error than firefox.

Uncaught SyntaxError: Invalid regular expression: /(^|\.)select2\.(?:.*\.|)select2-input-params-data-source[show-issues]-g0(\.|$)/: Range out of order in character class

@Max-Might
Copy link

select2 selector can not seem parsing dashes in "temp-0"

Yeah, this seems to be the problem.
IMO this issue is quite critical because it prevents people from using select2.

@semmais
Copy link

semmais commented Jan 4, 2016

there is also a problem when in the name there is a "-" is followed by a number, inside brackets.

name="group[guid-xp]" - ok
name="group[guid-2]" - error
name="group[id-200]" - error

example: https://jsfiddle.net/ogg1o102/

@bnjmnhndrsn
Copy link
Contributor

I ran into this issue in our own code. It actually appears to be the result of a more general bug relating to any time square bracket appears in an attribute name.

The problem is that generateId takes the name attribute directly and then uses it for namespacing events, e.g. here. It looks like jQuery passes the namespace strings directly into a RegExp constructor when matching events and so the square brackets get treated as regex square brackets.

This results in the errors above because the namespace is a malformed regex. It also causes other bugs any time a square bracket appears in a name. Because jQuery is interpreting the square brackets as regex square brackets, it can't correctly unbind events attached when there is a square bracket in the event namespace. Look at this fiddle for example. In fact, I encountered this bug for that reason. A scroll handler wasn't getting correctly unbound when the select2 container closed.

Because the bug is the result of _generateId, a workaround in the short term is adding an id attribute to the select in question. I'd be happy to submit a pull request, too, to fix the bug in the next few days when I have the time.

@kevin-brown
Copy link
Member

I think escaping the name/id in _generateId should fix the issue in all cases. We create all ids for other elements based on the result of _generateId, so any issues in elements down the line should also be fixed by escaping selectors there.

@kevin-brown kevin-brown added this to the 4.0.2 milestone Jan 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants