Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC custom validators
    text
    copied!<p>I am validating my spring form for that I wrote my own validator. In that I was unable to show my error codes.</p> <p>I have a model/pojo class.</p> <pre><code>public class Person{ private String dob; @InHouse private Contact contact; //getters and setters } </code></pre> <p>So here "Contact" is another class and it has 3 variables.</p> <pre><code>public class Contact{ private String mobile1; private String mobile2; private String mobile3; //getters and setters } </code></pre> <p>All my hibernate connections are fine.</p> <p>Below is my custom validator.</p> <pre><code> @Override public void validate(Object argTarget, Errors argErrors) { Person person = (Person) argTarget; validate(argTarget.getClass(), argErrors, person); List&lt;Field&gt; 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&lt;?&gt; argClass, Errors argErrors, Object argObject) { List&lt;Field&gt; 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) &amp;&amp; Precondition.checkNotNull(result)) { com.rise.validation.Validation&lt;String, String&gt; validation = (com.rise.validation.Validation&lt;String, String&gt;) 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(); } } } } </code></pre> <p>In the above code "InHouse" is my custom annotation. Contact(Person Class) variable has this annotation.</p> <p>In my input I gave a wrong phone number for <strong>mobile3</strong> field.</p> <p>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.</p> <p><strong>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?</strong></p> <p>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 <strong>argErrors</strong> object.</p> <p>Can any one help me on this.</p> <p><strong>Note:</strong> Here I am using reflection to iterate over the fields. For example First I will iterate over <strong>Person</strong> fields (dob) then I will get the <strong>Contact</strong> object from <strong>Person</strong> then I will iterate over <strong>Contact</strong> fields.</p> <p>I was stuck here. Please help me on this and also is it the right way to write <strong>Custom Validators</strong>.</p> <p>Thanq, Amar.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload