Re: [Play 2.0.3] What is the proper way to bind a Form to a Model which is composed of other objects ?

187 views
Skip to first unread message

James Roper

unread,
Oct 9, 2012, 12:18:34 AM10/9/12
to play-fr...@googlegroups.com
Try making otherText public or defining a setter on otherText, and then defining

public SimpleWrapper wrappedText = new SimpleWrapper();

in Simple.  The play docs state that Play uses the Spring data binder for data binding, you can read about that here:

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html

On Tuesday, 9 October 2012 14:14:10 UTC+11, jgatcher wrote:

I've been having trouble getting my Form bindings to work properly(basically Trial and Error). In Play 2.0.3 (Java) What is the proper way to bind a Form to a Model which is composed of other objects ?

I cooked up this little example to try and understand it better. But even this basic example seems to have issues.

The Simple class which I'm trying to bind the Form to has 3 fields a plain String Field,A List of Strings and a custom field which is just a wrapper around a string. On submitting the Form all the fields are populated except the custom field which remains null.

Here's the actual code

Controller

static Form<Simple> simpleform=form(Simple.class);
public static Result simpleForm(){
Form<Simple> filledForm=simpleform.bindFromRequest();
       
System.out.println(filledForm);
   
return ok(views.html.simpleForm.render(filledForm.get().toString()));
}

Model

public class Simple {
   
public String text;
   
public List<String> stringList;
   
public SimpleWrapper wrappedText;
   
@Override
   
public String toString(){
       
return text +"-"+simpleWrapper+"-"+stringList;
}

public  class SimpleWrapper{
       
String otherText;
       
public SimpleWrapper(){}
       
public SimpleWrapper(String otherText){
           
this.otherText=otherText;
       
}
       
@Override
       
public String toString(){
           
return otherText;
       
}
   
}
Reply all
Reply to author
Forward
0 new messages