0

I have an action for validating some fields in jsp. If this action returns input, it adds an appropriate field error. But after correcting the field and resubmitting the form, this will not resubmit; the field error will stay attached even if all the fields are correct.

Picture 1:

I have submitted the form with empty Surname field. An appropriate field error was added.

Picture 2:

I have then filled the Surname gap, but the error message is stuck and the form wont resubmit.

Validation:

public String validateForm() {
    if (name == null || name.equals("")) {
        addFieldError("name", "Name is required!");
        return INPUT;
    }

    if (surname == null || surname.equals("")) {
        addFieldError("surname", "Surname is required!");
        return INPUT;
    }

    if (username == null || username.equals("")) {
        addFieldError("username", "Username is required!");
        return INPUT;
    }

    if (password == null || password.equals("")) {
        addFieldError("password", "Password is required!");
        return INPUT;
    }

    if (!passwordCheck.equals(password)) {
        addFieldError("password", "Passwords do not match!");
        addFieldError("passwordCheck", "");
        return INPUT;
    } else {
        return SUCCESS;
    }
}

Struts.xml:

<package name="actions" namespace="/authenticated/admin" extends="struts-default">

    <action name="form">
        <result>/authenticated/admin/inputAccountSave.jsp</result>
    </action>

    <action name="register" class="AccountAction" method="createAccount">
        <result name="success">/success.jsp</result>
        <result name="input">/authenticated/admin/inputAccountSave.jsp
        </result>
    </action>

</package>


<package name="validations" extends="struts-default">

    <action name="validate" class="AccountAction" method="validateForm">
        <result name="success" type="redirectAction">
            <param name="actionName">register</param>
            <param name="name">${name}</param>
            <param name="surname">${surname}</param>
            <param name="username">${username}</param>
            <param name="password">${password}</param>
            <param name="role">${role}</param>
        </result>
        <result name="input">/authenticated/admin/inputAccountSave.jsp
        </result>
    </action>

</package>

Form:

<s:form name="register" action="validate" >
    <s:textfield name="name" label="Name" />
    <s:textfield name="surname" label="Surname" />
    <s:textfield name="username" label="Username" />
    <s:password name="password" label="Password" />
    <s:password name="passwordCheck" label="Confirm password" />
    <s:select name="role" label="Role" list="{'MANAGER', 'ADMIN'}"></s:select>
    <s:submit />
</s:form>

Thank you

Vladislav
  • 65
  • 4

1 Answers1

0

First, you don't need to return INPUT from validate, adding the fieldError will be enough to instruct the Workflow Interceptor to return INPUT automatically.

Second, the naming of your methods, actions and forms is overcomplicateed and potentially misleading... try keeping the things simpler for the future.

Third: you are doing the validation in the Action method. That's not how it works. The action method (by default execute(), but it can be anything, and you have named it validateForm()) must take care of the business of the Action, NOT of the validation. The validation is handled by (XML, Annotations or) a validate() method, that is called by the Validation Interceptor, and is executed before the action is reached. If the validation fails, the action method won't be executed, and the request will be redirected back to the result defined as INPUT.

Read more on the detailed flow if you are interested.

Then in your case:

public String validateForm() {        
    return SUCCESS;
}

public void validate() {
    if (name == null || name.equals("")) {
        addFieldError("name", "Name is required!");            
    }

    if (surname == null || surname.equals("")) {
        addFieldError("surname", "Surname is required!");
    }

    if (username == null || username.equals("")) {
        addFieldError("username", "Username is required!");
    }

    if (password == null || password.equals("")) {
        addFieldError("password", "Password is required!");
    }

    if (!passwordCheck.equals(password)) {
        addFieldError("password", "Passwords do not match!");
        addFieldError("passwordCheck", "");
    } 
}

But please change that methods / actions names ASAP, for your own safety :)

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243