select2: Original select has 'required' attribute, doesn't work fine
Hello,
I think this is the better plugin I’ve ever used 😉
My question is related to the ‘required’ attribute.
let me explain better I have a html page like this:
<form name="myForm" action="myAction" >
<select id="myId" name="myName" required="true" >
<option value="myvalue">An Element</option>
</select>
<button type="submit" >Go</button>
</form>
<script>
$("#myId").select2({placeholder: "Select"});
</script>
When I click on the submit button of the form containing the select above, I see the internal browser’s message “Please select an element from the list”. That’s right, because I want browser shows the message.
The problem is that the message is not correctly shown, maybe caused by “display: none” of the original select when I do: $(“#myId”).select2()
Is there a way to avoid this annoyance?
thanks in advance and good job again 😉
About this issue
- Original URL
- State: closed
- Created 12 years ago
- Reactions: 8
- Comments: 61 (12 by maintainers)
Commits related to this issue
- HTML5 required validation Fix #128 and #1699 Places the tooltip at the right position. Tested for IE11, FF and Chrome — committed to fdaugan/select2 by fdaugan 8 years ago
- HTML5 required validation (JS) Fix #128 and #1699 Transfert the required property to the Select2 input. Tested for IE11, FF and Chrome — committed to fdaugan/select2 by fdaugan 8 years ago
- Merge pull request #128 from nilbus/contributing Convert a code block of non-code into the bullet list it wanted to be — committed to Gataniel/select2 by simi 9 years ago
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Finally I could solve it adding the following css:
( I’m using select2 v4.0.3 with select2-bootstrap-theme v0.1.0-beta.9 )
😃
Thank you @epineda . Nice solution!
I’ve a really easy fix (using it with another plugin, but should work here too): do not
hide()theselect, but add the following css:This way it should show error bubbles at the right place
this my solution for version 4.0.3
$("#select2Element").select2().prop('required', true).trigger('change');I just override
select2CSSCss hack solution that worked for me if someone still need it:
Just so for people to understand this problem better with a fiddle!
This seems to have been fixed in version 3.5.4 in the sense an error message is shown. If you still want to reproduce the same problem in the version of select2 where this bug was prevalent. Here are the fiddles.
Problematic version : https://jsfiddle.net/bragboy/55okbL58/ @PikachuEXE’s patch (overriden styles): https://jsfiddle.net/bragboy/55okbL58/1/
This is my solution:
If the select element is contained within a div with a style of position relative works well.
If select2 introduces the select element within the div to it, it puts the div style position relative, it will work well. It works well in Firefox and Google Chrome, did not try it in Internet Explorer. Excuse my English.
Thanks! unfortunately I don’t use any jquery plugin for validating my fields but only the
required="true"html attribute in my html page.Thanks again 😉