【发布时间】:2012-06-22 09:47:41
【问题描述】:
我正在使用“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”之类的格式输入我自己,或者没关系,但必须是正确的格式,我在控制器 save() 中遇到验证错误- 带有检查“filledForm.hasErrors()”和错误消息“错误日期格式”的方法。
我以为它会从 play 自动转换,所以我不必自己添加转换。我能做些什么来解决这个问题?还是 play 2.0 的问题吗?
谢谢你。
干杯,
马可
【问题讨论】:
标签: validation date model playframework-2.0