开发者

javax.validation get field field owner object

Imagine the current situation:

class Alpha
{
    Integer x1;
    @BiggerThan("x1")
    Integer x2;
}

I want to write a custom constraint validator for this @BiggerThan annotation. However, there doesn't appear to be开发者_如何学编程 a way to get hold of the Alpha instance in the ConstraintValidator implementation. The signature of the isValid method is

public boolean isValid(Integer value, ConstraintValidatorContext context)

There is no 'Object owner' parameter. There doesn't seem to be a way to get it through the context either.

Is it at all possible to implement this in the javax.validator framework?

I'm using hibernate-validator as implementation, but I'm open to changing that. I would consider a hacked solution tied to a particular jsr303 implementation if need be.


You can implement it as a class-level constraint:

@BiggerThen(a = "x1", b = "x2")
class Alpha {
    Integer x1;
    Integer x2;
}

For example, as here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜