javaformsplayframework-2.1actionform

Play framework : How to make dynamic route in form


I created a form with play framework but when I want to set the "action" form property like this :

    @helper.form(action = @action) {

It didn't work ...

My @action is define as a String

How to make this "action" form property dynamic ?

I can do this with "if" but it's ugly ...


Solution

  • form() helper expects a play.api.mvc.Call as an action, so you need to prepare it somehow, ie in controller, like this:

    public static Result newForm(String arg1, String arg2) {
        play.api.mvc.Call saveAction = routes.Application.saveForm(arg1, arg2);
        return ok(newFormView.render(saveAction));
    }
    

    and then just pass saveAction into first argument of form helper:

    @(saveAction: play.api.mvc.Call)
    
    @helper.form(saveAction){
       @* form fields *@
    }