Cross field validation with HibernateValidator displays no error messages

后端 未结 1 1252
灰色年华
灰色年华 2020-12-08 08:49

I\'m validating two fields, \"password\" and \"confirmPassword\" on the form for equality using HibernateValidator as specified in this answer. The following is the cons

相关标签:
1条回答
  • 2020-12-08 09:00

    Could you try your isValid method to be like this? (this is certainly working for me in live project):

     public boolean isValid(final Object value, final ConstraintValidatorContext cvc){
        boolean toReturn = false;
    
        try{
            final Object firstObj = BeanUtils.getProperty(value, firstFieldName );
            final Object secondObj = BeanUtils.getProperty(value, secondFieldName );
    
            //System.out.println("firstObj = "+firstObj+"   secondObj = "+secondObj);
    
            toReturn = firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);
        }
        catch (final Exception e){
            System.out.println(e.toString());
        }
        //If the validation failed
        if(!toReturn) {
            cvc.disableDefaultConstraintViolation();
            //In the initialiaze method you get the errorMessage: constraintAnnotation.message();
            cvc.buildConstraintViolationWithTemplate(errorMessage).addNode(firstFieldName).addConstraintViolation();
        }
        return toReturn;
    }
    

    Also I see that you are implementing the ConstraintValidator interface with an Object, literally. It should be the backing object that you have from your form:

    tempBean // the one that you specify in the commandName actually.

    So you implementation should like this:

     implements ConstraintValidator<FieldMatch, TempBean>
    

    This is probably not the issue here, but as a future reference, this is how it should be.

    UPDATE

    Inside your FieldMatch interface/annotation you have two methods : first and second, add one more called errorMessage for example:

      Class<? extends Payload>[] payload() default {};
    
    /**
     * @return The first field
     */
    String first();
    
    /**
     * @return The second field
     */
    String second();
    
    /**
      @return the Error Message
     */
    String errorMessage
    

    Look inside you method from the Validation class - you are getting the first and second field names there., so just add the errorMessage, like this for example:

      private String firstFieldName;
      private String secondFieldName;
      //get the error message name
      private String errorMessagename; 
    public void initialize(final FieldMatch constraintAnnotation)
    {
        firstFieldName = constraintAnnotation.first();
        secondFieldName = constraintAnnotation.second();
        errorMessageNAme = constraintAnnotation.errorMessage(); 
    
        //System.out.println("firstFieldName = "+firstFieldName+"   secondFieldName = "+secondFieldName);
    }
    

    Inside isValida get it, the same way you do for first and second field name and use it.

    0 讨论(0)
提交回复
热议问题