How to get a DateTime input in a Play! CRUD form?

夙愿已清 提交于 2019-12-10 17:19:40

问题


By default a java.util.Date field is represented in a CRUD form as a simple "DATE" input.

public class DatedModel extends Model {

    public Date creationDate;

in the CRUD admin I see:

creationDate           [TEXTINPUT]
                       yyyy-MM-dd format.

Is there any way to have a DateTime input instead, on selected fields only (not all of them modifying the application.conf date.format)?

Is there a way to affect which "widget" is used for a given field in the 'automated' CRUD admin?


回答1:


Something like this in your application.conf:

module.crud=${play.path}/modules/crud
date.format=yyyy-MM-dd hh:mm:ss

Then in the model:

package models;

import java.util.*;
import play.data.binding.As;
import play.db.jpa.*;
import play.data.validation.*;
import play.templates.*;
import play.mvc.Scope.*;
import javax.persistence.*;
import play.Logger;
import play.templates.JavaExtensions;

@Entity
public class Product extends Model {
    @As(lang={"*"}, value={"yyyy-MM-dd hh:mm:ss"}) 
    public Date creationDate;
}

Your controller:

package controllers;
import play.*;
import play.mvc.*;
import java.util.*;
import models.*;

public class Products extends CRUD {
}
  • Documentation

  • Working demo: https://github.com/saxxi/play-framework-test




回答2:


I finally found in the documentation my answer:

You can indeed customize each field; a more extensive example can be found on the lunatech blog using jquery datatables which also shows how to modify pagination.

#{crud.table fields:['name', 'company']}

   #{crud.custom 'company'}
     <a href="@{Companies.show(object.company.id)}">
         ${object.company.name}
     </a>
   #{/crud.custom}

#{/crud.table}

PS.

@AditSaxena hint was good and indeed a simple solution; but not what I wanted because it's not acceptable that the 'hint' is not correct! Clearly confusing for a user!

So for a datetime input (the specific question) we can combine the annotation (described in the doc)

 @As(lang={"*"}, value={"yyyy-MM-dd hh:mm:ss"}) 

with a custom hint, eg.

   #{crud.custom 'mydate'}
   <span class="crudHelp">
       Date format: etcetc.
   </span>
     ...
   #{/crud.custom}

I'll also point out that you can write your own validator

Other related useful questions:

  • Is it possible in playframework to override the default save action in the CRUD controller and redirect to list after
  • Play! framework CRUD module: adding default values and change date format?
  • Play framework CRUD file upload about custom file upload fields


来源:https://stackoverflow.com/questions/8561717/how-to-get-a-datetime-input-in-a-play-crud-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!