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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With