Enumerate renaming to form a <select> element in Play! Structure 2.1.

I'm trying to find best practice for binding enum to creating a <select> drop-down in Play! 2.0

Here is my listing:

 public enum ContactType { CLIENT(1), CONTRACTOR(2), SUPPLIER(3); public final int id; ContactType(int id) { this.id = id; } } 

And here is what I would like to receive as a result:

 <select name="contactType"> <option value="1">CLIENT</option> <option value="2">CONTRACTOR</option> <option value="3">SUPPLIER</option> </select> 
+6
source share
3 answers

Something like this in your template should work:

 <select name="contactType"> @for(cType <- ContactType.values()){ <option value="@cType.id">@cType.name()</option> } </select> 

Note: it is better to use toString() instead of name() . If you override toString() in your listing, you can return the Contractor instead of the CONTRACTOR.

Note 2: if your enum is not in the models package, you need to specify its correct package name ie: @for(cType <- com.my_company.enums.ContactType)

+8
source

Assuming you put the selection in an HTML form, how it is done in our store, we add the map to the Java enumeration, and then use the select helper provided by the framework:

listing:

 public enum ContactType { CLIENT(1), CONTRACTOR(2), SUPPLIER(3); public final int id; ContactType(int id) { this.id = id; } public static Map<String, String> options(){ LinkedHashMap<String, String> vals = new LinkedHashMap<String, String>(); for (ContactTypecType cType: ContactType.values()) { vals.put(cType.id(), cType.name()); } return vals; } } 

View:

 @(contactForm: Form[models.Contact]) import helper._ <html><head></head><body> @helper.form(routes.Contact.formHandlerMethod()){ @* * Here contactForm("contactType") assumes that you want to map the * ContactType selected to a field in a Contact class (which we've * wrapped in play.data.Form<T> so that the fields in Contact become * inputs in our form *@ @select(contactForm("contactType"), options(ContactType.options()) } </body></html> 

With this design pattern, you can use the helper functions in the play.data.Form class to validate and bind form data in your contact controller, and you get the opportunity to have less logic in your view and put the logic in a place where it can be easily reused in the whole application.

If, on the other hand, you use the selection yourself and control other ways, then Aerus's answer is probably the easiest way.

+16
source

EDIT: The form should not include the entire enumeration path, only the enumeration name. Therefore, in the ContactType class, the put method must be replaced with this:

  put(value.name(),name); 

I cannot associate an enumeration with a class. In the controller, I have something like this:

 public Result saveUser() { Form<User> userForm = form(User.class).bindFromRequest(); if(userForm.hasErrors()) { return badRequest(createUser.render(userForm)); } User user=userForm.get(); user.save(); Logger.info(user.contacts.toString()); flash("success", "User " + userForm.get().identity + " has been created"); return GO_ADMIN; } 

Which returns me all the time "error.invalid". The POST response is as follows:

 identity=asas& status=1& contacts[0].name=fasddf& contacts[0].prename=afadfs& contacts[0] .email=ffdf@aa.com & contacts[0].contactType=models.constats.ContactType.MANAGER 

To have more information, I provide here the classes "User" and "Contact"

User

 public class User extends Model{ @Constraints.Required(message = "Field is required") public String identity; public Integer status; @Valid public List<Contact> contacts; } 

Contact

 public class Contact extends Generic{ @Constraints.Required(message = "Field is required") public String name; @Constraints.Required(message = "Field is required") public String prename; @Constraints.Required(message = "Field is required") @Constraints.Email public String email; public ContactType contactType; } 

ContactType

 public enum ContactType { ADMIN,SUPPORT,MANAGER,DEVELOPER,DESIGNER,NO_INFORMATION; } public static final Map<String,Object> types=new HashMap<String,Object>() {{ for (ContactType value : ContactType.values()) { String name=value.name().toLowerCase(); name=Character.toUpperCase(name.charAt(0)) + name.substring(1).replaceAll("_", " "); put(ContactType.class.getName() + "."+ value.name(),name); } }}; 
+4
source

Source: https://habr.com/ru/post/950747/


All Articles