Validating User Input

In most cases when a user enters data through a form, we want to ensure that the values entered are valid before the data is, for instance, stored in a database or used to determine the next action. Form validation is frequently done using client-side JavaScript. This approach is also possible in WebLogic Workshop. In addition, server-side validation checks the user input on the server side. The advantages of the latter approach:


Client-Side Validation

WebLogic Workshop provides a number of attributes on the <netui:form> tag and its form fields, such as onClick and onBlur, which can be used to invoke JavaScript. For example, you can have a <netui:textBox> tag in which a user enters his age:

    <netui:textBox dataSource="{actionForm.age}" onBlur="isValidAge()"/>

This is an example of the JavaScript function isValidAge which is mentioned in the onBlur attribute:

    function isValidAge() {
        if(document.forms[0].elements[1].value <= 0)
            alert('Wrong Age');
    }

After the user has entered a value and goes to the next form field, the onBlur attribute calls the JavaScript function isValidAge, which will display a dialog when the age is not considered valid.

Notice that in the example the form and its fields are accessed by using JavaScript forms and elements arrays. If you would like to use the id attributes to access the form and its fields, you need to (1) specify the tagID attributes on the <netui:form> and its input elements. Then use the JavaScript function getNetuiTagName(tagId, this). Also, you will need to ensure that the <html> tag is replaced with <netui:html> on the JSP page. Rewriting the previous example, the code will be:

<netui:html>
     ...
     <netui:form tagId="myForm" >
         <netui:textBox tagId="age" dataSource="{actionForm.age}" onBlur="isValidAge();"/>
     </netui:form>
     ...
</netui:html>

And this would be the JavaScript function isValidAge:

function isValidAge() {
    if(document.forms[getNetuiTagName("myForm",this)][getNetuiTagName("age",this)].value <= 0)
        alert('Wrong Age');
}

Notice that instead of using elements[1], we refer to the element's tagID using the function getNetuiTagName( tagId, this ).

These approaches also work in conjunction with a submit action of the form. For <netui:button> and <netui:imageButton> tags, you can invoke the JavaScript function through the tags's onClick attribute, as is shown in the following example:

<netui:button value="submit" onClick="return isValidForm();" />

When a user clicks the button, a JavaScript function isValidForm checks the form fields and returns true if validation is successful. Or the isValidForm function displays a dialog with error messages and returns false if not all entered values are correct. (The implementation of this function is not actually shown in the sample code.)

For <netui:anchor> and <netui:imageAnchor> tags, whose implementation rely on JavaScript to submit a form, your JavaScript validation function must contain additional code to set the method and action properties of the JavaScript Form object, and call its submit method if validation is successful. The following example, which uses the same tagIDs and JavaScript function isValidForm as the previous example, demonstrates how to set the various properties and invoke the submit method when form validation is successful:

function submitFromAnchor() {
    document.forms[getNetuiTagName("myForm",this)].method="POST";
    document.forms[getNetuiTagName("myForm",this)].action="/ClientSideValidation/thankYou.do";
    if(isValidForm())
        document.forms[getNetuiTagName("myForm",this)].submit();  
} 

As shown in the example, the form's method property must be set to POST. The action property must be set to the full name of the action to be called when the user clicks the anchor and submits the form. This action name corresponds to the web application name followed by the name of the action method in the JPF file, followed by a DO extension. In the example, the page flow JPF file of the web application ClientSideValidation contains an action method thankYou.

The same function could have been written without the use of getNetuiTagName( tagId, this ), as shown in the following example:

function submitFromAnchor() {
    document.forms[0].method="POST";
    document.forms[0].action="/ClientSideValidation/thankYou.do";
    if(isValidForm())
         document.forms[0].submit();  
}

However, if your application resides in a Portal application, it is recommended that you use getNetuiTagName(tagId, this) to retrieve page elements, since Portal containers may rewrite the id attributes of page elements. For details see Using JavaScript in Page Flow and Portal Applications.

Finally, the <netui:anchor> or <netui:imageAnchor> tags must call the JavaScript function in the onClick attribute and add "return false;" as is shown in the following example:

<netui:anchor onClick="submitFromAnchor(); return false;" action="thankYou">Submit</netui:anchor> 

If you do not add the expression "return false;", the form will always be submitted and no data will be posted to the form bean.

For more information on using client-side JavaScript see Using JavaScript in Page Flow and Portal Applications.

Server-Side Validation

WebLogic Workshop offers two ways to accomplish server-side validation, one using Java to implement the validate method in the form bean, and the other using the Struts ValidatorPlugIn to do XML-based validation. These examples are shown in the WebLogic Workshop sample application, and are described here. For the sample code, start in the following installed location:

<WEBLOGIC_HOME>/samples/workshop/SamplesApp/WebApp/validation/...

Java-Based Validation

To validate user input using the form bean's validate method, follow the steps outlined in this list. Note that these examples are from the following sample page flow, and associated files:

<WEBLOGIC_HOME>/samples/workshop/SamplesApp/WebApp/validation/basic

XML-Based Validation

To validate user input using the Struts ValidatorPlugIn, follow the steps outlined in this list. Note that these examples are from the following sample page flow, and associated files:

<WEBLOGIC_HOME>/samples/workshop/SamplesApp/WebApp/validation/validator

Related Topics

@jpf:validation-error-forward Annotation

@jpf:message-resources Annotation

Validating User Data with Struts Sample

Merging Struts Artifacts Into Page Flows

Using JavaScript in Page Flow and Portal Applications