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

Most upvoted comments

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:

.form-control.select2-hidden-accessible {
    top: 30px;
}

( 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() the select, but add the following css:

position: absolute;
visibility: hidden;

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 select2 CSS

.select2-offscreen,
.select2-offscreen:focus {
  // Keep original element in the same spot
  // So that HTML5 valiation message appear in the correct position
  left: auto !important;
  top: auto !important;
}

Css hack solution that worked for me if someone still need it:

select.use-select-2.select2-attach-processed.required {
    display: block;
    position: absolute;
    height: 0px;
    pointer-events: none;
    border-color: transparent;
    background: transparent;
    appearance: none;
}

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:

.select2-container + select {
    position:absolute;
    display: block !important;
    visibility: hidden;
    height: 1px;
    margin-left: 10px;
    margin-top: -14px;
}
<div style="position:relative;"><select required="required"></select></div>

.select2-offscreen[required],
.select2-offscreen[required]:focus {
    width: auto !important;
    height: auto !important;
}

If the select element is contained within a div with a style of position relative works well.

<div class="contenedor-select2"><select required="required">...</select></div>

Styles
.select2-offscreen,
.select2-offscreen:focus {
    clip: rect(0 0 0 0) !important;
/*    The message is squashed and not seen.*/
/*    width: 1px !important;*/
/*    height: 1px !important;*/
    border: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
    position: absolute !important;
    outline: 0 !important;
    left: 0px !important;
    top: 0px !important;
}
.contenedor-select2 {
position: relative;
}

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 😉