java多个类公共字段检验问题,java – 如何组合验证两个或更多字段?

Class-level constraints

Some of you have expressed concerns

about the ability to apply a

constraint spanning multiple

properties, or to express constraint

which depend on several properties.

The classical example is address

validation. Addresses have intricate

rules:

a street name is somewhat standard and must certainly have a length limit

the zip code structure entirely depends on the country

the city can often be correlated to a zipcode and some error checking can

be done (provided that a validation

service is accessible)

because of these interdependencies a simple property level constraint does

to fit the bill

The solution offered by the Bean

Validation specification is two-fold:

it offers the ability to force a set of constraints to be applied before an

other set of constraints through the

use of groups and group sequences.

This subject will be covered in the

next blog entry

it allows to define class level constraints

Class level constraints are regular

constraints (annotation /

implementation duo) which apply on a

class rather than a property. Said

differently, class-level constraints

receive the object instance (rather

than the property value) in isValid.

06000

The advanced address validation rules

have been left out of the address

object and implemented by

MultiCountryAddressValidator. By

accessing the object instance, class

level constraints have a lot of

flexibility and can validate multiple

correlated properties. Note that

ordering is left out of the equation

here, we will come back to it in the

next post.

The expert group has discussed various multiple properties support approaches: we think the class level constraint approach provides both enough simplicity and flexibility compared to other property level approaches involving dependencies. Your feedback is welcome.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值