java - How to create a constraint validator for multiple fields through annotation in spring 4.* -


how create validator restrictions more fields through annotation in spring 4.* example

@uniquevalidator

@entity @table(name = "persons") @uniquevalidator(message="peson exist",columns={"name","lastname"}) public class {     @column     private string name;     @column     private string lastname; } 

roughly speaking..

select ... persons name='qwerty' , lastname='asdfgh' 

here's 1 way in spring mvc , jsr 303 validation:

create constraint annotation:

package com.awgtek.model.validation;  import java.lang.annotation.documented; import java.lang.annotation.elementtype; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target;  import javax.validation.constraint; import javax.validation.payload;  /**  * target field should unique in data store.  */ @documented @constraint(validatedby = uniquenamevalidator.class) @target(elementtype.type) @retention(retentionpolicy.runtime) public @interface uniquename {      /**      * @return error message template      */     string message() default "{com.awgtek.model.validation.uniquename.message}";      /**      * @return groups constraint belongs      */     class<?>[] groups() default {};      /**      * @return payload associated constraint      */     class<? extends payload>[] payload() default {};      /**      * @return class instance of used check existence of name.      */     class<? extends nameexistencechecker> nameexistencechecker(); } 

apply annotation @ class level model class:

package com.awgtek.model;  import com.awgtek.model.validation.mynameexistencechecker; import com.awgtek.model.validation.uniquename; import com.awgtek.model.validation.uniquenameconstraint;  @uniquename(groups = uniquenameconstraint.class,  nameexistencechecker = mynameexistencechecker.class) public class configurationattribute {      private string name;      private string lastname;      // getters , setters omitted   } 

the uniquenamevalidator:

package com.awgtek.model.validation;  import com.awgtek.service.myservice;  import javax.inject.inject; import javax.validation.constraintvalidator; import javax.validation.constraintvalidatorcontext;  public class uniquenamevalidator implements constraintvalidator<uniquename, object> {      @inject     private nameexistencecheckerfactory nameexistencecheckerfactory;      private nameexistencechecker nameexistencechecker;      @override     public void initialize(uniquename constraintannotation) {         nameexistencechecker = nameexistencecheckerfactory.get(constraintannotation.nameexistencechecker());     }      @override     public boolean isvalid(object value, constraintvalidatorcontext context) {         return !nameexistencechecker.nameexists(value);     } } 

the uniquenamevalidator depends on couple of classes:

package com.awgtek.model.validation;  import javax.inject.inject;  import org.springframework.stereotype.component;  @component public class nameexistencecheckerfactory {      @inject     private namespacenameexistencechecker namespacenameexistencechecker;      @inject     private mynameexistencechecker mynameexistencechecker;      public nameexistencechecker get(class<? extends nameexistencechecker> clazz) {         if (clazz.equals(mynameexistencechecker.class)) {             return mynameexistencechecker;         }         throw new illegalstateexception("unknown nameexistencechecker");     } }  package com.awgtek.model.validation;  public interface nameexistencechecker {      boolean nameexists(object objectwithname); } 

the implementation class looks whether item exists in database (via service):

package com.awgtek.model.validation;  import com.awgtek.model.mymodelclass; import com.awgtek.service.myservice;  import javax.inject.inject;  import org.springframework.stereotype.component;  @component public class attributenameexistencechecker implements nameexistencechecker {      @inject     private myservice myservice;      @override     public boolean nameexists(object objectwithname) {         mymodelclass myobject = (mymodelclass) objectwithname;         return myservice.itemalreadyexists(myobject.getname(), myobject.getlastname());     } } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -