Search code examples
javahibernatevalidationsanitization

How do I sanitize string fields containing HTML at model level?


I have an app using Spring, JPA (Hibernate) and the Java validation framework (Hibernate Validator). I would like to be able to annotate fields in our domain model that are allowed to contain HTML and have them automatically sanitized at commit time. Anyone know a clever way to do this?

I have tried using the validation framework but this does not support modifying the value of the field at validation time. I could hack things to get something working but am hoping for a cleaner solution.


Solution

  • You can do it with JPA Entity Listeners annotations:

    @PreUpdate

    @PrePersist

    Anothers solution would be to do it in the own setter.