我正在制作Play 2.2表单应用程序。我使用这个函数创建一个表单
public static Result editIndicator() {
Indicators addIndObj = new Indicators();
addIndObj.aDt = (new Date()).toString();
addIndObj.aUid = Long.parseLong(play.mvc.Controller.session("userid"));
addIndObj.dTag = "N";
// List of all Countries
ALLCommon table_listobj = Indicators.ddl1();
Form<Indicators> newIndicatorForm = form(Indicators.class).fill(
addIndObj);
return (ok(addindicator.render(newIndicatorForm, table_listobj)));
}
指标模型的参数约束为@Required
,参数如下
@Constraints.Required
@Column(name = "A_DT")
public String aDt;
@Constraints.Required
@Column(name = "A_UID")
public Long aUid;
@Constraints.Required
@Column(name = "D_TAG")
public String dTag;
@Column(name = "TIME")
@Required
@Formats.DateTime(pattern = "HH:mm:ss")
public Date time;
@Constraints.Required
@Column(name = "INDFREQUENCY")
public String indFrequency;
因此,我先设置值,然后将其绑定到表单。我不会在我的表单中使用所有这些@Required
值(只是频率部分),当我尝试获取填充表单时,我会遇到表单错误
Form(of=class models.Indicators,
data={}, value=None,
errors={time=[ValidationError(time,error.required,[])],
aDt=[ValidationError(aDt,error.required,[])],
dTag=[ValidationError(dTag,error.required,[])],
aUid=[ValidationError(aUid,error.required,[])],
indFrequency=[ValidationError(indFrequency,error.required,[])]})
即使我不使用它,我是否需要在表单中设置这些值?或者我错过了什么.. 任何帮助都是适当的..提前感谢.. :))
答案 0 :(得分:0)
找到答案..
1)您必须使用模型所需的所有参数,例如@Required
,如果您不必在表单中使用它。只需将其放在<div display:none>
标记中即可。
2)在提交按钮上,我们调用@forms(routes.Application.index)
但是,括号应该封装您的完整代码,而不仅仅是submit
按钮。因此,
最佳做法是@forms(routes.Application.index){ your complete code here}
。
答案 1 :(得分:0)
如果您不需要一直验证所有字段,则可以使用组。
例如:
// interfaces to create groups
public interface Step1 {}
public interface Step2 {}
因此您需要将这些组添加到您的字段中:
@Required(groups = {Step1.class})
@Column(name = "A_DT")
public String aDt;
@Required(groups = {Step1.class})
@Column(name = "A_UID")
public Long aUid;
@Required(groups = {Step1.class})
@Column(name = "D_TAG")
public String dTag;
@Column(name = "TIME")
@Required(groups = {Step2.class})
@Formats.DateTime(pattern = "HH:mm:ss")
public Date time;
@Required(groups = {Step2.class})
@Column(name = "INDFREQUENCY")
public String indFrequency;
然后:
// this only validates the fields that have Step1 group (aDt, aUid, dTag in this example)
Form<Indicators> newIndicatorForm = form(Indicators.class, Step1.class)
.fill(addIndObj);
// this only validates the fields that have Step2 group (time, indFrequency in this example)
Form<Indicators> newIndicatorForm = form(Indicators.class, Step2.class)
.fill(addIndObj);
// this validates the fields of both groups (all the fields in this example)
Form<Indicators> newIndicatorForm = form(Indicators.class, Step1.class, Step2.class)
.fill(addIndObj);