简体   繁体   中英

Play Framework beginner. Get the data from the Form

I have a multiple select in the form. In debug mode, the data of the boundForm for interestedIn is only 2 (the id of the first item in the list), but I would like to get all the selected items

my createuser.scala.html with the form:

...
    <select class="form-control select2-multi" name="interestedIn" id="interestedIn" multiple="multiple">
    @for(interest <- interests){
        <option value="@interest.interestId">@interest.interestName</option>
    }
    </select>

@helper.inputText(userForm("user"), 'class -> "form-control"
...

When I debug this line

Http.MultipartFormData body = request().body().asMultipartFormData();

in data I can see the interestedIn[] array with all the chosen elements in multiple select.

But When I am defining a form on a model, I get only a single element. May this be because I render the view on a list<> instead of map? How do I save the values I gat in a Map?

private static final Form<BusyUser> userForm = Form.form(BusyUser.class);

Form<BusyUser> boundForm = userForm.bindFromRequest();

List<Interest> interests= NeoDataProvider.getInterests(play.api.i18n.Lang.defaultLang().language());
BusyUser user = boundForm.get();
user.mail=user.user; //here I assign the mail of the user the value from the createUser form field
user.active=true;
//HERE I WANT TO GET THE DATA FROM THE MULTIPLE SELECT

BusyUser.class:

public long id;
@Id
@Constraints.Required(message = "*")
@Constraints.Email(message = "?")
public String user;
@Constraints.Required(message = "*")
@Column(unique=true)
public String visibleUsername;
//@Transient
public String password;

//@Constraints.Required(message = "*")
public String name;
//@Constraints.Required(message = "*")
@Constraints.Email(message = "?")
@Column(unique=true)
public String mail;

public Boolean active;
@Formats.DateTime(pattern = "yyyy-MM-dd HH:mm:ss")
public Date lastLogin;

public String authToken;
public String language;

public BusyUser(){}

When I want to define a list where I would keep the selected items from the form

public List<String> interestedIn = new ArrayList<>();

I get this error message:

'basic' attribute type should not be a container

How can I keep all the selected items from the multiple select in a Java list or array?

You can simply read all Multiselect data just in place of name="interestedIn" in your form write name="intrestedIn[]" and in java either in bean or in model declare List<String> intrestedIn= new ArrayList<String>(); after that you can iterate through list and get data like

String value=null;
for(int i=0;i<intrestedIn.size();i++){
value=intrestedIn.get(i);
System.out.println(value);
}

To obtain multiple selected object I use the following code:

 DynamicForm requestData = Form.form().bindFromRequest();
 DynamicForm.Dynamic s = requestData.get();
 Map<String, String[]> map = request().body().asFormUrlEncoded();
 String[] checkedVal = map.get("interestedIn");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM