Search code examples
playframework-2.0ebeanrequiredfieldvalidator

Play! framework 2.+ using Ebean @Constraints.Required on a @ManyToOne foreign key


Is it possible to easily define a constraint on a foreign field?

example: I have an Entity1 and an Entity2. Entity1 has a field entity2_id which is the foreign key that makes the connection. However this doesn't work as well as the @Required constraint on a simple field, for example a String title:

This doesn't work:

@ManyToOne
@JoinColumn(name = "entity2_id")
@Required
public Entity2 entity2;

This works:

@Required
public String title;

I'm aware about the form form validate() method. Is there a simpler way? preferrably a way that will force the error on the specific field in the view without passing the message to the view as a parameter.

Thank you


Solution

  • I have use Required annotation on few ManyToOne relationship, this work perfectly.

    One diference with your code is the JoinColumn annotation that i don't use.

    I'm aware about the form form validate() method. Is there a simpler way? preferrably a way that will force the error on the specific field in the view without passing the message to the view as a parameter.

    Only solution i know is to create your own annotation.