0

I am trying to create an object on Play 2.1 with decimal number variables. I want to set a validation using annotation, currently what I did is:

import play.data.validation.Constraints.Max;
import play.data.validation.Constraints.Min;
@Max(10)
@Min(0.1)
public Float someNumber; 

but it said cannot convert double to long. How could I do this kind of validation?

Thank you

4

2 回答 2

1

您应该创建自己的验证器。看看MaxMin验证,这将让您开始通过扩展创建自己的验证器AbstractAnnotationCheck

这是我为能够注释 JPA 实体中的属性(如@Decimal("15,2").

用作注解的接口:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Constraint(checkWith = DecimalCheck.class)
public @interface Decimal {

    String[] value() default {""};
    String[] lang() default {"*"};
}

DecimalCheck 看起来像这样:

public class DecimalCheck extends AbstractAnnotationCheck<Decimal> {

    public int digits = 0;
    public int decimals = 0;

    @Override
    public void configure(Decimal number) {
        String[] values = StringUtils.split(number.value()[0], ",");

        if (values.length > 1) {
            decimals = Integer.parseInt(values[1]);
            digits = Integer.parseInt(values[0]) - decimals;
        }
    }

    public boolean isSatisfied(Object validatedObject, Object value, OValContext context, Validator validator) {
        value = play.data.validation.Validation.willBeValidated(value);
        if (value == null || value.toString().length() == 0) {
            return true;
        }
        if (value instanceof BigDecimal) {
            BigDecimal number = (BigDecimal) value;

            int numberOfDecimals = number.scale();
            int numberOfDigits = String.valueOf(number.intValue()).length();

            if (numberOfDecimals <= decimals && numberOfDigits <= digits) {
                return true;
            }
        }       
        return false;
    }
}

您当然可以扩展 isSatisfied 方法以支持 value Object 参数的更多类型。您可能有 aString或 aLong或任何您希望能够使用此检查的东西。或者,在您的情况下,一个Float.

于 2013-02-26T10:30:05.040 回答
1

这就是我最后所做的...

@Target({FIELD})
@Retention(RUNTIME)
@Constraint(validatedBy = DoubleMinValidator.class)
@play.data.Form.Display(name="constraint.min", attributes={"value"})
public @interface DoubleMin {
    String message() default DoubleMinValidator.message;
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    double value();
}


public class DoubleMinValidator extends Validator<Number> implements ConstraintValidator<DoubleMin, Number> {

    final static public String message = "error.min";
    private double min;

    public DoubleMinValidator() {}

    public DoubleMinValidator(double value) {
        this.min = value;
    }

    public void initialize(DoubleMin constraintAnnotation) {
        this.min = constraintAnnotation.value();
    }

    public boolean isValid(Number object) {
        if(object == null) {
            return true;
        }

        return object.doubleValue() >= min;
    }

    public Tuple<String, Object[]> getErrorMessageKey() {
        return Tuple(message, new Object[] { min });
    }

}

并调用 @DoubleMin(0.5) public Double num;

对 Max 执行相同的操作。

于 2013-03-07T04:44:07.917 回答