I tried to extract some manytomany values with request().body().asFormUrlEncoded().get(“RolesItems”);
My model :
@Entity
public class Sysuser extends BaseModel {
@Constraints.Required
public String username;
@ManyToMany(cascade=CascadeType.ALL)
public List<Role> roles;
My routes :
POST /sysusers/:id controllers.SysuserController.update(id:Long)
POST /DelRoles/:id controllers.SysuserController.DeleteRoles(id:Long)
But it seems to return a value here :
/**
* DELETE Roles from Sysuser
*/
public Result DeleteRoles(Long id)
{
String[] postAction = request().body().asFormUrlEncoded().get("RolesItems");
Logger.info("Post Action is not null : "+postAction);
.
.
if (postAction == null || postAction.length == 0)
{return badRequest("No roles selected");}
else
{String selected = ListUtils.mkString(postAction, s -> "" + s, ";");
String[] ids = selected.split(";");
Logger.info("Ids Selected are : "+ids);
for (String temp : ids)
{
List<Role> roles = new ArrayList<Role>();
roles.add(Role.find.ref(Long.valueOf(temp)));
Logger.info("List of roles here : "+roles);
/*
final Sysuser sysuser = Sysuser.find.byId(id);
sysuser.roles.remove(Role.find.byId(Long.parseLong(temp)));
Logger.info("Id : "+temp+" a été supprimé avec succès");
sysuser.save();*/
}
}
String refererUrl = request().getHeader("referer");
return redirect(refererUrl);
}
And returning a Null value here :
public CompletionStage<Result> update(Long id) throws PersistenceException {
String[] postAction = request().body().asFormUrlEncoded().get("RolesItems");
Logger.info("Post Action is null : "+postAction);
.
.
Form<Sysuser> sysuserForm = formFactory.form(Sysuser.class).bindFromRequest();
if (sysuserForm.hasErrors()) {
// Run departments db operation and then render the failure case
return departmentRepository.options().thenApplyAsync(departments -> {
// This is the HTTP rendering thread context
return badRequest(views.html.sysuser.edit.render(Sysuser.findByUserName(request().username()), id, sysuserForm, departments, Sysuser.find.byId(id)));
}, httpExecutionContext.current());
} else {
Sysuser newSysuserData = sysuserForm.get();
// Run update operation and then flash and then redirect
return sysuserRepository.update(id, newSysuserData).thenApplyAsync(data -> {
// This is the HTTP rendering thread context
flash("success", "Sysuser ["+ newSysuserData.name +"] a été modifié avec succès");
return GO_HOME;
}, httpExecutionContext.current());
}
}
The both methods are POST, but why in the update, seems to return a NULL value of :
public CompletionStage<Result> update(Long id) throws PersistenceException { String[] postAction = request().body().asFormUrlEncoded().get("RolesItems"); Logger.info("Post Action is null : "+postAction);
But here, the postaction is not null :
/**
* DELETE Roles from Sysuser
*/
public Result DeleteRoles(Long id)
{
String[] postAction = request().body().asFormUrlEncoded().get("RolesItems");
Logger.info("Post Action is not null : "+postAction);