Tony
Tony

Reputation: 3805

Form validation in Spring

I want to show errors for all the form, not single field. So I did this:

public void validate(Object target, Errors errors) {
        AddUserForm addUserForm = (AddUserForm) target;

        if (addUserForm.getPassword().length() == 0) {    
            errors.reject("Message error");
        } 

    }

Now I'm using reject method instead of rejectValue, because I don't need to set error message to one field.

And this is my jsp:

<form:form method="POST" commandName="addUserForm"
    class="form-horizontal" style="width: 510px;">

            <form:errors ??? />

...

Which attribute should I use now? I was trying this:

<form:errors commandName="addUserForm"/>

But doesn't help.

Upvotes: 0

Views: 63

Answers (1)

Prasad
Prasad

Reputation: 3795

If you are rejecting by using a fieldName in form object as:

    errors.rejectValue("fieldName", "errorMessage");

This you can display in view as:

    <form:errors path="fieldName"/>

If you are rejecting without specifying the fieldname in form object i.e., raising the error on whole form object then you can set these errors as:

    errors.reject("Message error");

This you can display in view without any attributes as:

    <form:errors/>

Upvotes: 2

Related Questions