Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF validation in action phase

Tags:

validation

jsf

We have a JSF based web application which submits the input form data to a web service and then displays the response provided by web service.

It is the web service that validates the input data entered by the user. I'm not sure how we can use the JSF validation here. As per my understanding JSF validates individual components in the view. But it will be very expensive operation to make a web service request each time.

Instead, when the user submits the form, we skip all validations, make the web service request in the action method. Based on the response from the web service, can we programmatically tell that the particular UI component is invalid?

Could you please provide your suggestion on this?

Environment:-

JSF 2.0
WebSphere Application Server 8.5
like image 851
Apps Avatar asked Dec 23 '12 15:12

Apps


People also ask

What is the process validation phase in JSF?

The Process Validations is the third phase of JSF life cycle. In this phase all convertors and validators are executed on local values and if validation passes all other phases executes normally otherwise JSF implementation adds an error message to the FacesContext instance and render response phase executes directly.

How to use JSF supplied standard validators?

JSF supplied standard validators : If you are using JSF supplied standard validators there is no need to write any code for validation logic. You need to use the standard validator tag of your choice inside a tag that represents a component of type UIInput (or a subclass of UIInput) and provide the necessary constraints.

What is the difference between apply request values and process validations?

The Apply Request Values is the second phase of JSF life cycle. In this phase component in the component tree will retrieve their values from request and store them locally. The Process Validations is the third phase of JSF life cycle.

What is the life cycle of an application in JSF?

JSF application life cycle consists of six phases which are as follows −. Restore view phase; Apply request values phase; process events; Process validations phase; process events; Update model values phase; process events; Invoke application phase; process events; Render response phase; The six phases show the order in which JSF processes a form.


1 Answers

Based on the response from the web service, can we programmatically tell that the particular UI component is invalid?

Yes, you can do that by calling UIInput#setValid(), passing false. Here's the complete piece:

FacesContext context = FacesContext.getCurrentInstance();
UIInput input = (UIInput) context.getViewRoot().findComponent("form:input");
input.setValid(false);
context.addMessage(input.getClientId(context), message);
context.validationFailed();

The FacesContext#validationFailed() doesn't need to be called on a per-input basis by the way.

like image 125
BalusC Avatar answered Sep 30 '22 08:09

BalusC