开发者

Does required on selectOneMenu only accept true?

I have this selectOneMenu that asks for something, the default value is empty, because people need to think about the question and actually needs to fill it in.

<h:selectOneMenu id="fileSecurity" value="#{flowScope.application.fileSecurity}" required="true" >  
    <f:selectItem itemLabel="" itemValue="" noSelectionOption="true"/> 开发者_如何学编程 
    <f:selectItem itemLabel="ja" itemValue="true" />  
    <f:selectItem itemLabel="nee" itemValue="false" />     
</h:selectOneMenu>

This is what i have, now when you fill in the empty value it gives: "managementTabs:ApplicationManagementForm:fileSecurity : Needs to be filled in." wich is correct.

When i click yes or the true value it accepts the anwser.

But when i click no or false it gives: "managementTabs:ApplicationManagementForm:fileSecurity: Validation Error: Value is not valid"

  • Why does Jsf2.0 only accept true as the valid awnser and not false?
  • Is there a way around this? Or do i have to write a custom validator?


It seems that a submitted value of false is conflicting with the empty string value of noSelectionOption in some way. The following entries for the 1st item works fine:

<f:selectItem itemLabel="" itemValue="" />  

and

<f:selectItem itemLabel="" noSelectionOption="true" />

and

<f:selectItem itemLabel="" itemValue="#{null}" noSelectionOption="true" />

I only don't understand why. It's illogical. To find the real answer, I would need to run the debugger.


Update: I debugged and found the culprit in the SelectUtils#valueIsNoSelectionOption() (Mojarra). The empty string of the noSelectionOption get coerced to boolean false instead of Boolean null and hence the comparison with the submitted value is valid and hence the selected value is invalid. This is "by design". You really need to explicitly specify an itemValue="#{null}".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜