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

2019-06-27 04:04发布

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?

2条回答
虎瘦雄心在
2楼-- · 2019-06-27 04:52

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 {
}
查看更多
放荡不羁爱自由
3楼-- · 2019-06-27 05:05

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:

查看更多
登录 后发表回答