简体   繁体   中英

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.

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:

替代文字
(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.

What is the scope of mybean? If it's request scope then its value will be reset when you click the button and the input field might be processed at all (if rendered resolves to 'false').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM