Play! framework CRUD module: adding default values

2019-04-16 02:15发布

I'm using Play! frameworks CRUD module but I can't figure something out: my database table has a created field which is basically the time that a row was created. I don't want my user to set this, I want to do it in the backend, simply add the current time. I can't figure out how to do this though.

I have made the field invisible using @Hidden but obviously now I can't create new rows because it's value simply isn't set. So where do I do this?

And another question I have: my table also has a column called publish which is another timestamp. The current format for this field in the CRUD form is yyyy-MM-dd. I would like the specify a date as well, and can't figure out how..

Can someone help?

2条回答
干净又极端
2楼-- · 2019-04-16 02:58

You could use the javax.persistence.PrePersist annotation to set the created date. Put this method in your model:

@PrePersist
public void prePersist() {
    created = new Date();
}
查看更多
神经病院院长
3楼-- · 2019-04-16 03:03

you can use custom field rendering in CRUD templates to display the values formatted or using any control you want (i.e.: a jquery date picker for dates).

To hide a value and assign a default value, first of all remove the value from the edit/blank forms of CRUD by removing the field. Then override the _save() method from the entity (be careful with the initial _, you want the _save(), not save()) and set in the code the values you want before calling super._save(). Like this:

/* Return value may differ */
public void _save() {
   current = new Date();
   super._save();
}
查看更多
登录 后发表回答