In a recent project, I needed such a function, and I had to redefine the Form class (based on the original playback form) to allow an additional parameter to the bindFromRequest() method.
Taking your code as an example, it will become something like this:
Model model = Model.findById(id); Form<Model> filledForm = CustomForm.form(Model.class).bindFromRequest(model);
The idea is to modify only the fields defined in your form and not change the rest of the fields in your model.
To enable this specific binding, you should override the bind(Map<String,String> data, String... allowedFields) method bind(Map<String,String> data, String... allowedFields) (along with bindFromRequest ) in something like this:
public Form<T> bind(T instance, Map<String,String> data, String... allowedFields) { DataBinder dataBinder = null; Map<String, String> objectData = data; if(rootName == null) { dataBinder = new DataBinder(instance); } else { dataBinder = new DataBinder(instance, rootName); objectData = new HashMap<String,String>(); for(String key: data.keySet()) { if(key.startsWith(rootName + ".")) { objectData.put(key.substring(rootName.length() + 1), data.get(key)); } } }
Instead of creating a DataBinder with blankInstance() , as the standard playback form class does, you create it with an instance of the model as an argument to the constructor.
source share