Reputation: 2098
I have following models:
public class Color {
public String name;
public Long id;
public String rgb;
//setters, getters
}
public class Product {
private static List<Product> products;
static {
products = new ArrayList<Product>();
products.add(new Product("1111111111111", "Paperclips 1",
"Paperclips description 1", new Color(1L,"yellow","ff4400")));
products.add(new Product("2222222222222", "Paperclips 2",
"Paperclips description ",new Color(2L,"red","ff1100")));
products.add(new Product("3333333333333", "Paperclips 3",
"Paperclips description 3",new Color(3L,"brown","ff8800")));
products.add(new Product("4444444444444", "Paperclips 4",
"Paperclips description 4",new Color(4L,"blue","ff4400")));
products.add(new Product("5555555555555", "Paperclips 5",
"Paperclips description 5",new Color(5L,"black","ff4400")));
}
@Constraints.Required
public String ean;
@Constraints.Required
public String name;
public String description;
public Color color;
public Product() {
}
public Product(String ean, String name, String description, Color color) {
this.ean = ean;
this.name = name;
this.description = description;
this.color = color;
}
//other methods required by controller
}
And a view which shows a product form:
@(productForm: Form[Product])
@import helper._
@import helper.twitterBootstrap._
@main("Product form") {
<h1>Product form</h1>
@helper.form(action = routes.Products.save()) {
<fieldset>
<legend>Product (@productForm("name").valueOr("New"))</legend>
@helper.inputText(productForm("ean"), '_label -> "EAN")
@helper.inputText(productForm("name"),'_label -> "Name")
@helper.textarea(productForm("description"), '_label -> "Description")
@helper.inputText(@productForm("color.id"),'_label -> " @productForm('color.name') : @productForm('color.rgb')")
</fieldset>
<input type="submit" class="btn btn-primary">
<a class="btn" href="@routes.Products.index()">Cancel</a>
}
}
My question concerns the line:
@helper.inputText(@productForm("color.id"),'_label -> " @productForm('color.name') : @productForm('color.rgb')")
I know it is not correct, but I need something like this, i.e. to access object of an object in a form, and it also should be both bindable and unbindable as any field of Form[Product]. Is it possible? If not, how usually people go about such cases?
Upvotes: 0
Views: 37
Reputation: 2796
You can access the contents of fields using value
, for example
productForm("color.name").value.getOrElse("unknown")
If it gets a bit unwieldy, or you want to reuse it, you can define it as a template variable with defining
:
@defining( productForm("color.name").value.getOrElse("unknown")+" : "+productForm("color.rgb").value.getOrElse("-") ) { colorLabel =>
@helper.inputText(@productForm("color.id"), '_label -> colorLabel)
Upvotes: 1