2016-04-12 37 views
0

考慮下面的類:Playframework Java的驗證問題

class Foo{ 
    @Column(name = "created_date", nullable = false) 
    @NotNull 
    public Timestamp getCreatedOn() { 
     return this.createdOn; 
    } 
} 

及以下控制器的操作方法:

public Result post(){ 
    Form<Foo> form = request.bindFromRequest(); 
    if(form.hasErrors()){     
    return badRequest(views.html.formview(form)); 
    }else{ 
    doSomethingAwesome(form.get()); 
    } 
} 

和下面的視圖

@form(routes.fooController.post(), 'class -> "form-horizontal", 'role -> "form") { 
      @inputText(
       form("createdDate"), 
       '_label -> "Create Date", 

      ) 
} 

當我填寫在文本框中以mm/dd/yyyy格式的日期,它仍然會拋出一個表單驗證錯誤,而不是綁定到時間戳。

什麼是正確的方式綁定到時間戳,並沒有形式助手拋出驗證錯誤?

回答

0

「Form form = request.bindFromRequest();」獲取請求上的信息並將其綁定到Foo,嘗試打印form.createdOn的值,它可能爲null,並且您有標記「nullable = false」,並且驗證失敗。