bean-validation

Custom validation message for bean validation

柔情痞子 提交于 2019-12-06 02:50:47
I'm creating a JSF 2-application and I'm trying to use form validation in the bean instead of the faces-page. I'd also like to use a .properties file to store the messages. I looked at this question but I don't think I have the properties-file set up correctly. Let's say I have a bean called User in a package called 'dev': @ManagedBean @SessionScoped public class User implements Serializable { @Pattern(pattern=".+@.+\\.[a-z]+", message="{dev.User.emailAddress}") private String emailAddress; // getter/setter } I've also created a file 'ValidationMessages.properties' in WEB-INF/classes (I'm

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'.

How to create a constraint validator for multiple fields through annotation in spring 4.*

落花浮王杯 提交于 2019-12-05 21:46:13
How to create a validator restrictions for more fields through annotation in spring 4.* for example @UniqueValidator @Entity @Table(name = "persons") @UniqueValidator(message="Peson already exist",columns={"name","lastName"}) public class { @Column private String name; @Column private String lastName; } roughly speaking.. select ... from persons where name='qwerty' and lastName='asdfgh' Here's one way to do it in Spring MVC and JSR 303 validation: Create a constraint annotation: package com.awgtek.model.validation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType

JSR303 Validation Group inheritance

你说的曾经没有我的故事 提交于 2019-12-05 20:40:35
Given the following classes and interfaces class A{ @NotNull(groups=Section1.class) private String myString } interface All{} interface Section1 extends All {} When calling A a = new A(); validator.validate(a,All.class); I would expect that it should be invalid since myString is null and it's notNull group extends All but it does not. Note that i'm using the Hibernate impl (4.0.2.GA) of the validator Your expectations are backwards from what the specification requires. From the spec (page 27 on the PDF): For a given interface Z, constraints marked as belonging to the group Z (i.e. where the

Method Parameter Validation with JSR-303

百般思念 提交于 2019-12-05 20:27:20
问题 Is JSR-303 also intended for method parameter validation? If so, is there any example on the web? The biggest challenge I'm facing is how to get a validator within each method. With Spring 3, doesn't it mean that I'd have to inject virtually every class with a LocalValidatorFactoryBean ? Thanks! 回答1: This sounds like a use case for AOP (AspectJ). Write a pointcut for methods that are annotated with javax.validation.constraints.* , inject a validator into the aspect (probably using Spring) and

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,

Cross field validation (JSR 303) problem

大兔子大兔子 提交于 2019-12-05 19:08:28
I have a simple bean, i.e.: public class MyBean { private boolean selected; private String someString; ... } So if selected is true, I want someString to be @NotNull etc. . Any hints, links how to achieve this behaviour? Thanks Jonny You could do this by annotating MyBean with a custom validator, for example: @ValidMyBean public class MyBean { private boolean selected; private String someString; ... } ValidMyBean: @Target({ ElementType.FIELD }) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = MyBeanValidator.class) public @interface ValidMyBean { boolean allViolationMessages()

Spring boot, how to use @Valid with List<T>

一世执手 提交于 2019-12-05 18:33:24
问题 I am trying to put validation to a Spring Boot project. So I put @NotNull annotation to Entity fields. In controller I check it like this: @RequestMapping(value="", method = RequestMethod.POST) public DataResponse add(@RequestBody @Valid Status status, BindingResult bindingResult) { if(bindingResult.hasErrors()) { return new DataResponse(false, bindingResult.toString()); } statusService.add(status); return new DataResponse(true, ""); } This works. But when I make it with input List<Status>

Using Spring's LocalValidatorFactoryBean with JSF

倾然丶 夕夏残阳落幕 提交于 2019-12-05 18:10:20
I am trying to get a bean injected into a custom ConstraintValidator . I have come across some things: CDI is supported in validation-api-1.1.0 (Beta available) Hibernate Validator 5 seems to implement validation-api-1.1.0 (Alpha available) Use Seam validation module Use Spring's LocalValidatorFactoryBean The last one seems most appropriate for my situation since we're already using Spring (3.1.3.Release). I have added the validator factory to the XML application context and annotations are enabled: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001

Does Spring Boot automatically resolve message keys in javax and hibernate validation annotations

回眸只為那壹抹淺笑 提交于 2019-12-05 11:51:32
I was writing a Spring Boot Application. I wanted to know does Spring Boot automatically resolve message keys in javax and hibernate validation annotations. For example: @NotEmpty(message = "${message.key}") String name; I have provided @PropertySource in my application with message properties file and file is also in my classpath. The keys are resolving with @Value but they are not being resolved in validation annotations. What could be the reason for this? Do I need configure a message source bean? Because I have seen examples working without configuring the message source bean. Miloš