全。我使用play框架,但我在验证必填字段时遇到了问题。型号:
@Entity
@Table(name = "office")
public class Office extends AppModel{
@Id
public Long id;
@Constraints.Required(message = "asdfasfd")
public String name;
...
}控制器:
public static Result update() {
Form<Office> filledForm = form(Office.class).bindFromRequest();
if (filledForm.hasErrors()) {
String errorMsg = getErrors(filledForm.errors());
flash("error", errorMsg);
return badRequest(edit.render("Edit", filledForm));
}
Office office = filledForm.get();
...
}查看:
@(title: String, officeFrom: Form[Office])
...
<h1>@officeFrom.get.getName</h1>
...如果从字段值name中删除表单,则在保存时出现错误
[IllegalStateException: Error(s) binding form: {"name":["Field required"]}]怎样做才是正确的?
发布于 2015-12-06 02:56:50
这是因为您在模板中调用了@officeFrom.get.
您指定name是必需的,因此当您调用officeFrom.get时,它将验证约束并在值违反约束时生成错误。在您的例子中,"name“是空的。
您需要使用@fficeFrom("name").value -这将不会检查constarints。
https://stackoverflow.com/questions/34001442
复制相似问题