Spring MVC custom validators

Amar

I am validating my spring form for that I wrote my own validator. In that I was unable to show my error codes.

I have a model/pojo class.

public class Person{
    private String dob;

    @InHouse
    private Contact contact;

   //getters and setters
}

So here "Contact" is another class and it has 3 variables.

public class Contact{
  private String mobile1;
  private String mobile2;
  private String mobile3;

  //getters and setters
}

All my hibernate connections are fine.

Below is my custom validator.

    @Override
public void validate(Object argTarget, Errors argErrors) {
    Person person = (Person) argTarget;
    validate(argTarget.getClass(), argErrors, person);
    List<Field> inHouseAnnotationFields = AnnotationProcessor
            .getAnnotatedFields(argTarget.getClass(), InHouse.class);
    if (Precondition.checkNotEmpty(inHouseAnnotationFields)) {
        for (Field field : inHouseAnnotationFields) {
            System.out.println(field.getName());
            Object obj = getValue(person, field.getName());
            validate(field.getType(), argErrors, obj);
        }
    }
}

@SuppressWarnings("unchecked")
protected void validate(Class<?> argClass, Errors argErrors,
        Object argObject) {
    List<Field> calidationFieldsList = AnnotationProcessor
            .getAnnotatedFields(argClass, Validation.class);
    if (Precondition.checkNotEmpty(calidationFieldsList)) {
        for (Field field : calidationFieldsList) {
            try {
                field.setAccessible(true);
                Object result;
                result = field.get(argObject);

                Object instance = getValidatorInstance(argClass, field);
                if (Precondition.checkNotNull(instance)
                        && Precondition.checkNotNull(result)) {
                    com.rise.validation.Validation<String, String> validation = (com.rise.validation.Validation<String, String>) instance;
                    boolean valid = validation.validate(result.toString());
                    if (!valid) {
                        argErrors.rejectValue(field.getName(),
                                field.getName() + " Validation Failed");
                    }
                }
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

In the above code "InHouse" is my custom annotation. Contact(Person Class) variable has this annotation.

In my input I gave a wrong phone number for mobile3 field.

I am validating each and every field here.Here I am using Reflections. I will add all my error messages based on my variable("Valid"). So when I use "argErrors.rejectValue(arg1,arg2)" I am getting an exception like.

org.springframework.beans.NotReadablePropertyException: Invalid property 'mobile3' of bean class [com.rise.common.model.Person]: Bean property 'mobile3' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

Here my mobile3 is in my "Contact" class. I don't know why I am getting this exception while adding all my error messages to argErrors object.

Can any one help me on this.

Note: Here I am using reflection to iterate over the fields. For example First I will iterate over Person fields (dob) then I will get the Contact object from Person then I will iterate over Contact fields.

I was stuck here. Please help me on this and also is it the right way to write Custom Validators.

Thanq, Amar.

noobandy

the error says that it can't find the property mobile3 in com.rise.common.model.Person because it's not in there.it'sin contact so you should change

argErrors.rejectValue(field.getName(),
                                field.getName() + " Validation Failed");

to

argErrors.rejectValue("contact.mobile3 ",
                                field.getName() + " Validation Failed");  

that is nested path of the field you are rejecting the value of

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring MVC @Valid Validation with custom HandlerMethodArgumentResolver

From Dev

Spring MVC: How to return custom 404 errorpages?

From Dev

How to configure custom MediaType in Spring MVC?

From Dev

Custom HttpMessageConverter in Spring MVC

From Dev

CausesValidation not working with custom validators

From Dev

Abstract Factory for custom Validators - options discarded?

From Dev

Spring MockMVC - How to mock custom validators running outside of controllers

From Dev

Spring MVC: use different JSR-303 validators on the same bean?

From Dev

Spring MVC validator annotation + custom validation

From Dev

Custom handling for 405 error with Spring Web MVC

From Dev

How to conditionally invoke spring validators

From Dev

Custom queries in JPA in Spring MVC

From Dev

Consume custom jar in spring MVC project

From Dev

Custom HTTP Methods in Spring MVC

From Dev

Spring MVC custom message for HTTP 400

From Dev

Testing custom validators with Minitest

From Dev

Spring MVC - Failed to invoke custom ExceptionHandler

From Dev

asp.net core mvc password validators

From Dev

Custom validators in WTForms using Flask

From Dev

Spring MVC Custom Converter for JSON data not working

From Dev

Spring MockMvc - Custom Validators are not registered/called in Spring Container

From Dev

Spring MockMVC - How to mock custom validators running outside of controllers

From Dev

Spring MVC: use different JSR-303 validators on the same bean?

From Dev

Custom Annotation-driven Formatting Spring MVC

From Dev

Custom Taglib in liferay using Spring MVC & Maven?

From Dev

Tests fail with custom jsr validators with @Inject for Spring boot application

From Dev

Consume custom jar in spring MVC project

From Dev

Django Custom Validators Not Working

From Dev

Proptypes custom validators with Flow

Related Related

  1. 1

    Spring MVC @Valid Validation with custom HandlerMethodArgumentResolver

  2. 2

    Spring MVC: How to return custom 404 errorpages?

  3. 3

    How to configure custom MediaType in Spring MVC?

  4. 4

    Custom HttpMessageConverter in Spring MVC

  5. 5

    CausesValidation not working with custom validators

  6. 6

    Abstract Factory for custom Validators - options discarded?

  7. 7

    Spring MockMVC - How to mock custom validators running outside of controllers

  8. 8

    Spring MVC: use different JSR-303 validators on the same bean?

  9. 9

    Spring MVC validator annotation + custom validation

  10. 10

    Custom handling for 405 error with Spring Web MVC

  11. 11

    How to conditionally invoke spring validators

  12. 12

    Custom queries in JPA in Spring MVC

  13. 13

    Consume custom jar in spring MVC project

  14. 14

    Custom HTTP Methods in Spring MVC

  15. 15

    Spring MVC custom message for HTTP 400

  16. 16

    Testing custom validators with Minitest

  17. 17

    Spring MVC - Failed to invoke custom ExceptionHandler

  18. 18

    asp.net core mvc password validators

  19. 19

    Custom validators in WTForms using Flask

  20. 20

    Spring MVC Custom Converter for JSON data not working

  21. 21

    Spring MockMvc - Custom Validators are not registered/called in Spring Container

  22. 22

    Spring MockMVC - How to mock custom validators running outside of controllers

  23. 23

    Spring MVC: use different JSR-303 validators on the same bean?

  24. 24

    Custom Annotation-driven Formatting Spring MVC

  25. 25

    Custom Taglib in liferay using Spring MVC & Maven?

  26. 26

    Tests fail with custom jsr validators with @Inject for Spring boot application

  27. 27

    Consume custom jar in spring MVC project

  28. 28

    Django Custom Validators Not Working

  29. 29

    Proptypes custom validators with Flow

HotTag

Archive