Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I validate a complex model object in Spring 3 using @Valid annotations?
    primarykey
    data
    text
    <p>I have a model object modelling a purchase order. The purchase order has a few fields (such as ID and date) and a list of line-items as ArrayList. I can validate the parent purchase order ok, but it chokes when validating the line-items.</p> <p>Can anyone help me with validation of complex objects? If I cannot validate complex objects auto-magically, how can I write a custom validator that relies upon the constraint annotations in the parent and then iterates over the child line-items? This Validator instance needs to be able to call <code>something.validate(purchaseOrder)</code> and (for each line-item) <code>something.validate(lineItem)</code>. Where do I get "something" from?</p> <p>I have specified <code>&lt;mvc:annotation-driven /&gt;</code> in <code>dispatcher-servlet</code>. I am not using <code>@InitBinder</code>. And I am using <code>@Valid</code> annotation for validation in controller's method like</p> <pre><code> @RequestMapping(params="confirmPurchaseOrder") public String confirm( @ModelAttribute("purchaseOrder") @Valid PurchaseOrder purchaseOrder, BindingResult result, @RequestParam("account") String accountString, @RequestParam("division") String divisionString, Model model) { if (result.hasErrors()) { return PURCHASE_ORDER_CREATE_VIEW; } </code></pre> <p>The domain classes look like this: -</p> <pre><code>public class PurchaseOrder implements Comparable&lt;PurchaseOrder&gt; { /** Based on GUID */ private String id; /** SOP */ @NotNull private Integer SOP; /** Reference from client */ @NotBlank private String purchaseOrderReference; /** PO date */ @DateTimeFormat(style="S-") @NotNull private Date date; @Valid private final Collection&lt;LineItem&gt; lineItems = new ArrayList&lt;LineItem&gt;(); </code></pre> <p>And</p> <pre><code>public class LineItem { ...Elided /** Generated from GUID */ private String id; @NotNull @DateTimeFormat(style="S-") @Future private Date expiry; private String softwareVersion; @NotNull @NumberFormat(style = Style.NUMBER) @Min(1) private Integer licenceCount; </code></pre> <p>When committing a Purchase Order with an empty expiry date, I get the following exception:</p> <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'lineItems[]' of bean class [com.nit.ols.domain.PurchaseOrder]: Invalid index in property path 'lineItems[]'; nested exception is java.lang.NumberFormatException: For input string: "" org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:730) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:634) javax.servlet.http.HttpServlet.service(HttpServlet.java:641) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload