Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 530
David Tinker Avatar asked Apr 09 '26 13:04

David Tinker


1 Answers

You can do it with JPA Entity Listeners annotations:

@PreUpdate

@PrePersist

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

like image 145
Manolo Santos Avatar answered Apr 12 '26 04:04

Manolo Santos