bean-validation

When using @AssertTrue in methods, the method is invoked 4 times during validation (Bean Validation)

本秂侑毒 提交于 2019-12-10 17:52:35
问题 When using bean validation to validate the state of an object, the method annotated with @AssertTrue is called 4 times whenever the validation is invoked. It should only be called once per invocation. Hibernate-validator version: 5.1.3.Final Here is an example: For the following class Motorcycle: import javax.validation.constraints.AssertTrue; class Motorcycle{ private int fuel; private int tireDurability; @AssertTrue(message = "motorcycle.not.available.to.use") public boolean isAvailable(){

Springboot error message interpolation not working for custom validator

折月煮酒 提交于 2019-12-10 16:07:28
问题 I mixed both org.springframework.validation together with JSR-303 annotation. JSR-303 annotation: public class Model{ private String type; private State state; @NotNull(message = "{comment.notnull}") private String comment; } Spring framework validation: @Component public class ModelValidator implements Validator { @Override public boolean supports(Class<?> clazz) { return Model.class.equals(clazz); } @Override public void validate(Object obj, Errors errors) { Model model = (Model) obj; if

How to change location of ValidationMessages.properties in Bean Validation

二次信任 提交于 2019-12-10 12:39:13
问题 By default, ValidationMessages.properties can be located in root of my classpath. Can I change the location of this file like com.myapp.Anything.properties? 回答1: From the JSR-303 specification: 4.3.1.1. Default message interpolation algorithm The default message interpolator uses the following steps: Message parameters are extracted from the message string and used as keys to search the ResourceBundle named ValidationMessages (often materialized as the property file /ValidationMessages

How to trigger validation with @Valid annotation in struts2?

怎甘沉沦 提交于 2019-12-10 12:27:52
问题 I cannot make @javax.validation.Valid annotation work in hibernate,struts2 webapp. Here is my simple Entity with constraints: import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotEmpty; @Entity @Table(name = "user") public class User implements Serializable { @Id @GeneratedValue @Column(name = "user_id") private Long id; @NotEmpty @Length(max = 50, min = 3, message = "Name should be not less 3 and not more 50") @Column(name = "name", nullable =

JSR-303 / Spring MVC - validate conditionally using groups

て烟熏妆下的殇ゞ 提交于 2019-12-10 11:37:30
问题 I worked out a concept to conditionally validate using JSR 303 groups. "Conditionally" means that I have some fields which are only relevant if another field has a specific value. Example: There is an option to select whether to register as a person or as a company. When selecting company, the user has to fill a field containing the name of the company. Now I thought I use groups for that: class RegisterForm { public interface BasicCheck {} public interface UserCheck {} public interface

@NotNull Bean Validation ignored for viewParam

余生颓废 提交于 2019-12-10 11:34:28
问题 Problem I'm trying to validate a mandatory GET request parameter. In the view I've added a corresponding viewParam tag. <f:metadata> <f:viewParam name="customerId" value="#{customerDetailBean.customerId}"/> </f:metadata> And my CDI bean looks like this @Model public class CustomerDetailBean { @NotNull private Integer customerId; public Integer getCustomerId() { return customerId; } public void setCustomerId(Integer customerId) { this.customerId = customerId; } } When I use the following

java/beans validation - collection/map does not contain nulls

若如初见. 提交于 2019-12-10 11:32:34
问题 There is the @NotNull annotation which validates that a certain object is not null. There is the @NotEmpty annotation which validates that a certain collection/map/string/... is not empty. Is there also an annotation which valides that a certain collection/map does not contain any nulls? I am unable to find it. It seems so basic, that I believe it must be in the JSR-303 spec. 回答1: There is no such built-in constraints. You can easily write your custom constraints, eg @NoNullElements, which

Submit form without bean validation

半世苍凉 提交于 2019-12-10 11:29:35
问题 I've got a form which has a domain model with some JSR-303 validation beans. Now I would like to include a "Save draft" feature without any validation. If I set immediate=true on my corresponding commandButton validation is skipped but also the form submit. Is there a way to update the model in my save draft action? 回答1: Use <f:validateBean> where on you set the disabled attribute. <h:inputText value="#{bean.input}"> <f:validateBean disabled="#{bean.draft}" /> </h:inputText> If this evaluates

Doing JSR-303 validation in logical order

自作多情 提交于 2019-12-10 10:45:12
问题 I have such field in my domain model class validation constraints: @Column(nullable = false, name = "name") @NotEmpty(groups = {Envelope.Insert.class, Envelope.Update.class}) @Size(min = 3, max = 32) private String name; When this field is empty ("") or null, validator produces both "cannot be empty" and "size must be between..." error messages. I understand it, but when I show this validation error to the client, it seems quite odd (because when something is null / empty it cannot fulfill

Cross field validation (JSR 303) problem

我怕爱的太早我们不能终老 提交于 2019-12-10 10:12:16
问题 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 回答1: 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)