-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
html5 required attribute support? #515
Comments
I've also just recently noticed this issue. "A validation message consists of four div elements with pseudo classes and some nodes for message text. You can customize the appearance by changing the style of these pseudo classes." I'm thinking the best way to deal with this is to have chosen generate the additional markup to their rendered select with similar classes so that they will be styled by default in the browser, or potentially append pseudo elements to elements that have failed validation by using the appropriate failed conditions in CSS like :invalid. For now, it looks like your only option is to have a DOM-level fallback like my javascript solution. I can provide it here if that would be helpful but it's really a fallback to chosen, not a way to fix the chosen framework. |
What's the status on this issue ? I have the exact same problem with the last version of Firefox ( 14.0.1 ) Since HTML5 attributes are more and more used in websites, this should be adressed a way or the other. |
I have the same issue too. I would be curious to know your fallback solution. Thanks |
It looks like there is only 1 major problem, and a secondary problem that would be great to fix.
The first item is the more problematic one. Could this be fixed by utilizing a different select element hiding method? Replacing "display:none" on the hidden select element with "position: absolute;visibility: hidden;" gets pretty close - though that'll be more dependent on surrounding CSS... |
It doesn't work for Webkit though. It seems the error flag inherits the styles of the select box in webkit (Chrome), so if you set opacity: .5, the flag will also be opacity .5 (as one example). bummer. |
One more - Opera works correctly as is, but if I change it to the above position based CSS, then it doesn't work right. What a pain. |
Sorry for spamming this up. I think I got it though! If you wrap the select element in a span, and use this CSS on the wrapper (and remove the display:none on the select element): width: 0px;
height: 0px;
overflow: hidden;
display: inline-block;
position: absolute; You will get the error message and no break the page flow at the same time. The only problem is you have to leave the box visible in order to see the error message because Firefox and WebKit will hide the message if you don't. This causes the select box to receive tabs through the form. The way around that might be to toggle display from none to inline-block on submit (or the 'invalid' event), then toggle it back on certain input events (like mouse down or keypress). There is also a small positioning issue, resolvable by forcing the position to be directly under the chosen box. |
Would be nice to see some kind of built-in support for this, if possible at all. |
I wrote a patch here: https://github.com/adcSTUDIO/chosen It may not be robust enough for inclusion, but feel free to test it out. |
Thanks @CaptainN! I'll check it out! |
@CaptainN, I got it working - thanks for making those changes. However, do you know how to allow the width of the error popups to exceed the width of the select boxes themselves? On some of my smaller select elements, the error message is really thin which doesn't exactly look very good, and can even be hard to read on some. If not, that's ok, you've already helped a ton with this. Thanks again! |
@caderade I'm not sure how to fix that problem. I tried overflow:visible and a couple of other things, and it still shows up short I'll try a few more things, but this looks like a problem with Firefox that may not have a workaround (these flags are generally pretty hard to deal with, along with that glow, which I haven't yet found a way to style completely). |
@CaptainN: Very nice. It works pretty well for me in Firefox 17 and Chrome 23. In IE 8+9 (which don't support HTML5 form validation at all), when changing the value of the field, I get a JavaScript error because of the unknown |
Oh bother. I actually figure jQuery would protect me more! I'll have to add an IE check or something... Kevin N. |
@CaptainN It has the issue for me in Chrome as well. Wasn't sure if you thought it was a Firefox issue only or not. But dude, thanks a ton again man. Awesome work! Let us know if you are ever able to address the width thing though. |
It looks like jQuery has some built in pseudo selector support, and a hooks system so plugins can add their own, and if it doesn't find any, it passes it on to the underlying implementation - so :required and :valid throw an error in IE (and probably some versions of Safari) if they haven't been polyfilled. I'll see if I can throw an alternative kind of check in there, instead of using the pseudo selectors. It looks like I can just check attr("required") to replace :required - I'll probably have to do some kind of feature detect for :valid though - maybe a check for a .oninvalid property? |
Ok, I got a new patch up that fixes the IE error (and should fix it in any other browsers too). This patch changes a few things (nothing you should notice). Here are my notes:
|
How do I switch the version using npm? |
@CaptainN |
I was able to build the uglified versions on my work mac (maybe it has an older version of uglify-js?). They are now up to date in my repo. |
I'm only using the jQuery flavor currently. No script error in IE8/9 anymore. 👍 for seeing this feature in upstream soon. |
We are also facing this issue. Is there a workaround except disabling HTML5 validation? |
I thought I fixed that. That fork was a while ago, and may need to be updated. I'll see if I can get to that this week, and look into the IE bug. |
I synced this upstream, and I didn't see any undefined error notices in IE. Try my repo again, and let me know if you still see this problem. (sorry it took so long) Thanks! |
👍 |
Could someone sum this up for me? Is there a fork or a patch around that is ready for production? |
I had to modify @ghiculescu 's script so I could call chosen twice as such:
Otherwise, 2nd trip removed the style that was added in the first:
I just added this extra condition to the 1st if: |
@doganmeh Works great in Chrome and mobile (with Chrome) but tested in Firefox 53.0 where it does not work |
Even this http://jsfiddle.net/hq7b426j/1/ does not work in Firefox 53.0 |
Hmmm, but this seems more like a Firefox problem because if you click on the button more times, you will see that Firefox tries to display the message (but such is never rendered completely) |
I've used this solution. It is very simple and works perfect:
|
it seems that this doesn't work if you have chosen with multiple selections : |
I fixed it. ` if (is_creating_chosen && select.css('position') === 'absolute') { var ret = select.oldChosen(options) // only act if the select has display: none, otherwise chosen is unsupported (iPhone, etc)
select.attr('tabindex', -2); } |
+1 Can somebody commit a fix for this? It's pretty old issue and very important functionality nowadays. Thank you. |
Not working on latest version. |
This seems to be breaking angular.js form validation when using angular-chosen too. |
It's 2018 and we're still having the same issue. It's a hell how HTML standards cannot work properly and integrated with external plugins. Crazy times :) |
This is breaking Drupal's chosen plugin as well. See https://www.drupal.org/project/chosen/issues/2705891 |
Couldn't find any proper solution to this other than a workaround: <div class="form__select">
<select class="chosen">
<!-- options -->
</select>
</div> .form__select {
position: relative;
}
.form__select .chosen {
display: block !important;
height: 0;
position: absolute;
left: 35px;
bottom: 0;
outline: none;
border-color: white;
pointer-events: none;
} |
If you're using Chosen for all These CSS selectors target invalid select elements, with one of them targeting select:invalid {
height: 0px !important;
opacity: 0 !important;
position: absolute !important;
display: flex !important;
}
select:invalid[multiple] {
margin-top: 15px !important;
} Demo: http://jsfiddle.net/tripflex/2zdeu9oc/ BUT REALLY ... this should already be handled by the Chosen.JS lib |
Here's my workaround, if it's of any use:
|
@jonathanbull can you show it within context? I tried your solution and it didn't work. |
@AndrewSouthpaw This is my code, fixs works fine for me :)
|
I'm using a slight different code (based on the code here): $('.chosen-select').on("chosen:ready", function (evt, data) {
$(this)
.addClass('chosen_hidden')
.attr('tabindex', '-1')
.prependTo(data.chosen.container)
.show()
})
.chosen({width: '100%'}) .chosen_hidden {
position: absolute;
top: 0;
bottom: 0;
max-height: 100%; // required for IE 10
// **not required** opacity: 0;
} It does not handle the 'destroy' action, but it does position the error message nicely and seems to work on Firefox and Chromiun. As the original selector is not hidden, the error highlight border is visible. Update: with |
There has been an open PR that resolves this, but I think it got lost since the other thread it targeted was closed. See #2594 |
@jhedstrom would love to see that merged. Do you know if this library is still maintained? |
I used something similar to @eloyesp's solution above and shared that here: Thanks @jhedstrom for pointing me in the right direction! |
The HTML5 required attribute does not work on chosen selects. This is a known bug since 2012: harvesthq/chosen#515 The chosen project is dead, and all chosen selects will be replaced when we upgrade bootstrap. For now, this fix works just as well in the self registration form.
When chosen is initialized on a select input, it adds [style="display:none"] to the select tag and creates the various div tags it needs, as follows.
This is fine, but we have a problem if I add the html5 'required' attribute to my select tag, which produces the following markup after chosen initializes:
The problem occurs when the user submits the form without selecting a value. To the user, the browser seems to do nothing. In fact, it's displaying an error message to the user to select a value, but because the CSS instructs the browser not to show the select element, the error message also doesn't appear.
This is happening in Chrome, I'm not sure about the behaviour of other browsers.
I haven't dug deep enough to suggest a solution, but this is an issue as we start to include html5 attributes in our forms.
The text was updated successfully, but these errors were encountered: