简体   繁体   English

可以使用休眠验证器根据来自另一个类的字段值来验证字段

[英]Can hibernate validator be used to validate a field depending on a field value from another class

I have a scenario where I need to validate some fields of a class depending on the value of a field from another class. 我有一种情况,我需要根据另一个类的字段的值来验证一个类的某些字段。

For ex: Street, City, Postal Code of Address class should not be null if the Employee has registered for a Group Retirement Plan. 例如:如果员工已经注册了团体退休计划,则街道,城市,地址的邮政编码类别不应为null。

Can this scenario be handled using Custom Validator of Hibernate Validator? 可以使用Hibernate Validator的Custom Validator处理这种情况吗? Is this a good candidate for Hibernate Validator ? 这是Hibernate Validator的不错选择吗? If so, could anyone provide me with some examples. 如果是这样,谁能给我提供一些例子。

I have already gone through cross field validation examples like: Cross field validation with Hibernate Validator (JSR 303) 我已经看过诸如以下的跨字段验证示例: 使用Hibernate Validator(JSR 303)进行跨字段验证

If I have a boolean indicator to indicate whether a user is registered for group retirement plan in Address domain class, I know I can come up with Custom Validator to verify the fields. 如果我有一个布尔值指示符来指示用户是否在“地址”域类中注册了组退休计划,我知道我可以提出“自定义验证器”来验证字段。 But, I want to avoid adding indicator to Address class. 但是,我想避免将指示器添加到Address类。 As well, Address is not a field in Employee class to do nested validation. 同样,Address也不是Employee类中用于进行嵌套验证的字段。

The Frameworks I am using are Spring MVC and Mybatis for Data access. 我使用的框架是用于数据访问的Spring MVC和Mybatis。 The validation is happening at Spring Rest API call with @Valid annotation. 验证发生在带有@Valid批注的Spring Rest API调用中。

I guess a custom class level constraint might work, but it depends on your actual domain classes. 我想自定义类级别的约束可能会起作用,但是它取决于您的实际域类。 It is currently not clear how for example Employee, Group Retirement Plan and these other entities are connected. 目前尚不清楚例如雇员,团体退休计划和这些其他实体之间如何连接。

Whether Hibernate Validator is a good fit is whether you have other validations as well or which other frameworks are involved. Hibernate Validator是否合适,是您是否还需要其他验证或涉及其他框架。 For example, if you want validation to take place at JPA live cycle events, then Bean Validation is a good fit, since it integrates per specification with JPA. 例如,如果您希望在JPA实时周期事件中进行验证,那么Bean验证是一个很好的选择,因为它根据JPA集成了每个规范。 If you really only have this one validation, writing some custom code might be all you need. 如果您确实只有这一验证,则可能只需要编写一些自定义代码即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据Symfony中的另一个字段值验证字段 - Validate a field depending on another field value in Symfony 如何使用Cerberus根据另一个字段的值对照模式验证字段? - How can I validate a field against a schema depending on the value of another field with Cerberus? 如何根据SEAM中另一个字段的值验证所需的字段? - How can I validate a field as required depending on another field's value in SEAM? Hibernate验证器验证空字段,而不是@NotEmpty - Hibernate validator validate empty field where is not @NotEmpty 使用@Length注释并且没有@NotEmpty注释的Hibernate验证器验证字段 - Hibernate validator validate field with @Length annotation and without @NotEmpty annotation 使用 validator.v2 根据不同字段的值验证字段 - Validate field based on value of a different field with validator.v2 根据字段的值symfony验证字段 - Validate field depending on its value url symfony Laravel根据另一个字段的值来验证该字段 - Laravel validate the field on the basis of value of another field 如何使用 nest.js 中的类验证器验证数组中每个对象的字段 - How I can validate each object's field in array using class-validator in nest.js 具有类名和字段名的Hibernate Validator自定义消息键 - Hibernate Validator custom messages key with class name and field name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM