Dogfalo / materialize

Materialize, a CSS Framework based on Material Design
https://materializecss.com
MIT License
38.86k stars 4.74k forks source link

How to validate select input? #1861

Closed faizplus closed 7 years ago

faizplus commented 9 years ago

I just want to use HTML5 required attribute to make a select box required but it is not working in materializecss select. Please help!

It also raises error on console An invalid form control with name='name_of_input' is not focusable.

Dogfalo commented 9 years ago

This is how html works, you must have an empty value for the first option for required to work On Aug 6, 2015 5:18 AM, "Jimish Fotariya" notifications@github.com wrote:

I have faced same problem, and i resolved it by comparing select box value with its Label name in before submit function.

— Reply to this email directly or view it on GitHub https://github.com/Dogfalo/materialize/issues/1861#issuecomment-128345495 .

chi-bd commented 8 years ago

you must have an empty value for the first option

No, this is another problem. Please test this patch.

$(document).ready(function() {
    $("select").material_select();

    // for HTML5 "required" attribute
    $("select[required]").css({display: "inline", height: 0, padding: 0, width: 0});
}
rhuanbarreto commented 8 years ago

@chi-bd can you explain why this patch works?

chi-bd commented 8 years ago

(link to #2028) This is "Egg of Columbus". Meterial-select makes the original <select> DOM hidden("display: none"). On the other hand, validation message of HTML5 isn't output when the validation target DOM is hidden(at least IE & Chrome). To cope with both, the CSS of <select> will set to display by size "0".

TomFreudenberg commented 7 years ago

Hi @chi-bd thanks for post

I have made some improvements, now it look pretty nice to me.

    $('select[required]').css({
      display: 'inline',
      position: 'absolute',
      float: 'left',
      padding: 0,
      margin: 0,
      border: '1px solid rgba(255,255,255,0)',
      height: 0, 
      width: 0,
      top: '2em',
      left: '3em'
    });

Cheers Tom

brunakov commented 7 years ago

The patches provided leave a red dot under the select element on FF ESR 45.7.0 (Debian Jessie 8).

TomFreudenberg commented 7 years ago

Hi @atomtm

could you check if this happened at the forms on https://meetus.koella.com also?

I can't see any red dot so far but don't use Debian currently.

brunakov commented 7 years ago

@TomFreudenberg select perhaps you have to zoom a bit to clearly see it .

TomFreudenberg commented 7 years ago

@atomtm Yeah, see that ...

I guess this might be the "active" indicator from FF to show "you are on the field" and just catch some nasty pixels ...

Okay ... I can reproduce this on FF 50 on MacOS as well (not on Chrome nor Safari) ... Just click into the field "First Name" and "Shift-TAB" (use keyboard) to select previous field. Within this action ... there is also a dot ...

Seems to be on FF only

brunakov commented 7 years ago

@TomFreudenberg Opera leaves a small triangle. http://imgur.com/a/y6CNf.

TomFreudenberg commented 7 years ago

Hm ... so that needs more / better adjustment or fix ...

brunakov commented 7 years ago

If you come up with something else please let me know !

hylke94 commented 7 years ago

When does this come to the base code?

shanehoban commented 7 years ago

Adding opacity: 0 to Tom's fix gets rid of that red circle in FF. Edit: and it works as intended

$('select[required]').css({
      display: 'inline',
      position: 'absolute',
      float: 'left',
      padding: 0,
      margin: 0,
      border: '1px solid rgba(255,255,255,0)',
      height: 0, 
      width: 0,
      top: '2em',
      left: '3em',
      opacity: 0
    });
Dogfalo commented 7 years ago

fixed in 97c450b

mrdnote commented 7 years ago

I also had to add pointerEvents: 'none'; to the css override, else the old select was still clickable!

My workaround for the select validation: http://jsfiddle.net/b8frk03m/6/

AJLeonardi commented 6 years ago

This issue appears to be present in rc2.

When I add required to my select, I get this message In the console (same as the original issue report): An invalid form control with name='name_of_input' is not focusable.

The browser wants to add the validation message to the