Play framework : How to make dynamic route in form

620 views Asked by At

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 ...

1

There are 1 answers

0
biesior On BEST ANSWER

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 *@
}