Pedantic
Pedantic

Reputation: 1378

<form:errors is not woking

Since morning i am stuck in follwing problem. at one place the same thing is working and second place its not working is not displaying the errors. I checked there are many times this question has been asked, i refer all the question and solutions, but still the problem is same.follwiing the code I am using in my controller.

@RequestMapping(method = RequestMethod.POST)
    public String findRide(@ModelAttribute("postAd") PostAdRR user,ModelMap model,@Valid PostAdRR postAd,
    BindingResult result,HttpSession session,HttpServletRequest request) {
    if (result.hasErrors()) {
        System.out.println(result.toString());

            return "postAd";
     }

even it enter in error block and print the SOP line.

but its not showing on form.

 <form:form method="POST" commandName="postAd" enctype="multipart/form-data" >
     <form:errors path="contactName"  cssClass="Err"/>

and my domain class(PostAdRR) code is

@NotEmpty(message = "You must provide Title.")
    private String title =null;

and the same code is working on other place Please suggest something almost i tried all the combinations.

Upvotes: 0

Views: 320

Answers (3)

Japan Trivedi
Japan Trivedi

Reputation: 4483

I think the problem is in your controller function signature. You are validating on other object and your model attribute is another object. That is not the correct way to validate the object and show the errors on the form.

Your method signature should be as following.

public String findRide(@Valid @ModelAttribute("postAd") PostAdRR user,ModelMap model, BindingResult result,HttpSession session,HttpServletRequest request)

That is now you will bind the same object to the view which is you are validating.

Hope that helps you.

Cheers.

Upvotes: 1

raddykrish
raddykrish

Reputation: 1866

You can try annotating @NotNull along with @NotEmpty. Also is it possible to try @Valid in your controller before your model attribute. These are just guesses since you told the other page is working.

Upvotes: 0

Ralph
Ralph

Reputation: 120791

I am a bit wondering why the names in your excample does not match.

On one hand it is: contactName in <form:errors path="contactName" cssClass="Err"/> on the other it is: title

May this is the cause.


If this does not work try <form:errors path="*" cssClass="Err"/> (within the from) to show all error messages. The result of that test hopefully helps you to decide if the problem is in thw view or in the controll.er

Upvotes: 0

Related Questions