1

I have a <p:selectOneMenu> with the required="#{variable == ''}" attribute. Furthermore in the same form I have a <h:commandButton>.

When pressing the button for the first time the selection field is not validated. Instead the field is validated on the second submission and validates that there is no selection.

So it seems that the expression in the required attribute does work, but never on the first submission of the form.

I tried with process="@this fieldId" but that didn't really help. Any suggestions ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Stephan
  • 696
  • 15
  • 37

1 Answers1

1

You're thus comparing to an empty string.

required="#{variable == ''}"

This will fail if the variable is actually null, as evident by below quick test.

System.out.println("".equals(null)); // false

That the second validation pass works can only mean that the variable represents the value of another input field, and that you relied on default (but IMO bad) behavior that empty input values end up as empty strings instead of nulls in the model.

Basically, you need this:

required="#{variable == null || variable == ''}"

But better make use of EL empty operator. It will check both nullness and emptiness:

required="#{empty variable}"

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Yeap, just noticed it that required="#{variable == '' || variable == null}" solves the problem. Thank you – Stephan Jul 10 '15 at 09:31