0

I would like to send the results of the validate() method of an action as (JSON?) data to the ajax call on the client side so that I can inform the user.

I am sending form data in the client side through ajax. In the server side I am implementing a simple validate() method (I am no ready for a validation framework yet). This is my ajax call

function save_new_user() {

    var user = 
    {       username: $('#new_user_username').val(),
            email: $('#new_user_email').val(),
            password: $('#new_user_password').val()         
    };

    data = {'user':user}; 
    datastr = JSON.stringify(data);

    $.ajax({
        type : 'POST',
        url : 'SaveNewUser',
        data: datastr,
        dataType : 'json',
        contentType: 'application/json',
        success : function(data, textStatus, jqXHR) {
            if(data) {

            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);
        }
    });
}

And in my Struts2 Action I have

public void validate() {
    if(user.getUsername().length() < 6){
        addFieldError("user", "username must be at least 6 charachers long");
    }

    if(user.getPassword().length() < 8){
        addFieldError("user", "password must be at least 8 charachers long");
    }
}

/* Execute */
public String execute() throws Exception  {

    dbServices.saveUser(user);

    return "SUCCESS";
}

I have a default stack being applied to this action call

<package name="json-data" extends="json-default">
    <interceptors>
        <interceptor-stack name="jsonDataStack">
            <interceptor-ref name="json"/>
            <interceptor-ref name="basicStack"/>
            <interceptor-ref name="validation">
                <param name="excludeMethods">input,back,cancel</param>
            </interceptor-ref>
            <interceptor-ref name="jsonValidation"/>
            <interceptor-ref name="workflow"/>
        </interceptor-stack> 
    </interceptors>

    <default-interceptor-ref name="jsonDataStack"/>

</package>

and the validate() method is being called.

So, here is my question once again: how can I send the results of the validate() function as data to the ajax call so that I can use it to inform the user?. It seems that the method addFieldError() is not configuring the data in a JSON readable form since the error message in the ajax result is

SyntaxError: Unexpected token <(…)
1
  • thanks @Roman, I will add a different answer here, but both questions are in fact very simuilar Feb 6, 2016 at 15:25

1 Answer 1

0

In this answer, regular expressions are used to filter the fields error properties out of the full action properties returned as json object.

In this case I preferred to add the "fieldErrors" property as root object of the result, instead of filtering with regular expressions.

So, I added this to the action configuration (in this case with annotations)

@Result(name="input", type="json", params={"root","fieldErrors"})

And in the ajax configuration, under the success result, I used the returned JSON as

success : function(fieldErrors, textStatus, jqXHR) {
        for (var property in fieldErrors) {
            if (fieldErrors.hasOwnProperty(property)) {
                var this_field_err = fieldErrors[property];
                $('#submit_result').append(property+" error");
                for(var ix=0; ix<this_field_err.length; ix++) {
                    $('#submit_result').append(this_field_err[ix]);
                    $('#submit_result').append("<br>"); 
                }
            }
        }
    }

this adds to the #submit_result div I have the page

username error: Username must be at least 6 charachers long
password error: Password msut be at least 8 charachers long 

Not the answer you're looking for? Browse other questions tagged or ask your own question.