hibernate-validator

How can I change annotations/Hibernate validation rules at runtime?

守給你的承諾、 提交于 2019-12-06 04:03:37
If have a Java class with some fields I want to validate using Hibernate Validator. Now I want my users to be able to configure at runtime which validations take place. For example: public class MyPojo { ... @NotEmpty String void getMyField() { ... } ... } Let's say I want to remove the NotEmpty check or replace it with Email or CreditCardNumber , how can I do it? Is it even possible? I guess it comes down to changing annotations at runtime... You can't do it normally. Here's what I've done to get more dynamic validations working via Hibernate Validator. Extend the ClassValidator class.

Spring validator: having both annotation and validator implementation

一笑奈何 提交于 2019-12-06 03:57:46
问题 Is it possible to have both a validator for a form and annotation constraints? For example to have in a form object this field: @NotEmpty private String date; but then validate the date's pattern in a validator. I know there is the pattern annotation but I just want to see if I can use both types of validating. 回答1: Here is the link to a very good site where it's explained how you can combine the JSR-303 validator with the spring validator. I'll present next my solution that works. Hope it

How to upgrade the hibernate-validator 4.3.0.Final to the Glassfish 3.1.2?

﹥>﹥吖頭↗ 提交于 2019-12-06 03:31:25
问题 At the moment, the Hibernate Validator has released the latest version as 4.3.0.Final here. I have tried to upgrade it to my Glassfish 3.1.2 as the following step: - 1. Remove the GLASSFISH/glassfish/modules/bean-validator.jar 2. Copying the hibernate-validator-4.3.0.Final.jar to GLASSFISH/glassfish/modules 3. Restart the Glassfish 4. The Glassfish cannot start. It seems hang. After searching via the Google, I've found that the file named "bean-validator.jar" was created by the Glassfish team

JSR303 - Apply all validation-groups defined in sequence

戏子无情 提交于 2019-12-06 03:02:15
I've got a bean I'd like to do conditional validation on. For this purpose, I've defined a DefaultGroupSequenceProvider<MyObject> which returns the list of groups to validate against. Now, when validating an object that violates the constraints in more than one group in the sequence, only the first group with failures return it's result. I'd like to get an error on all violations, not just the ones in the first group with failures. I'm thinking that this doesn't need a code-sample, but if I'm wrong, I'll be happy to provide one. I followed this http://kh-yiu.blogspot.com/2014/04/conditional

javax.validation.NotBlank missing validator

本小妞迷上赌 提交于 2019-12-05 22:16:35
问题 I have requirement that in common api module(multi module project) I can't use any kind of hibernate's validation annotations, so I did use one from javax.validation which is acceptable. Problem starts when I want to validate my domain objects(I use vaadin) that contains NotBlank annotation. I get the following exception javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.String'.

Bean validation group sequence not working

社会主义新天地 提交于 2019-12-05 19:17:58
问题 I'm using spring 4.1, hibernate validator 5.1.3 for my project. I've been trying to get the GroupSequence to work from last 2 days. I've referred the validation doc, blogs and a few questions posted on stackoverflow. Please see the below class. When I remove the GroupSequence and groups from the annotations, all the validation messages come up together, i.e, all the checks on name and other fields are fired together. Lets say for name field - I want @NotBlank and @Size to be validated first,

With Hibernate validation, can I query the entity being validated?

寵の児 提交于 2019-12-05 19:00:28
I have an entity on which I need to implement the following constraint: "There may only ever be one record for any combination of columns X and Y, for which column Z is null if the record is of type A." The last part turns this from a simple uniqueness constraint to something more complex. I'm writing a custom Hibernate validator to check it. What I'm doing is: @Override public boolean isValid(MyEntity value, ConstraintValidatorContext context) { Query query = DB.createQuery( // DB is just a convenience class "select count(*) from MyEntity" + " where propertyX = :propertyX" + " and propertyY =

Unit testing JSR-303 validation in Spring

三世轮回 提交于 2019-12-05 18:48:19
I am trying to add a unit test for an annotated bean in spring using JSR-303 validation. The bean is a simple one like this: public class Bean { @Size(max=XX) String text; } In the Spring config I have the all the necessary JAR-files: validation-api hibernate-validator The validator is initialized with: <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /> My attempt at a testcase looks like this (the spring and other fluff removed): @Autowired private LocalValidatorFactoryBean factory; @Test public void testTemplateFormBeanValidation() { //

Annotation for hibernate validator for a date at least 24 hours in the future

落爺英雄遲暮 提交于 2019-12-05 12:19:55
I know that exist annotation @Future. If I annotate field with this annotation @Future private Date date; date must be in future means after current moment. Now I need to validate that date was at least 24 hours after current moment. How can I make it? AfterTomorrow.java: @Target({ FIELD, METHOD, PARAMETER }) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = AfterTomorrowValidator.class) @Documented public @interface AfterTomorrow { String message() default "{AfterTomorrow.message}"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }

@Size(min, max) but not required

假如想象 提交于 2019-12-05 07:08:51
Hi In my spring webapp I have a password variable which I want to be at least 0 characters or more than 6 and less than 20. I know that there is annotation: @Size(min=6, max=20) but I have no idea how to add possibility that password can be 0 characters. Will somebody help me with this? Given the comment, you can use StringTrimmerEditor to convert empty string to null, and then @Size check will not trigger (null is considered as valid in @Size). In your controller add following method: @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(String.class, new