Tanmoy Banerjee
Tanmoy Banerjee

Reputation: 1453

Client side validation not working using validate()

I am doing validation inside validate() method.

public void validate(){
    if(continent.equals("-1")){
        HttpServletRequest request=ServletActionContext.getRequest();  
        HttpSession session=request.getSession();  
        String s=(String)session.getAttribute("operation");
        if(s.equals("edit"))
                edit();
        else if(s.equals("add"))
                add();
        addFieldError( "Continent", "Continent must be selected");
    }
}

And in jsp(view) added form attribute validate=true

<s:form action="add" name="aaa" cssClass="yy" method="post" validate="true">
        <s:textfield name="Code" label="Code" readonly="false" cssClass="defaultTextBox"/>
        <s:textfield name="Name" label="Name" cssClass="defaultTextBox"/>
        <s:select name="Continent" label="Continent" headerKey="-1" headerValue="Select" list="continentlist" cssClass="defaultTextBox"/>
        <s:textfield name="IndepYear" label="Independance Year" cssClass="defaultTextBox" />
        <s:submit value="Save" cssClass="login login-submit" theme="simple"/>
</s:form>

But only server side validation is working. My question is -->is it not possible to add client side validation using validate() method?

Upvotes: 2

Views: 625

Answers (3)

Andrea Ligios
Andrea Ligios

Reputation: 50281

It is possible to perform AJAX validation using your server side code using struts2-jquery plugin , as shown in the Showcase under:

  • Form Forms with Validation
  • Form Forms with Custome Validation

The example that might interest you more is Form Submit without AJAX.

Upvotes: 0

Andrea Ligios
Andrea Ligios

Reputation: 50281

In Struts 2, Client Side Validation has different meanings, and totally depends on the type of theme you are using.

  1. With XHTML (default) and CSS XHTML, you can use the

    that is totally client side, Javascript based and doesn't communicate with the server.

  2. With the AJAX theme instead, you can run the

    that will contact the server, running the whole validation Stack, and (to answer your question) running your validate() methods too.


I personally prefer to use the SIMPLE theme, completely handling the HTML, the CSS and the JavaScript on my own.

Since the server-side validation is mandatory, the client-side validation is to be considered just a surplus, positive for making the page more user-friendly, and to reduce the network traffic in high users environment (you block unsuccessfull - but legit - requests before they go through the wire :)

Consider using HTML5 types with fallback on jQuery, especially if you are targeting the mobile.

Upvotes: 1

Eugene Stepanenkov
Eugene Stepanenkov

Reputation: 936

actually you shouldn't mix up server side and client side code. validate method can be invoked only in server side... So there is no way to use this method on client side. You need to write your own JS side validation as the same as server side validation.

Upvotes: 0

Related Questions