Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip validation conditionally JSF

Tags:

jsf-2

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}"/>

Name Input composite component

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

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: 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)

like image 924
user679526 Avatar asked Jan 19 '26 20:01

user679526


1 Answers

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>

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}.

like image 59
BalusC Avatar answered Jan 23 '26 12:01

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!