singhakash
singhakash

Reputation: 7919

Play framework Form object for multiple entities

I am using Play 2.2.1 and got stuck in a situation. Basically I am working in a Jobportal application and I have a situation where the Jobseeker needs to fill his/her skills (with skill name, skill experience, etc). So for that I have created a separate entity Skill.

My problem is that the user have an option of adding multiple skills. So if the user enters only one skill I can simply do:

Form<Skill> sk=Form.form(Skill.class).bindRequest();

But if the user enters multiple skills how can I retrieve these multiple objects from form? I need something like:

Form<List<Skill>> sk=Form.form(Skill.class).bindRequest();

Means to retrieve list of entity object from form, the above line is just an explanation to what I want.

I have searched about this topic but didn't get any success. I also know I can simply use DynamicForm or request but it will give me individual columns, not the entity object.

Is this possible? If yes how can I achieve this?

Upvotes: 0

Views: 643

Answers (2)

Mon Calamari
Mon Calamari

Reputation: 4463

Create a wrapper class for Skill class as follows:

public class Skills {

    private List<Skill> skills;

    // setters and getters

}

And bind it from request:

Form<Skills> skills = Form.form(Skills.class).bindRequest();

If you post a json, make it look as follows:

{
   "skills": [
      {
         // skill 1
      },
      {
         // skill 2
      }
   ]
}

Upvotes: 1

biesior
biesior

Reputation: 55798

There are several ways for binding multiple objects Mon Calamari showed one. You can also just bindFromRequest() without any form with DynamicForm and then update given skill(s) yourself.

Anyway... personally I'd do it with jQuery and AJAX definitely, that would be much more modern approach, this way you just send request for the skill that you want to add/edit/delete and others are untouched, AJAX returns the status of the operation so you can modify page without its reloading and re-rendering whole view each time.

Upvotes: 0

Related Questions