0

I have a problem with the forms in play2.

Model:

@Id private ObjectId id;

Form

<input name="id" id="id" type="hidden" value="@guidesForm.field("id").value()">

I can not initialize the id field with bindFromRequest() it will always be empty. And I can only query with an ObjectId not with a string.

ObjectId id = new ObjectId(form().bindFromRequest().get("id"));

This is the correct constructor for an objectid. As you can see this is my workaround I don't use guideForm.bindFromRequest(); I just have to bind it directly.

This feels a bit hacky. Is a soltuion where i just can use the normal binding?

Form<Myclass> guideForm = form(Myclass.class);
Form<Myclass> filledForm = guideForm.bindFromRequest();
Maik Klein
  • 15,548
  • 27
  • 101
  • 197

1 Answers1

1

Try to register a custom DataBinder in the onStart() of the Global object:

Formatters.register(ObjectId.class, new SimpleFormatter<ObjectId>() {

    @Override
    public ObjectId parse(String input, Locale l) throws ParseException {

        return ...; // create the object from the input of the form
    }

    @Override
    public String print(ObjectId objectId, Locale l) {
        return String.valueOf(objectId.id);
    }

});

The doc is available at the end of this page: http://www.playframework.org/documentation/2.0.3/JavaForms

ndeverge
  • 21,378
  • 4
  • 56
  • 85