Search code examples
javajsfseamajax4jsf

Conditionally required jsf validation in a4j form


I'm having a little problem with using conditionally evaluated expression in jsf/a4j

Here's my code

<a4j:form>
         <h:inputText id="id1" value="#{mybean.myvalue}" size="1"
                   required="#{not mybean.condition}"
                   rendered="#{not mybean.condition}"
                   requiredMessage="Put a number in here" />

           <h:selectBooleanCheckbox value="#{mybean.condition}">
                <a4j:support event="onclick" reRender="id1"/>
            </h:selectBooleanCheckbox>

<a4j:commandButton action="#{mybean.myaction}" value="Do something" />

 </a4j:form>

The boolean checkbox conditionally enable/disable the validation.

This doesn't work: the a4j:commandButton simply skip the validation.

Thanks.


Solution

  • If the inputText fails validation (Process Validations phase), then the value mybean.condition will not be updated (Process Updates phase). Since the text field uses the required attribute, this is quite likely.

    Lifecycle from the RichFaces doc:

    alt text
    (source: jboss.org)

    If any JSF phase fails, the lifecycle skips to Render Response to avoid operating on invalid input. You can use the h:message and h:messages tags to view reported errors (though because you're using AJAX, you'll have to put them in something that will get re-rendered.

    I am guessing that if you set the ajaxSingle attribute on the aj4:support tag, you would get the behaviour you want. The documentation says that this will only submit the value for the control, so the text field will not be involved in the Apply Request Values/Process Validations/Process Updates phases.