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.
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.
Comments