有条件地需要以a4j形式进行jsf验证

时间:2023-01-24 20:01:02

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

我在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.

这不起作用:a4j:commandButton只是跳过验证。

Thanks.

2 个解决方案

#1


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.

如果inputText验证失败(Process Validations阶段),则不会更新值mybean.condition(Process Updates阶段)。由于文本字段使用了必需属性,因此很有可能。

Lifecycle from the RichFaces doc:

RichFaces doc的生命周期:

alt text http://www.jboss.org/file-access/default/members/jbossrichfaces/freezone/docs/devguide/en/html_single/images/newpic1.png

alt text http://www.jboss.org/file-access/default/members/jbossrichfaces/freezone/docs/devguide/en/html_single/images/newpic1.png

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.

如果任何JSF阶段失败,则生命周期将跳至“渲染响应”以避免对无效输入进行操作。您可以使用h:message和h:messages标签来查看报告的错误(虽然因为您使用的是AJAX,所以您必须将它们放在可以重新渲染的内容中。

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.

我猜你如果在aj4:support标签上设置ajaxSingle属性,你会得到你想要的行为。文档说这只会提交控件的值,因此文本字段不会涉及Apply Request Values / Process Validations / Process Updates阶段。

#2


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').

mybean的范围是什么?如果是请求范围,则单击该按钮时将重置其值,并且可能根本处理输入字段(如果呈现解析为“false”)。

#1


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.

如果inputText验证失败(Process Validations阶段),则不会更新值mybean.condition(Process Updates阶段)。由于文本字段使用了必需属性,因此很有可能。

Lifecycle from the RichFaces doc:

RichFaces doc的生命周期:

alt text http://www.jboss.org/file-access/default/members/jbossrichfaces/freezone/docs/devguide/en/html_single/images/newpic1.png

alt text http://www.jboss.org/file-access/default/members/jbossrichfaces/freezone/docs/devguide/en/html_single/images/newpic1.png

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.

如果任何JSF阶段失败,则生命周期将跳至“渲染响应”以避免对无效输入进行操作。您可以使用h:message和h:messages标签来查看报告的错误(虽然因为您使用的是AJAX,所以您必须将它们放在可以重新渲染的内容中。

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.

我猜你如果在aj4:support标签上设置ajaxSingle属性,你会得到你想要的行为。文档说这只会提交控件的值,因此文本字段不会涉及Apply Request Values / Process Validations / Process Updates阶段。

#2


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').

mybean的范围是什么?如果是请求范围,则单击该按钮时将重置其值,并且可能根本处理输入字段(如果呈现解析为“false”)。