2

I have an "inputSecret" component and I need to enable both HTML native non null validation, as well as JSF validation. Or to switch both of them off based on a parameter.

As a start, I have the xmlns:h="http://xmlns.jcp.org/jsf/html and xmlns:f="http://xmlns.jcp.org/jsf/core namespaces imported

This code

<h:inputSecret required="#{passwordRequired}" >
</h:inputSecret>

will enable JSF validation if the passwordRequired parameter is 'true'. And I thought the "required" attrib would also translate into the final HTML to enable native HTML validation, but it does not. So then I used the passThroughAttribute to have the "required" attribute down into the HTML rendered component.

<h:inputSecret required="#{passwordRequired}" >
    <f:passThroughAttribute name="required" value="#{passwordRequired}" />
</h:inputSecret>

One problem is that the value of the passThroughAttribute does not really matter, I can set it to "required", "true", "false" or even empty string, the HTML native validation will just occur if I type in the passThroughAttribute tag as described above (again, regardless of value).

This would be fine for the use case I initially had, but there is also a situation in which the use case does not require the user to change the password, so I would like to be able to conditionally add that "required" attrib as a "passThroughAttribute" tag in the JSF code.

Something ala

<h:inputSecret required="#{passwordRequired}" >
    <if:condition value="#{passwordRequired}">
        <f:passThroughAttribute name="required" value="true" />
    </if>
</h:inputSecret>

For an UI component I would use the "render" attribute, but this is not the case. I simply want to be able to switch the inclusion of the "passthroughAttribute" on or off based on my parameter. I know it sounds like JSTL and "c:if" but that doesn't always work so I need something else.

Does anyone know of a method for me to accomplish what I need without some horrible hacks?

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
Andy David
  • 83
  • 5

1 Answers1

2

Use JSTL <c:if>:

<!-- xmlns:c="http://java.sun.com/jsp/jstl/core" -->

<h:inputSecret required="#{passwordRequired}" >
    <c:if test="#{passwordRequired}">
        <f:passThroughAttribute name="required" value="true" />
    </c:if>
</h:inputSecret>

As alternative, you could duplicate the code with a rendered condition:

<h:inputSecret required="#{passwordRequired}" rendered="#{passwordRequired}">
    <f:passThroughAttribute name="required" value="#{passwordRequired}" />
</h:inputSecret>
<h:inputSecret required="#{passwordRequired}" rendered="#{!passwordRequired}"/>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • As to your JSTL unawareness (based on original answer), carefully read http://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense – BalusC Jan 13 '15 at 14:36
  • @BalusC In my original answer, I wrote that I can't try it at the moment, so I can't tell for sure will it work or not. Thanks for the JSTL info, it was helpful. – Predrag Maric Jan 13 '15 at 15:18
  • @BalusC Thank you for yout input, I was trying to avoid JSTL exactly because I had skimmed through some posts that said JSF and JSTL don't mix well, but your post made more sense of that. The solution seems to work in this case however so I will consider the question solved. – Andy David Jan 14 '15 at 10:15