playframework 2.0 - Overzealous @Constraints.Required enforcement on update in Play 2.0 -
i'm trying make play stop complaining when updates don't specify required fields in json requests. required fields have values don't want change, shouldn't have specify them again.
stripped down model:
@entity public class run extends model { public enum status { running, ok, warnings, errors, failed, certified }; @id public long id; @constraints.required @manytoone(cascade = cascadetype.refresh) public task task; @jodadatetime @type(type="org.joda.time.contrib.hibernate.persistentdatetime") public datetime started; @jodadatetime @type(type="org.joda.time.contrib.hibernate.persistentdatetime") public datetime completed; @enumerated(enumtype.string) @column(columndefinition="enum('ok','warnings','errors','running','failed','certified')") public status result; }
based on 1 of sample applications initial controller action was:
@transactional @bodyparser.of(play.mvc.bodyparser.json.class) public static result update(long run_id) { form<run> runform = form(run.class).bindfromrequest(); if(runform.haserrors()) { return badrequest(runform.errorsasjson()); } runform.get().update(run_id); objectnode result = json.newobject(); result.put("id", run_id); return ok(result); }
but if send { completed: '1341268791000', result: 'errors }
complains task
required.
then thought maybe use form.fill
fix it:
@transactional @bodyparser.of(play.mvc.bodyparser.json.class) public static result update(long run_id) { run run = run.find.byid(run_id); form<run> runform = form(run.class).fill(run).bind(request().body().asjson()); if (runform.haserrors()) return badrequest(runform.errorsasjson()); runform.get().update(run_id); objectnode result = json.newobject(); result.put("id", run_id); return ok(result); }
but didn't either. what's secret?
you don't need use form insert/update field, can update field 'manually' (simplified sample, of course in example need extract somefield json)
public static result update(long run_id) { run run = run.find.byid(run_id); run.somefield = form().bindfromrequest().get("somefield"); run.update(); return ok(); }
Comments
Post a Comment