Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass second value in JSF validator?

Tags:

jsf

jsf-2

I have a simple JSF input form into edit page and Validator which checks the value for duplication:

<td>Name</td>
<td>
    <h:outputText value="#{ud.name}"
                  rendered="#{not DCProfileTabGeneralController.editable}" />
    <h:inputText id="dcname" value="#{ud.name}" rendered="#{DCProfileTabGeneralController.editable}"
                 validator="#{ValidatorDatacenterController.validateDatacenterName}" autocomplete="off">
        <f:ajax event="blur" render="dcnamevalidator" />
    </h:inputText>
    <h:message id="dcnamevalidator" for="dcname" />
</td>

public void validateDatacenterName(FacesContext context, UIComponent component, Object value){
....
}

I'm interested is there any possible way to send a second value which will be used into the validation process?

like image 877
Peter Penzov Avatar asked Feb 04 '13 20:02

Peter Penzov


2 Answers

<f:attribute> comes to mind. Add one to the inputtext and retrieve it in the validator.

<h:inputText id="dcname".....
<f:attribute name="param" value="myparam" /> 
</h:inputText>

And:

String param = (String) component.getAttributes().get("param"); 

Can get the value from EL. Good luck

like image 158
Karl Kildén Avatar answered Oct 23 '22 13:10

Karl Kildén


You can add a postValidate event to validate multiple fields , like

<f:event listener="#{bean.validationMethod}" type="postValidate" />

this should fire before model updates and you can get the new value for different component with

FacesContext fc = FacesContext.getCurrentInstance(); 
UIComponent components = event.getComponent();
UIInput param1 = (UIInput) components.findComponent("param1");
UIInput param2 = (UIInput) components.findComponent("param2");

If the validation fails , call the FacesContext renderResponse method to skip model update.

like image 37
Avinash Singh Avatar answered Oct 23 '22 12:10

Avinash Singh