【问题标题】:Playframework: Handling Form SubmissionPlayframework:处理表单提交
【发布时间】:2023-03-30 22:39:03
【问题描述】:

我正在开发一个 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,[])]})

即使我不使用它,我是否需要在表单中设置这些值?或者我错过了什么.. 任何帮助都会得到帮助..在此先感谢.. :)

【问题讨论】:

    标签: java model-view-controller playframework


    【解决方案1】:

    找到答案了..

    1) 您必须使用模型所需的所有参数,即@Required,如果您不必在表单中使用它。只需将其放在&lt;div display:none&gt; 标记中即可。

    2) 在提交按钮上,我们调用@forms(routes.Application.index),但是,括号应该封装您的完整代码,而不仅仅是在submit 按钮周围。因此, 最佳做法是@forms(routes.Application.index){ your complete code here}

    【讨论】:

      【解决方案2】:

      如果您不需要一直验证所有字段,则可以使用组。

      例如:

      // 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-10
        • 1970-01-01
        • 1970-01-01
        • 2012-12-10
        • 1970-01-01
        • 1970-01-01
        • 2013-03-05
        • 2018-02-03
        相关资源
        最近更新 更多