有条件地跳过验证JSF [英] Skip validation conditionally JSF

查看:94
本文介绍了有条件地跳过验证JSF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要求是有条件地跳过字段验证.当用户选择添加时,应验证表单字段并将值添加到表中. 如果单击编辑,则应跳过表单字段验证,但应将Bean值复制到输入字段.

Requirement is to skip field validation conditionally. When user selects add, the form fields should be validated and values added to the table. If edit is clicked, form field validation should be skipped but bean values should be copied to the input fields.

<composite:nameInput id="name" value="#{buyer.name}" 
  disableBeanValidation ="#{param['skipBeanValidation']}"/>
<h:commandLink action="#{buyerBacking.cancelEdit}" value="cancel"/>

 <h:dataTable value="#{bean.list}">
    <h:commandLink value="Edit" action="#{buyerBacking.edit}">
     <f:param name="skipBeanValidation" value="true" /> 
    </h:commandLink>
 </h:dataTable>

   <h:commandLink id="add" action="#{buyerBacking.add}"/>

名称输入复合组件

<h:inputText id="fName" value="#{bean.fname}">
     <f:validateBean disabled="#{cc.attrs. disableBeanValidation}"/>
</h:inputText>

如果用户输入详细信息并选择添加,则详细信息将添加到表中. 如果验证错误,我们将显示错误消息.

If user enter's details and selects add, the details are added to the table. If validation errors, we display the error message.

然后用户选择编辑,将值复制到输入字段. 用户选择取消编辑,这些值将从输入字段中删除. 如果用户再次选择edit,我们将获得索引超出范围的异常错误.对name_firstName的索引超出范围.

Then user selects edit, the values are copied to the input fields. User selects cancel edit, the values are removed from the input fields. If user selects edit again, we get a index out of bound exception error. Index out of bound exception for name_firstName.

但是,如果用户选择添加并选择了取消编辑,则不会发生这种情况.

But this does not occur if user selects add and cancel edit is selected. Does it has to do with the view parameter being passed which is null somehow?

javax.faces.FacesException:标识为frmAddAuthBuyerBR_inpAuthBuyerBRName_prefix_input的组件的意外错误恢复状态.原因:java.lang.IndexOutOfBoundsException:索引:0,大小:0. 在com.sun.faces.application.view.StateManagementStrategyImpl $ 2.visit(StateManagementStrategyImpl.java:272) 在com.sun.faces.component.visit.FullVisitContext.invokeVisitCallback(FullVisitContext.java:151) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1612) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) 在javax.faces.component.UINamingContainer.visitTree(UINamingContainer.java:163) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) 在javax.faces.component.UINamingContainer.visitTree(UINamingContainer.java:163) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) 在javax.faces.component.UIForm.visitTree(UIForm.java:371) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1626)

javax.faces.FacesException: Unexpected error restoring state for component with id frmAddAuthBuyerBR_inpAuthBuyerBRName_prefix_input. Cause: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0. at com.sun.faces.application.view.StateManagementStrategyImpl$2.visit(StateManagementStrategyImpl.java:272) at com.sun.faces.component.visit.FullVisitContext.invokeVisitCallback(FullVisitContext.java:151) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1612) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) at javax.faces.component.UINamingContainer.visitTree(UINamingContainer.java:163) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) at javax.faces.component.UINamingContainer.visitTree(UINamingContainer.java:163) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1626) at javax.faces.component.UIForm.visitTree(UIForm.java:371) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1626)

推荐答案

您的代码不完整,因为您不知道如何实现disableBeanValidation以及如何传递#{param[skipBeanValidation]}.

Your code is incomplete as you're nowhere showing how disableBeanValidation is implemented and how you're passing the #{param[skipBeanValidation]} around.

但是它应该基本上可以归结为:

But it should basically boil down to:

<f:validateBean disabled="#{param.skipBeanValidation}" />

...

<h:commandLink value="Edit" action="#{buyerBacking.edit}"/>
    <f:param name="skipBeanValidation" value="true" />
</h:commandLink>

请注意,#{param.skipBeanValidation}#{param[skipBeanValidation]}完全不同(但与#{param['skipBeanValidation']}相同).还请注意,与#{param}所期望的完全一样,跳过Bean验证所需的请求参数已作为HTTP请求参数传递.

Please note that #{param.skipBeanValidation} is quite different from #{param[skipBeanValidation]} (but the same as #{param['skipBeanValidation']}). Also please note that the desired request parameter to skip the bean validation is been passed as a HTTP request parameter, exactly as is been expected by #{param}.

这篇关于有条件地跳过验证JSF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆