Friday 15 August 2014

playframework - Play Framework Processing partial form -


I am using the play framework and I have a mango to update a model with a form in one controller. The case of use is action. But I have some problems in understanding the concept behind it because it is just working if you have a form that contains all the properties of the model if you have only one partial form, eg Editing just one password of a user model, this method destroys the model, because it sets the other properties of the model to clear Is there any "official" solution to that problem? Either way update only existing properties?

  public static result update (long id) {model model = Model.findById (id); The form's & lt; Model & gt; FillForm = modelForm.bindFromRequest (); If (supplement HACAERS ()) {change badRequest (edit.reader); } Else {model.update (); Flash ("Message", "New Model Made!"); Return is OK (index.render ()); }}   

Perhaps the solution in any way indicates that the bindFormRequest () method can be called with additional parameters, such as a string of strings or strings? But I can not know its purpose There will be some insights in it too. Thanks a lot!

In a recent project, I needed this type of facility and re-implementing me Form class (based on the original play form) to allow an additional parameter for the bindFromRequest () method.

Take your code as an example, it will be something like this:

  model model = model.findbid (id) ; The form's & lt; Model & gt; FillForm = CustomForm.form (Model.class) .bindFromRequest (model);   

This idea is only to modify the field defined in your form and to keep other fields of your model un-attached.

To allow this specific binding, you have to define bind (map & lt; string, string & gt; data, string ... permissions) method ( Such as with bindFromRequest ) something like this:

  public form & lt; T & gt; Tie (T examples, map & lt; string, string & gt; data, string ... allowed field) {DataBinder dataBinder = null; Maps & lt; String, string & gt; ObjectData = data; If (rootName == zero) {dataBinder = New DataBinder (example); } Other {dataBinder = new databar (example, root name); ObjectData = new Hashmap & lt; String, string & gt; (); (String key: data.keySet ()) {if (key.startsWith (rootName + "."}} {ObjectData.put (key.substring (rootName.length () + 1), data. Get (key)); }   

instead of DataBinder with blankInstance () as a standard play form class, you create it with your Creator Model example as an example.

No comments:

Post a Comment