0

Class Level Constraint BeanValidator

asked 2015-06-30 02:00:15 +0800

ghostomg gravatar image ghostomg flag of Indonesia
15 3

updated 2015-06-30 02:29:14 +0800

I'm using ZK 7.0.3 MVVM, Is that possible to integrated class level constraint to MVVM Zul just like usual (@validator('beanValidator'))?

This is snipset of Zul component:

<intbox id="ibQty" width="200px" 
  value="@bind(fx.qty) @validator('beanValidator')" 
  errorMessage="@load(vmsgs[self])"/>

This is snipset of class level constraint Entity:

@Entity
@AllowFractional(allowedStateProperty = "allowedFractional", 
                 valueProperty = "qty", 
                 message = "Qty can't have fractional")
public class TbInvUserreqDetail implements Serializable {
    //... Others field ...

    @Column(name = "allowedfractional")
    private Boolean allowedFractional = false;

    @Column(name = "qty")
    @NotNull(message = "Qty can't empty.")
    private double qty;

    //... setter getter ...
}

This is snipset of custom validator 'AllowFractional' interface:

@Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = AllowFractionalValidator.class)
@Documented
public @interface AllowFractional {
    String message() default "{default message}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    String allowedStateProperty();      
    String valueProperty();    

    @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @interface List {
        AllowFractional[] value();
    }
}

This is the implemented validator class:

public class AllowFractionalValidator implements ConstraintValidator<AllowFractional, Object> {
    private String allowedStateProperty;
    private String valueProperty;
    private String msg;

    @Override
    public void initialize(AllowFractional a) {
        this.allowedStateProperty = a.allowedStateProperty();
        this.valueProperty = a.valueProperty();
        this.msg = a.message();
    }

    @Override
    public boolean isValid(Object obj, ConstraintValidatorContext context) {
        try {
            Boolean allowedState = Boolean.valueOf(BeanUtils.getProperty(obj, this.allowedStateProperty));
            Double value = Double.valueOf(BeanUtils.getProperty(obj, this.valueProperty));

            if (allowedState) {
                return true;
            } else {
                Double fractionalPart = value % 1;
                Double integralPart = value - fractionalPart;
                if (Double.compare(0d, fractionalPart) >= 0) {
                    return true;
                } else {
                    context.disableDefaultConstraintViolation();
                    context.buildConstraintViolationWithTemplate(msg).addPropertyNode(valueProperty)
                            .addConstraintViolation();
                    return false;
                }
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            Logger.getLogger(AllowFractionalValidator.class.getName()).log(Level.SEVERE, null, ex);
            context.disableDefaultConstraintViolation();
            context.buildConstraintViolationWithTemplate(msg).addPropertyNode(valueProperty)
                    .addConstraintViolation();
            return false;
        }
    }
}

Hibernate validator class level constraint just work fine, and when running the application, Zk general error popup appears :

Validation failed for classes [com.myproject.entity.inv.table.TbInvUserreqDetail] during update time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='Qty can't have fractional', propertyPath=qty, rootBeanClass=class com.myproject.entity.inv.table.TbInvUserreqDetail, messageTemplate='Qty can't have fractional'} ]

Could it return the constraint error to ibQty component's errorMessage? Thank you.

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2015-07-01 01:47:47 +0800

ghostomg gravatar image ghostomg flag of Indonesia
15 3

Hi, anyone experianced the same problem? please kindly share.Thank you

link publish delete flag offensive edit
0

answered 2015-07-03 02:20:22 +0800

ghostomg gravatar image ghostomg flag of Indonesia
15 3

Seem, there's no one yet having this problem. or maybe this is ZK BeanValidator limitation that only support field or method level constraint?

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2015-06-30 02:00:15 +0800

Seen: 21 times

Last updated: Jul 03 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More