Spring MVC Form Hibernate Validator:交叉参数约束没有交叉参数验证器

时间:2013-08-11 07:20:35

标签: forms hibernate spring-mvc hibernate-validator

这是我的表格

public class TaskForm extends WebForm<TaskModel> {

    public TaskForm(){
        this(new TaskModel());
    }

    public TaskForm(TaskModel form) {
        super(form);
    }


    @NotNull
    @NotEmpty
    public void setName(String taskName){
        target.setTaskName(taskName);
    }


    @NotNull
    @NotEmpty
    public void setDescription(String description){
        target.setDescription(description);
    }

    public void setStartDate(DateTime startDate){
        target.setStartDate(startDate);
    }

    public DateTime getStartDate(){
        return target.getStartDate();
    }

    @DateTimeFormat(pattern = "MM/dd/yyyy")
    public void setEndDate(DateTime endDate){
        target.setEndDate(endDate);
    }

    public DateTime getEndDate(){
        return target.getEndDate();
    }

    public String getName(){
        return target.getTaskName();
    }

    public String getDescription(){
        return target.getDescription();
    }
}

当我提交表格时,它给了我一个例外。

HTTP Status 500 - Request processing failed; nested exception is javax.validation.ConstraintDefinitionException: HV000154: Cross parameter constraint org.hibernate.validator.constraints.NotEmpty has no cross-parameter validator.

是什么导致了这个问题?

2 个答案:

答案 0 :(得分:4)

尝试在getter中移动@NotNull和@NotEmpty验证约束。像这样改变你的形式

public class TaskForm extends WebForm<TaskModel> {

    public TaskForm(){
        this(new TaskModel());
    }

    public TaskForm(TaskModel form) {
        super(form);
    }

    public void setName(String taskName){
        target.setTaskName(taskName);
    }



    public void setDescription(String description){
        target.setDescription(description);
    }

    public void setStartDate(DateTime startDate){
        target.setStartDate(startDate);
    }

    public DateTime getStartDate(){
        return target.getStartDate();
    }

    //@DateTimeFormat(pattern = "MM/dd/yyyy")
    public void setEndDate(DateTime endDate){
        target.setEndDate(endDate);
    }

    public DateTime getEndDate(){
        return target.getEndDate();
    }

    @NotNull
    @NotEmpty
    public String getName(){
        return target.getTaskName();
    }

    @NotNull
    @NotEmpty
    public String getDescription(){
        return target.getDescription();
    }
}

答案 1 :(得分:1)

Bean约束必须位于类型的字段或getter上。如果将它们放在返回void的setter上,则验证引擎会假定交叉参数约束(方法验证)。移动约束将解决问题。