2017-11-03 17 views
-3

JSD 유효성 검사기를 사용하는 BigDecimal 객체의 유효성 검사 길이는 입니다. 최대 10자를 포함해야합니다.정확한 길이가 아닌 주어진 길이의 BigDecimal에 대한 유효성 검사 크기

Some valid examples: 
123456789.0 
12345.67890 
12345.67 
1.2345 

Invalid examples: 
123456789.
123.32131232 

어노테이션을 사용하여 어떻게하면됩니까? @Size 주석 다음에 오는 것은 JSR 문서에 따라 String 객체를위한 것이다.

@Size(max = 10) 
@Column(name = "totalPrice") 
private BigDecimal totalPrice; 
+0

최대 10 자 *** ***? 'bd.toString(). length() <= 10' – Andreas

+0

JSR 주석을 사용하여 유효성을 검사하고 싶습니다. 그런 식으로는 안된다. –

+1

나만의 맞춤 검증 제약 조건, 즉 자신의 주석과 자신의 'ConstraintValidator'구현을 구현한다. – Andreas

답변

0

당신은 시도 할 수

'@Digits (정수 = 분수 =) 또는 @DecimalMax가 (값 = "9999999999.999는"메시지 = "소수점 값 이상 9999999999.999 수 없습니다")'

이 모두 작동해야합니다.

당신이 사용하는 방법을 알고 싶다면, 다음

https://www.owasp.org/index.php/Bean_Validation_Cheat_Sheet

사용자 정의 제약은 @decimalmax

http://www.c-sharpcorner.com/UploadFile/5fd9bd/javax-annotation-and-hibernate-validator-a-pragmatic-appro/

0

에 대한 @digit에 대한 다음 URL

을 함께 할 것입니다 필요합니다.

주석 :

@Target({ METHOD, FIELD }) 
@Retention(RUNTIME) 
@Documented 
@Constraint(validatedBy = { BigDecimalLengthValidator.class}) 
public @interface BigDecimalLength { 
    int maxLength(); 
    String message() default "Length must be less or equal to {maxLength}"; 
    Class<?>[] groups() default { }; 
    Class<? extends Payload>[] payload() default { }; 
} 

ConstraintValidator :

public class BigDecimalLengthValidator implements ConstraintValidator<BigDecimalLength, BigDecimal> { 
    private int max; 

    @Override 
    public boolean isValid(BigDecimal value, ConstraintValidatorContext context) { 
     return value == null || value.toString().length() <= max; 
    } 

    @Override 
    public void initialize(BigDecimalLength constraintAnnotation) { 
     this.max = constraintAnnotation.maxLength(); 
    } 
} 

사용법 : 더 조정을위한 기본적인 요구를 작성해야

@BigDecimalLength(maxLength = 3) 
private BigDecimal totalPrice; 

(메시지를 다음과 같이 그 약을 수행 할 수 있습니다 속성 파일 등)을 확인하려면 Creating custom constraints을 확인하십시오.