1

我正在使用“Play 2.0”-Framework (v. 2.0.1) 并且在日期值的表单验证方面遇到了一些麻烦。

我的模型代码的和平:

@Entity
public class Appointment extends Model {
    public Date start;

    public Date end;
}

我的模板代码和平:

<input type="text" id="start" name="start" placeholder="yyyy-mm-dd" />

<!-- ALSO tested with chrome beta v. 20 with html5 support-->
<input type="date" id="end" name="end" placeholder="yyyy-mm-dd" />

我的控制器:

public class Appointment extends Controller {
    static Form<Appointment> appointmentForm = form(Appointment.class);

    //on calling form page
    public static Result create() {
        return ok(create.render("create", appointmentForm));
    }

    //called on saving form data
    public static Result save() {
        Form<Appointment> filledForm = appointmentForm.bindFromRequest();
        if (filledForm.hasErrors()) {
            return badRequest(
                    create.render("create", filledForm)
            );
        } else {
            Appointment.create(filledForm.get());
            return redirect(routes.Appointment.index());
        }
    }
}

如果我通过 jquery ui datepicker 选择一个日期或以“yyyy-mm-dd”之类的格式输入我自己,或者没关系,但必须是正确的格式,我在控制器保存()方法中遇到验证错误检查“filledForm.hasErrors()”和错误信息“错误的日期格式”。

我以为它会从 play 自动转换,所以我不必自己添加转换。我能做些什么来解决这个问题?它仍然是play 2.0的问题吗?

谢谢你。

干杯,

马可

4

1 回答 1

0

我认为您必须定义日期的格式。请参阅Play Framework 2.0:自定义格式化程序的自定义格式化程序。也许需要自定义活页夹,请参阅https://groups.google.com/d/topic/play-framework/Xi2xAiCnINs/discussion 就像一些提示给你一个方向一样。希望有人能给你一个更好的答案。

于 2012-06-22T11:35:10.720 回答