hibernate-validator

How would I specify a Hibernate “@Pattern” annotation using a regular expression from a .properties file or database

只愿长相守 提交于 2019-12-03 14:23:08
Situation : I would like to perform Hibernate Validation based upon user properties (to allow different validation rules for input based upon a user's account data) - I think it must be possible to use a .properties file to specify a particular regular expression, but I can't figure out what is wrong: My current method of specifying a validation regex pulls that regex from a constant in a particular interface file (to keep everything together) and plugs it in as a constant in an @Pattern() annotation for each variable - e.g. for the variable workPhone : @Column(name = "WORK_PHONE") @NotEmpty

Syntactically incorrect request sent upon submitting form with invalid data in Spring MVC (which uses hibernate Validator)

我与影子孤独终老i 提交于 2019-12-03 08:53:53
Login form: <f:form class="form-horizontal" method="post" action="/login" commandName="logindata"> <fieldset> <legend class="text-info">Login</legend> <div class="control-group"> <f:label path="uname" class="control-label" for="uname">Username</f:label> <div class="controls"> <f:input type="text" path="uname" name="uname" id="uname" placeholder="Username" /> </div> </div> <div class="control-group"> <f:label path="pwd" class="control-label" for="pwd">Password</f:label> <div class="controls"> <f:input type="password" path="pwd" name="pwd" id="pwd" placeholder="Password" /> </div> </div> <div

Empty validatedBy in @Constraint

亡梦爱人 提交于 2019-12-03 06:03:52
I noticed that all built-in constraints have an empty value for the validatedBy parameter in @Constraint . i.e. @Constraint(validatedBy = {}) First, why are they allowed to have an empty value for validatedBy ? I thought you can leave it blank only for constraint composition that does not need addtional validation? Also, note that the Hibernate Validator can still find a validator implementation class for each built-in constraint, despite the validatedBy is empty, but if I leave the validatedBy blank for my constraint, my custom validator never gets picked up. Why is that? Thanks. Those built

Accent in Regular Expression in Java

烂漫一生 提交于 2019-12-03 05:57:16
问题 I'd like to use Hibernate Validator to validate some columns. The problem, as I understand, is that the \w marker in java doesn't accept letters with accents on them. Is there any way that I could write the regexp so that words like Relatório could be validated (i wouldn't want to write all letters with accents between brackets, because I expect to be writing this regexp in a lot of columns)? 回答1: The Java regex documentation has a section on Unicode categories (search for "Classes for

hibernate unique key validation

纵然是瞬间 提交于 2019-12-03 03:17:45
问题 I have a field, say, user_name , that should be unique in a table. What is the best way for validating it using Spring/Hibernate validation? 回答1: One of the possible solutions is to create custom @UniqueKey constraint (and corresponding validator); and to look-up the existing records in database, provide an instance of EntityManager (or Hibernate Session )to UniqueKeyValidator . EntityManagerAwareValidator public interface EntityManagerAwareValidator { void setEntityManager(EntityManager

Hibernate Validator - @Length - How to specify separate message for min and max?

徘徊边缘 提交于 2019-12-02 20:37:42
I am using Hibernate validator for form validation in my web-app. I am using the @Length annotation for my String attribute as follows: @Length(min = 5, message = "The field must be at least 5 characters") private String myString; However, I have a need to display a different message if the String exceeds 50 characters. Is there a way to use the out-of-the-box @Length validator to do this? An example of what I would like to do (compiler will not let me) is as follows: @Length(min = 5, message = "The field must be at least 5 characters") @Length(max = 50, message = "The field must be less than

Accent in Regular Expression in Java

て烟熏妆下的殇ゞ 提交于 2019-12-02 19:17:46
I'd like to use Hibernate Validator to validate some columns. The problem, as I understand, is that the \w marker in java doesn't accept letters with accents on them. Is there any way that I could write the regexp so that words like Relatório could be validated (i wouldn't want to write all letters with accents between brackets, because I expect to be writing this regexp in a lot of columns)? The Java regex documentation has a section on Unicode categories (search for "Classes for Unicode blocks and categories"). If you're just looking for letters, I think \p{L} is the category you want. I had

hibernate unique key validation

别来无恙 提交于 2019-12-02 16:48:32
I have a field, say, user_name , that should be unique in a table. What is the best way for validating it using Spring/Hibernate validation? One of the possible solutions is to create custom @UniqueKey constraint (and corresponding validator); and to look-up the existing records in database, provide an instance of EntityManager (or Hibernate Session )to UniqueKeyValidator . EntityManagerAwareValidator public interface EntityManagerAwareValidator { void setEntityManager(EntityManager entityManager); } ConstraintValidatorFactoryImpl public class ConstraintValidatorFactoryImpl implements

Spring MVC + Hibernate: data validation strategies

為{幸葍}努か 提交于 2019-12-02 14:02:03
We all know, that Spring MVC integrate well with Hibernate Validator and JSR-303 in general. But Hibernate Validator, as someone said, is something for Bean Validation only, which means that more complex validations should be pushed to the data layer. Examples of such validations: business key uniqueness, intra-records dependence (which is usually something pointing at DB design problems, but we all live in an imperfect world). Even simple validations like string field length may be driven by some DB value, which makes Hibernate Validator unusable. So my question is, is there something Spring

How to get the Invalid value in the error message while using custom validator in spring?

不打扰是莪最后的温柔 提交于 2019-12-02 12:59:01
I having trouble in showing Invalid value in the error message I have messages.properties file as follows ProductId.exists.custom.validator.validation = A product already exists with product id ${validatedValue}. and following is my custom validtor interface @Target( { METHOD, FIELD, ANNOTATION_TYPE }) @Retention(RUNTIME) @Constraint(validatedBy = ProductIdValidator.class) @Documented public @interface ProductId { String message() default "{ProductId.exists.custom.validator.validation}"; Class<?>[] groups() default {}; public abstract Class<? extends Payload>[] payload() default {}; } Here is