Play 2.3.7 框架表单的自定义验证不起作用



我在Play 2.3.7框架项目中有以下地址.java类,但用于检查国家/地区字段输入的自定义验证函数validate()不起作用。

字段国家/地区应仅接受:"AR"、"BE"或"SL"。但是现在它实际上接受任何 2 个字符的字符串,这意味着验证不起作用。

谁能说出这里出了什么问题?

package models;
import play.data.*;
import play.data.validation.Constraints.*;
import javax.persistence.*;
@Entity
public class Address extends play.db.ebean.Model {
  @Id
  @GeneratedValue
  public Long internalId;
  //CUSTOM :: Sample implementation of Hard Coded data
  public enum Country {
    ARDA("Arda", "AR"),
    BELGIUM("Belgium", "BE"),
    SMURFS_LAND("Smurfs Land", "SL");
    public String name;
    public String id;
    private Country(String name, String id) {
      this.name = name;
      this.id = id;
    }
    public static Country getById(String id) {
      for (Country c: values()) {
        if (c.id.equals(id)) {
          return c;
        }
      }
      throw new IllegalArgumentException("Country not found => Bad id {"+id+"}");
    }
  }

  @Required
  @Pattern(
    value="[A-Z]{1}\w*, [0-9]+",
    message="A street starts with an upper case, and ends with a number after a comma"
  )
  public String fullStreet;
  @Required
  public String county;
  @Required
  @MaxLength(2)
  public String country;
  //CUSTOM :: validation rules
  public String validate() {
    try {
      Country.getById(country);
      return null;
    } catch (IllegalArgumentException e) {
      return "Bad country : " +country;
    }
  }

}

这是表单中的顶级对象,还是嵌套对象? validate仅在顶级对象上执行。

最新更新