Search code examples
springhibernatespring-mvcbean-validation

BeanValidation to check if delete is possible


How can I use BeanValidation to validate record before delete? For example I can only delete record when executeDate field is in the future.

I'm using with success beanvalidation in my project. It's annotation based configured. I use it by @Valid annotation on Controller method. It's Spring based MVC application.

my environment:

<hibernate4.core.version>4.2.1.Final</hibernate4.core.version>
<spring.version>3.2.2.RELEASE</spring.version>
<hibernate.search.version>4.2.0.Final</hibernate.search.version>

Edit:
I have seen this: hibernate validator - different groups on create, update, delete, and Hibernate Documentation also, but it is steel not clear for me. If I have validation working so far do I need only add special case for delete? I'm confused what steps I need to do for get only this one feature.


Solution

  • add to your persistence configuration

    <property name="javax.persistence.validation.group.pre-remov">MyDeleteGroup</property>
    

    Add this annotation to your date field

    @Past(groups=MyDeleteGroup.class)
    

    Delete Group MyDeleteGroup is just a marker interface like javax.validation.groups.Default