4

I am new to JSF and I came across the noSelectionOption attribute in JSF 2.0.

I don't understand the purpose of this attribute. As per the description, it's used when the selection is required and the user selects noSelectionOption causing a validation error.

So, if noSelectionOption = true then the user can select noSelectionOption and bypass that list or menu?

Or, if noSelectionOption = true then the user has to select one of the items, and, if he chooses noSelectionOption then the validation error occurs?

Can the user see noSelectionOption as one of the items under the List or menu if it's true?

Please help me to understand the logic behind this.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
devOpsTools
  • 190
  • 1
  • 1
  • 12

1 Answers1

9

A f:selectItem that has noSelectOption set to true represents a "no selection" option, something like this:

-- Select a Colour -- < noSelectOption was intended for this case
Red
Green
Blue
Tomato

This item is rendered in the menu, unless hideNoSelectionOption is set to true in your menu component. In that case, the option is selected when the user interacts with the menu.

Just bear in mind that if an entry is required and this "no selection" option is the one selected, there will be a validation error.

An alternative that requires a little more of coding is to use a f:selectItem with value="#{null}", to represent the case in which an user did not select a value. If you have a converter you'll have to check for this null case and, if you feel like it, introduce some custom validators.

Marco Sulla
  • 15,299
  • 14
  • 65
  • 100
Fritz
  • 9,987
  • 4
  • 30
  • 49
  • How to change the error message for the validation error? requiredMessage or validatorMessage doesn't change it. – user1643352 Aug 07 '13 at 09:52
  • @user1643352 Depends on which kind of validation you're triggering. If it's just a `required="true"` validation, you can check [this](http://www.mkyong.com/jsf2/customize-validation-error-message-in-jsf-2-0/) out but be warned, it will change the message for all of the `required` validations. You can, on the other hand, register a custom validator and assign your own messages. Depends on the kind of validation you're using. – Fritz Aug 07 '13 at 14:38
  • Sorry, I have not understood why `value="#{null}"` is more complicated. Just set `required="true"`. I continue to not understand the purpose of `noSelectionOption` – Marco Sulla Jul 28 '21 at 09:08