Apache Struts 2 Documentation > Home > Guides > Core Developers Guide > Validation > Client Validation |

Apache Struts 2 Documentation
Client Validation
Edit Page Browse Space Add Page Add News
Added by tm_jee, last edited by Musachy Barroso on Jan 02, 2007 (view change) show comment hide comment
Comment: add note about required "action" and "namespace"
View page history

Let's create a Client-Side validation workflow, step by step.

The `validate` attribute is set to `true`.

Some themes do not support client-side validation.

## Step 1

Create the form.

    <html>
    <head>
        <title>Validation - Basic</title>
        <s:head/>
    </head>

    <body>

    <s:form method="post" validate="true">
        <s:textfield label="Name" name="name"/>
        <s:textfield label="Age" name="age"/>
        <s:textfield label="Favorite color" name="answer"/>
        <s:submit/>
    </s:form>

    </body>
    </html>

This case uses the default xhtml theme, so the `<saf:head >` tag is used to link a style sheet.

## Step 2

Create the Action class.

    public class QuizAction extends ActionSupport {

        private static final long serialVersionUID = -7505437345373234225L;

        String name;
        int age;
        String answer;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getAnswer() {
            return answer;
        }

        public void setAnswer(String answer) {
            this.answer = answer;
        }
    }

### Step 3

Create the `validation.xml` to configure the validators to be used.

An error occurred: http://svn.apache.org/repos/asf/struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/validation/QuizAction-validation.xml. The system administrator has been notified.

### Action and Namespace

A correct action and namespace attributes must be provided to the <s:form> tag. For example, if the action named "quizClient" is defined in the "/validation" namespace, the form must be configured as:

    <s:form method="post" validate="true" action="quizClient" namespace="/validation">
       <s:textfield label="Name" name="name"/>
       <s:textfield label="Age" name="age"/>
       <s:textfield label="Favorite color" name="answer"/>
       <s:submit/>
    </s:form>

While the following will "work" in the sense that the form will function correctly, client-side validation will not. That is because Struts must know the exact namespace and action (rather than a URL) to properly support validation.

    <s:form method="post" validate="true" action="/validation/quizClient.action">
       <s:textfield label="Name" name="name"/>
       <s:textfield label="Age" name="age"/>
       <s:textfield label="Favorite color" name="answer"/>
       <s:submit/>
    </s:form>