How to create a specific validator for a field's model - spring

I want to use a specific validation for a field in my class. It should assure that the username field follow some specific rules.
Can someone point me some docs that show me how to create a validator specifically for a field in a class?
I'm using Spring framework.

Whats wrong with the spring documentation.
The page for that is usually http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html
There are different ways to do that. I personally prefer the javax.validation spring integration instead of the system by spring directly. The creation of a custom validator for an object can be seen in the hibernate-validator (implementation of javax.validation) https://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html/validator-customconstraints.html

Related

AuditAware with Kotlin Exposed

I have a project using Kotlin in Spring Boot. The tables have the standard audit fields 'createdBy', 'createdDt'. Is there a way to handle setting these fields in some way that doesn't involve passing these fields around? Ideally I'd like to intercept every transaction before commit and just set the fields on the entities.
Using JPA and Hibernate, an AuditAware bean could be used to pass the current Principal along with using annotations. Is there a similar integration available or easily implemented? I've looked at the EntityHooks class and don't see an easy way to change data.

Spring Boot field watcher (observer) or something similar

How to run a method in Spring Boot when the value of concrete database field changed or it reached a certain level (programmatically)? The user wants to choose which field the method will respond to. Is there any common solution or best practice in Spring Boot?
I think what you are looking is reload a bean when the properties which are loading from db is changed. you can use something similar here in this stackoverflow post with ApplicationContextAware in prototype scope.

Spring MVC: Recommended way to validate things that require existing Services/Repositories

Spring MVC offers form validation through both annotations on the forms (for example #NotNull) to do a simple check of the value of a field, and custom Validators, which help you do cross-field validations (verifying two password fields contain the same value, etc).
I am looking for the recommended way to do validations that go a bit further, however. For example verify if a username is not used already, which requires a call to the database. I assume I can simply inject my PersonRepository into the custom validator (which is an #Component) after all, but I doubt it'll be transactionally safe..or very clean.
What is the recommended way to do this, that requires the least amount of duplicated code? Or should I simply write my own validation layer, that throws some ValidationException with a list of validationmessages, which I have to map to the bindingresult?
To just clearify: Spring MVC don't offers form validation by itself. It integrates with Java Bean validation vendors (like Hibernate Validator).
You're right: If you configure LocalValidatorFactoryBean as a bean in your application context, you benefit from dependency management in your custom validators. In my opinion there is no need to implement a custom validation layer within your setup, since you already have a powerful and generic abstraction of validation which even conforms to Java standards.
If you worry about whether calling your repository in validator is transaction safe or not see http://docs.spring.io/spring-data/jpa/docs/1.8.2.RELEASE/reference/html/#transactions for details. CRUD operations are transactional by default. If you need a more sophisticated validation logic with needs a transaction context, you could either make your isValid(...) method transactional by annotating it, or you could autowire a business service with likely is transactional by itself. This perfectly integrate with the concepts of Spring.
Use the same business transaction
If you need to handle validation and business logic (check whether a user name is already used and insert a new if not) in the same transaction you could think about restricting those validations in Controller layer to the basic ones (#NotNull for example). This will ensure, that only syntactic correct requests make their way to your service layer.
In the service you will use an autowired validator and trigger the entire validation constraints manually. If you have a combination Hibernate as JPA vendor and Hibernate as Validation vendor you could even make use of the integration of both (pre persit and pre update events) which will cause the validation to occur automatically before the changes are written to the database.
However you decide, you will likely use validation groups to split the constraints into two groups 'syntactic' and 'semantic' for example. If you call the validator manually you can pass the groups you want to take into account. If you use integrated validation with Hibernate you can control the groups for the different events by specifying the following properties:
javax.persistence.validation.group.pre-persist
javax.persistence.validation.group.pre-update
If you decide for this way you will simply call your transactional business service method from your controller. Business logic and validation will participate in the same transaction this way.

Spring client validation on a non DB field

I have a spring flow project. On the form I want to add a check box, like "I confirm that I have provided correct details" and want to stop the flow if this check box is not checked. I don't want to add this as a DB field (validation through that is pretty simple). I cant find a way to validate the check box at client side. Any examples/options are appreciated.
jsr-303 or jsr-349 are the standards for bean validation, and is the reference yoou're looking for, from a high point of view. Youcan start reading from Wikipedia: https://en.wikipedia.org/wiki/Bean_Validation
When it comes to implement this in Spring, you have to annotate the parameter passed to the MVC handler method as #Valid and implement a Validator. The best example you could look at, initially, is the Spring official documentation:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html

How can I replace Hibernate Validator with something else?

I'm not the fan of Hibernate Validator. I would like to use something else (like oval) or implement my custom validation logic. What's the best way to replace Hibernate Validator with my custom validation logic?
As Hibernate Validator (>= 4.x) is one implementation of the Bean Validation specification (javax.validation.* API) you can use other compliant implementations easily (of course depending on to which degree you're using HV specific APIs and functionality such as the provided custom constraints).
Bean Validation provides the concept of custom constraints to implement custom validation logic.
That said (I'm one of the HV committers), what is it that makes you wish to use another implementation? If you found any bugs or have feature requests, you can open a ticket in our issue tracker. If you have an idea for improvement you'd like to discuss, you can either post to our forum or the mailing list. Any feedback matters, so be sure to speak up.

Resources