0

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.

4
  • 1
    Hope this helps codetutr.com/2013/05/29/… Commented Sep 22, 2013 at 14:55
  • Hay Thanq. But I have a doubt is it using Spring Validation. How can I integrate with Spring controller...? Also the example class has two variables but In my example I have an extra reference variable. The problem with this reference variables. How can I solve these kind of problems... Commented Sep 22, 2013 at 15:05
  • Can you show what calidationFieldsList holds? Commented Sep 22, 2013 at 15:10
  • Hi, I added my whole validator code. You can see now. Commented Sep 22, 2013 at 15:24

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hay Thanq Man. Its work foe me. Hay Now I am not getting this issue but I was unable to display my errors in the jsp page. Can you please help me on this. I tried but I failed. Can you please help me on this...?
Yes you have to use spring form tag for this path is the nested path in this case contact.mobile3 <form:errors path="contact.mobile3" /> read here docs.spring.io/spring/docs/3.2.x/spring-framework-reference/… it explains everything in detail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.