Form Validation in Play framework
Asked Answered
L

1

0

I am creating a Play application with Play 2.3 in Java.

I'm trying to use Form for handling POST request with JSON body.

My problem is that if my JSON is a simple object with only Strings or Floats attribute, it works well. But if I put some of Object imbrication, it continue to bind the request corectly but don't do the Constraints validation in nested objects.

Here an exemple of what I'm trying to do :

public class PairRequest
{
    @Required
    public String epc;

    @Required
    public RequestProduct product;
}

public class RequestProduct
{
    //Product data
    @Constraints.Required
    private String productCode;

    @Constraints.Required
    public Brand brand;

    @Constraints.Required
    private String furniture;
}

@Entity
public class Brand extends Model {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;

    @Column(length = 250)
    @Constraints.Required
    public String name;

    @Column(nullable = true, length = 512)
    public String regex;
}

Have I missed something ? It's weird because I think it was working during first times... But I can't be sure.

Lizabethlizard answered 1/7, 2015 at 15:13 Comment(0)
S
1

Not only the annotation @Required is needed but also the @Valid for complex objects, as pointed out here: Bind complex (JSON) form data automatically

Solita answered 2/7, 2015 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.