Play Framework 2无法从表单中获取数据

时间:2013-07-09 08:08:23

标签: java forms playframework-2.0

我正在尝试实施重置密码,但我收到错误,但我无法理解,错误是什么。

这是更改密码的类:

public static class PasswordChange {

    @MinLength(6)
    @Required
    public String password;

    @MinLength(6)
    @Required
    public String repeatPasssword;

    public String getPassword() {
        return password;
    }

    public String getRepeatPasssword() {
        return repeatPasssword;
    }

    public void setRepeatPasssword(String repeatPasssword) {
        this.repeatPasssword = repeatPasssword;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String validate() {
        if (password == null || !password.equals(repeatPasssword)) {
            return Messages.get("auth.change_password.error.passwords_not_same");
        }
        return null;
    }
}

由类重新设置以重置密码:

public static class PasswordReset extends Account.PasswordChange {

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String token;  

    public PasswordReset() {}

    public PasswordReset(final String token) {
        this.token = token;
    }

}

这是我的表格:

@(resetForm: Form[controllers.Signup.PasswordReset])

@import helper._
@import helper.twitterBootstrap._

@main(Messages("auth.password.forgot.title")){

<p>
@form(routes.Signup.doResetPassword()) {
    @if(resetForm.hasGlobalErrors) {
        <p class="error">
            <span class="label label-important">@resetForm.globalError.message</span>
        </p>
    }

    @views.html.auth.account.signup._passwordPartial(resetForm)

    <input type="hidden" name="token" id="token" value='@resetForm("token").value' />

    <input type="submit" value="@Messages("auth.change.password.cta")" class="btn btn-primary">
}
</p>

}{ }

提交后,我在控制器中获得表格:

final Form<PasswordReset> filledForm = PASSWORD_RESET_FORM.bindFromRequest(request());

PASSWORD_RESET_FORM是在同一个控制器中声明的字段:

private static final Form<PasswordReset> PASSWORD_RESET_FORM = form(PasswordReset.class);

这就是结果:

Form(of=class controllers.Signup$PasswordReset,
    data={token=e2d48b70-9d00-4b8f-a8e4-ee17089c4e22,
    repeatPassword=1234567, password=1234567},
    value=None,
    errors={repeatPasssword=[ValidationError(repeatPasssword,error.required,[])]})

显然,filledForm.hasErrors()返回true,我什么也得不到(因为value = None)。 任何人都可以指出我的错误吗?

UPD:@ views.html.auth.account.signup._passwordPartial(resetForm)是密码和密码确认字段的模板

@(f: Form[_])

@import helper._
@import helper.twitterBootstrap._

@inputPassword(
    f("password"),
    '_label -> Messages("auth.password.placeholder")
)

@inputPassword(
    f("repeatPassword"),
    '_label -> Messages("auth.password.repeat"),
    '_showConstraints -> false,
    '_error -> f.error("password")
)

1 个答案:

答案 0 :(得分:1)

您需要确定密码的数量。您的表格使用两个,而您的班级使用三个。