Conditional Validation in Java Studio Creator 2

There have been some Creator forum posts lately about delaying validation on some fields when a drop down list selection is made if “Auto-submit On Change” is selected.

Problem

Here's a typical scenario. You have a drop down list and one or more static text fields. You also have a text field and a button. You want the value of the static text field to changed based on the value selected on the drop down list. You also want the text field to be required when the user presses the button. So you select the drop down list “Auto-submit On Change” and the text field to required. The Creator 2 designer would look something like this.

The effect of this situation causes the text field validation to trigger the “required” when you select a new value for the drop down list. The figure below shows a snapshot of the results of selecting a value from the drop down list.

Solution

There are a couple of ways to address this problem. The overall term for what is being done is called “Conditional Validation”. Conditional Validation should be used when you need to do validation based on logic that goes beyond defining a validator for a field.

First, I need to give the credit for the following solutions to Jayashri Visvanathan and Matt Bohm. Jayashri is an engineer on the Creator 2 team. She has in-depth knowledge of JavaServer Faces since she worked on the JavaServer Faces team with Craig McClanahan. Matt is also a Creator engineer who was in on the ground work of the data providers and the inventor of the virtual forms. He also has in-depth knowledge of the JavaServer Faces life cycle.

Option 1 – Logic in the Action Method

”In your JSF application built using Creator, you might run into a scenario where you want to execute validators only upon button click and not when form submission happens via JavaScript (for example when turn on autosubmit on change on a drop down list). In this case you won't be able to attach any validators on your input components because once you attach them, they will always get executed during “ProcessValidations” phase irrespective of whether the postback happened via button click or form submission. So the work around is to do conditional validation upon button click. So in your button's action handler, you validate your input field and if it failed, return "null" as outcome from your action handler to tell the JSF framework to redisplay the same page instead of processing the navigation rules for the current view. Here is the sample code you can add to your action handler to ensure that textfield is not null when the page is submitted via button click.” - Jayashri

        if (textField1.getValue() == null || textField1.getValue().equals("")) {
            FacesMessage message = new FacesMessage("Textfield1 is required. Enter a value");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            getFacesContext().addMessage(textField1.getClientId(getFacesContext()), message);
            return null;
        }
        return "success";

With Jayashri's solution, you need to make sure “required” is not selected for the component you are validating.

Option 2 – Logic in The Preprocess Method Before “ProcessValidations”

Matt's solution is implemented differently. Matt's solution will change the required attribute of a component in the “preprocess()” method of the page which happens before the JSF “ProcessValidations”. Here's the designer shot of Matt's layout.

In Matt's example, you don't want to validate the drop down list unless the user presses the “Next Page-->” button. If the user presses the “<--Previous Page” button, you don't want to validate the drop down list but rather allow the user to navigate back. Here's the code that will accomplish this.

    public void preprocess() {
        FacesContext context = FacesContext.getCurrentInstance();
        Map map = context.getExternalContext().getRequestParameterMap();
        if (map.containsKey(nextButton.getClientId(context))) {
            shippingMethodDropDown.setRequired(true);
        } else {
            shippingMethodDropDown.setRequired(false);
        }
    }

The request parameter map is obtained and checked to see if it contains the “nextButton”. If it does, this means the button was pressed and the drop down list is set to “required”. Otherwise, the drop down list is set to NOT “required”.

Again, thanks go to Jayashri and Matt!

Cheers!

-David

Comments:

Post a Comment:
Comments are closed for this entry.
About

David Botterill

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today