Wednesday, 15 February 2012

forms - How do I tell Play Framework 2 and Ebean to save null fields? -



forms - How do I tell Play Framework 2 and Ebean to save null fields? -

i'm using play framework 2 , ebean. when user submits form edit existing object in database, doesn't save null values. guess prevent overwriting fields aren't in form null. how can allow them set fields in form null if need to?

for example, user edits event object. event.date 1/1/13. user sets event.date field in form empty , submits form. inspecting event.date in debugger shows value null. save event. if @ event in database, value still 1/1/13.

edit: seems there method this. problem doesn't work on nested entities. solutions this?

update(object bean,set<string> properties)

null properties in ebean considered unloaded, prevent accidental nulling properties shouldn't nulled, excluded.

because of reverting date (and other fields) null in ebean is... hard :). lastly time when had same thing (revert date) used sec query just... nulling date (after event.update(object o)):

public static result updateevent(){ form<event> eventform = form(event.class).bindfromrequest(); // validation if required... event event = eventform.get(); event.update(event.id); if (eventform.get().date == null){ ebean .createupdate(event.class, "update event set date=null id=:id") .setparameter("id", page.id).execute(); } }

on other hand, if using comparison, filtering events (always selecting newer x), can set date 'old' value, should trick. in case you'll update object once.

private static final date very_old_date = new gregoriancalendar(1, 0, 1).gettime(); public static result updateevent(){ form<event> eventform = form(event.class).bindfromrequest(); event event = eventform.get(); if (eventform.get().date == null){ event.date = very_old_date; } event.update(event.id); }

in case in html form need clear value of form's field (or send every time date 0001-01-01), can done javascript.

forms playframework save playframework-2.0 ebean

No comments:

Post a Comment