Is it possible to define optional parameter when rendering an scala template in Play Framework 2?
My controller looks like this:
public static Result recoverPassword() {
Form<RecoveryForm> resetForm = form(RecoveryForm.class);
return ok(recover.render(resetForm));
// On success I'd like to pass an optional parameter:
// return ok(recover.render(resetForm, true));
}
My Scala template looks like this:
@(resetForm: Form[controllers.Account.RecoveryForm], success:Boolean = false)
Also tried:
@(resetForm: Form[controllers.Account.RecoveryForm]) (success:Boolean = false)
In both cases i got “error: method render in class recover cannot be applied to given types;”
From Java controller you can’t omit assignation of the value (in Scala controller or other template it will work), the fastest and cleanest solution in this situation is assignation every time with default value, ie:
Scala template can stay unchanged, as Scala controllers and other templates which can call the template will respect the default value if not given there.
BTW: as you can see, you should use proper methods for returning results from Play’s
actions, seeok()vsbadRequest()also:forrbiden(),notFound(), etc, etcYou can also use flash scope for populating messages and use
redirect()to main page after successful password change, then you can just check if flash message exists and display it:in ANY template:
(this snippet is copied from Computer Database sample for Java, so you can check it on your own disk)