Play! framework CRUD module: adding default values ​​and change date format?

I am using Play! Framework CRUD, but I can’t understand something: there is a created field in my database table, which is basically the time the row was created. I do not want my user to set this, I want to do this in the backend, just add the current time. I can’t figure out how to do this.

I made the field invisible with @Hidden , but obviously now I cannot create new lines because its value is simply not set. So where am I doing this?

And one more question that I have: in my table there is also a column called publish , which is another timestamp. The current format for this field in the CRUD form is yyyy-MM-dd. I would also like to indicate a date, and I cannot figure out how ..

Can anyone help?

+1
source share
2 answers

you can use personalized field rendering in CRUD templates to display values ​​formatted or using any control you want (for example: select jquery date for date).

To hide the value and set the default value, first remove the value from the CRUD edit / space forms by deleting this field. Then override the _save () method from the object (be careful with the initial _, you want _save (), not save ()) and set the values ​​you want in the code before calling super._save (). Like this:

 /* Return value may differ */ public void _save() { current = new Date(); super._save(); } 
+1
source

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

 @PrePersist public void prePersist() { created = new Date(); } 
+7
source

Source: https://habr.com/ru/post/1387034/


All Articles