Sami Asmar
Sami Asmar

Reputation: 131

how to show validation error in jsp in spring mvc

I want to show validation error in my jsp page.

My object is:

public class MyObjectDTO{ @valid private TextDTO text1; @valid private TextDTO text2 }

public class TextDTO{ @NotBlank private String code;@NotBlank private String label;}

My controller:

@RequestMapping(value = "/create", method = RequestMethod.POST)
public String creationProjet(@Valid @ModelAttribute MyObjectDTO obj, BindingResult result,
                             Model model) {
    if (result.hasErrors()) {
        model.addAttribute("hasErrors", true);
        return "create";
    } else {
        ....
        return "redirect:/list";
    }
}

my jsp

<div class="col-md-6 form-group ${requestScope['org.springframework.validation.BindingResult.obj'].hasFieldErrors('text1') ? 'has-error' : ''}">
    <label class="col-lg-3 control-label">my label</label>
    <div class="col-lg-5">
        <form:select class="form-control" name="type" path="text1.code" id="selectType">
    <option value="">---------</option>
            <c:forEach items="${types }" var="type">
                <form:option value="${type.id }">
                    <c:out value=" ${type.code}"></c:out>
                </form:option>
            </c:forEach>
        </form:select>
        <form:errors path="text1.code" class="has-error  error"></form:errors>
    </div>
</div>

My controller redirects to the page create but the errors are not showing. In debug mode there is one error that indicates text1.code cannot be a blank.

Upvotes: 7

Views: 7423

Answers (2)

Bibek Shakya
Bibek Shakya

Reputation: 1273

In your Jsp page add following line

<div class="col-md-6 form-group ${requestScope['org.springframework.validation.BindingResult.obj'].hasFieldErrors('text1') ? 'hasErrors' : ''}">

or use hasFieldErrors() instead

<div class="col-md-6 form-group ${requestScope['org.springframework.validation.BindingResult.obj'].hasFieldErrors()}">

And About addAttributes("hasErrors",true), use addFlashAttribute() which is store in flashmap and Object (In your case Error Message will be alive when you navigate to create page or redirect between two controller.) Look at this for more

In your controller Add RedirectAttributes Object like this

@RequestMapping(value = "/create", method = RequestMethod.POST)
public String creationProjet(@Valid @ModelAttribute MyObjectDTO obj,     
BindingResult result,
RedirectAttributes redirectAtt,
Model model) {
if (result.hasErrors()) {
    redirectAtt.addFlashAttribute("hasErrors","ur message");//instead of true you can write your own message 
    return "create";
} else {
    ....
    }
    return "redirect:/list";
}

}

If you write your own message instead of true

<c:if test="${not empty hasErrors">
     <p>${hasErrors}</p>
</c:if>

Upvotes: 5

Sanka
Sanka

Reputation: 1324

only you missing is put part to model map.You are putting only flag. But you need to put result.

if(result.hasErrors()){             
    mm.addAttribute("errors", result);
    return "create";
}

Upvotes: 0

Related Questions