gpt4 book ai didi

hibernate - javax.validation.validationException : At least one custom message must be created

转载 作者:行者123 更新时间:2023-12-05 08:01:14 26 4
gpt4 key购买 nike

根据用 hibernate JSR 303 编写的 checkCase 约束 validator chackCase contraintValidator

public boolean isValid(Group group, ConstraintValidatorContext constraintValidatorContext) {
boolean isValid = false;
...

if(!isValid) {
constraintContext.disableDefaultConstraintViolation();
constraintValidatorContext
.buildConstraintViolationWithTemplate( "{my.custom.template}" )
.addNode( "myProperty" ).addConstraintViolation();
}
return isValid;
}

使用 <f:validateBean> 在 JSF 中应用此 validator 将导致此异常:
javax.validation.validationException:如果默认错误消息被禁用,则至少必须创建一条自定义消息

使用此 validator

package com.mycompany;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class CheckCaseValidator implements ConstraintValidator<CheckCase, String> {

private CaseMode caseMode;

public void initialize(CheckCase constraintAnnotation) {
this.caseMode = constraintAnnotation.value();
}

public boolean isValid(String object, ConstraintValidatorContext constraintContext) {

if (object == null)
return true;

if (caseMode == CaseMode.UPPER)
return object.equals(object.toUpperCase());
else
return object.equals(object.toLowerCase());
}

}


没有 constraintValidatorContext 将在 <h:messages> 中给出消息

最佳答案

问题是, validator 中的 validate() 返回 false 但没有创建消息。你必须像这样添加一个:

if (some condition) {
constraintContext.buildConstraintViolationWithTemplate("something is wrong")
.addConstraintViolation();
return false;
}
return true;

关于hibernate - javax.validation.validationException : At least one custom message must be created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15066391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com