Hello everyone !
I am a newbie with play 2.X, and I'm using Play 2.4.0.
I have a Category model with Ebean, to handle Categories (a category can have several mothers, that's why the ManyToMany instruction):
@Entity
@Table(name="category")
public class Category extends Model {
@Id
public Long id;
@Constraints.Required
public String title;
@Constraints.Required
public String desc;
public String url_image;
@CreatedTimestamp
@Constraints.Required
public Timestamp dateAdd = new Timestamp(System.currentTimeMillis());
public Timestamp dateValidated;
@Version
public Timestamp version;
@Column(nullable=true)
@ManyToMany(mappedBy="mothers")
public Set<Category> daughters= new HashSet<Category>();
@Column(nullable=true)
@ManyToMany( cascade=CascadeType.ALL)
@JoinTable(
name="MOTHERS_CATEGORIES",
joinColumns={@JoinColumn(name="DAUGHTER_ID", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="MOTHER_ID", referencedColumnName="id")}
)
@OrderBy("dateAdd asc")
public Set<Category> mothers = new HashSet<Category>();
}
I'd like to create a form where the user can select multiple categories in a select.
I've tried with and without helpers, but I still can't figure out how to code that...
What I've tried with the template:
<form action="@fastAddForms.routes.categoriesForms.addCategory()" method="get" class="" role="form">
<div class="form-group ">
<input type="text" name="title"/>
<input type="text" name="desc"/>
<select class="selectpicker" multiple="multiple" id="mothers" name="mothers[]">
@for(c <- categories) {
<option value="@c.id" >@c.title</option>
}
</select>
<button>Add Category</button>
</div>
</form> And the controller:
public Result addCategory() {
Form<Category> form = Form.form(Category.class).bindFromRequest();
if (form.hasErrors()) {
return ok("ERRORS");
}
else {
form.get().save();
return ok(dmForm.data().toString());
}
}
Please, help me !
Thank you !
Regards,
Cwellan