cvc.buildConstraintViolationWithTemplate(remoteKeyInfo.annotation.message()) .addNode(Dom.convertName(remoteKeyInfo.method.getName())) .addConstraintViolation();
@SuppressWarnings("deprecation") @Override public boolean isValid(Group group, ConstraintValidatorContext constraintValidatorContext) { if ( group == null ) { return true; } constraintValidatorContext.disableDefaultConstraintViolation(); for ( int index = 0; index < group.persons.size(); index++ ) { Person person = group.persons.get( index ); if ( !group.gender.equals( person.gender ) ) { constraintValidatorContext .buildConstraintViolationWithTemplate( "constraints.CompatiblePersons.gender.message" ) .addNode( "persons" ) .addNode( null ).inIterable().atIndex( index ) .addConstraintViolation(); return false; } } return true; } }
.addNode("userName").addConstraintViolation(); .addNode("contactMethod").addConstraintViolation(); } else { switch (userContact.getContactOption()) { case email: context.buildConstraintViolationWithTemplate("{vgr.tycktill.usercontact.contactmethod.email}") .addNode("contactMethod").addConstraintViolation(); break; case telephone: context.buildConstraintViolationWithTemplate("{vgr.tycktill.usercontact.contactmethod.telephone}") .addNode("contactMethod").addConstraintViolation(); break; default: context.buildConstraintViolationWithTemplate("{vgr.tycktill.usercontact.contactmethod}") .addNode("contactMethod").addConstraintViolation(); break;
for (String fieldName : fieldNames) { constraintValidatorContext.buildConstraintViolationWithTemplate(message) .addNode(fieldName).addConstraintViolation();
.addPropertyNode("name") .inIterable().atIndex(i) .addConstraintViolation(); return false;
.addPropertyNode( "myNode3" ) .inIterable().atIndex( 84 ) .addBeanNode() .addConstraintViolation(); .addPropertyNode( "myNode4" ) .inIterable().atKey( "AnotherKey" ) .addBeanNode() .addConstraintViolation();
.addPropertyNode( "myNode7" ) .inIterable().atIndex( 42 ) .addConstraintViolation(); .addPropertyNode( "myNode9" ) .inIterable().atKey( "Foo" ) .addConstraintViolation();
@Override public boolean isValid(String value, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate() ) .addPropertyNode( "myNode1" ) .addPropertyNode( "myNode2" ) .inContainer( Map.class, 1 ) .inIterable() .atKey( "key" ) .addBeanNode() .addConstraintViolation(); context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate() ) .addPropertyNode( "myNode3" ) .inContainer( List.class, 0 ) .inIterable() .atIndex( 3 ) .addBeanNode() .addConstraintViolation(); context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate() ) .addPropertyNode( "myNode4" ) .inContainer( Optional.class, 0 ) .addPropertyNode( "myNode5" ) .addConstraintViolation(); return false; } }
/** * validate the following: * <ul> * <li>an interface must have an address when the boot protocol is static * </ul> */ @Override public boolean isValid(VdsNetworkInterface iface, ConstraintValidatorContext context) { Ipv4BootProtocol bootProtocol = iface.getIpv4BootProtocol(); String address = iface.getIpv4Address(); if (bootProtocol != null && bootProtocol == STATIC_IP) { if (isNullOrEmpty(address)) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate("NETWORK_ADDR_MANDATORY_IN_STATIC_IP") .addNode("address").addConstraintViolation(); return false; } } if (!isEmpty(iface.getBondName()) && !validateSlave(iface)) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate("SLAVE_INTERFACE_IS_MISCONFIGURED").addConstraintViolation(); return false; } if (!validateLabel(iface)) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate("IMPROPER_INTERFACE_IS_LABELED").addConstraintViolation(); } return true; }
/** * {@inheritDoc} */ @Override public boolean isValid(UserSecurityDto dto, ConstraintValidatorContext context) { JCUser currentUser = userService.getCurrentUser(); String editedUserName = getUsername(dto.getUserId()); String currentUserName = currentUser.getUsername(); boolean isWillBeChangedByOwner = ObjectUtils.equals(editedUserName, currentUserName); if (isWillBeChangedByOwner) { boolean result = dto.getNewUserPassword() == null; //we must compare the hashes, so we encrypt the entered value String enteredCurrentPassword = encryptionService.encryptPassword(dto.getCurrentUserPassword()); result |= currentUser.getPassword().equals(enteredCurrentPassword); if (!result) { // add validation error to the field context.buildConstraintViolationWithTemplate(message) .addNode("currentUserPassword") .addConstraintViolation(); } return result; } return true; }
/** * {@inheritDoc} */ @Override public boolean isValid(UserProfileDto userProfile, ConstraintValidatorContext context) { String changedEmail = userProfile.getEmail(); if (changedEmail == null) { // null emails checks are out of scope, pls use separate annotation for that return true; } String currentUserEmail = getEmailOfUser(userProfile.getUserId()); if (ObjectUtils.equals(changedEmail, currentUserEmail)) { return true; // no changes in an email, that's ok } else { boolean userWithTheSameChangedEmailExists = userDao.getByEmail(changedEmail) != null; if(userWithTheSameChangedEmailExists){ context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate(message) .addNode("email") .addConstraintViolation(); } return !userWithTheSameChangedEmailExists; } }
@Override public boolean isValid(Object value, ConstraintValidatorContext context) { Class<?> type = value.getClass(); Field startField = getField(annotation.start(), type); Field endField = getField(annotation.end(), type); Date startValue = getValue(startField, value); if(startValue == null) { return true; } Date endValue = getValue(endField, value); if(endValue == null) { return true; } if(endValue.before(startValue)) { context.buildConstraintViolationWithTemplate("EndBeforeStart") .addNode(annotation.end()) .addConstraintViolation(); return false; } return true; }
@Override public boolean isValid(PageRangeSelection value, ConstraintValidatorContext context) { if (value != null) { List<PageRange> ranges = new ArrayList<PageRange>(value.getPageSelection()); for (int i = 0; i < ranges.size(); i++) { PageRange range = ranges.get(i); for (int j = i + 1; j < ranges.size(); j++) { PageRange current = ranges.get(j); if (range.intersects(current)) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("Invalid page ranges, found an intersection between %s and %s", range, current)).addNode("page ranges").addConstraintViolation(); return false; } } } } return true; }
@Override @SuppressWarnings("deprecation") public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) { if ( disableDefaultError ) { constraintValidatorContext.disableDefaultConstraintViolation(); } if ( errorMessages != null ) { for ( Map.Entry<String, String> entry : errorMessages.entrySet() ) { if ( entry.getKey() == null ) { constraintValidatorContext.buildConstraintViolationWithTemplate( entry.getValue() ) .addConstraintViolation(); } else { constraintValidatorContext.buildConstraintViolationWithTemplate( entry.getValue() ) .addNode( entry.getKey() ) .addConstraintViolation(); } } } return false; }
@Override public boolean isValid(java.lang.Object[] value, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate() ) .addParameterNode( 0 ) .addConstraintViolation(); context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate() ) .addParameterNode( 0 ) .addBeanNode() .addConstraintViolation(); context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate() ) .addParameterNode( 0 ) .addPropertyNode( "myNode1" ) .inIterable().atIndex( 23 ) .addBeanNode() .addConstraintViolation(); return false; } }
public boolean isValid(Group group, ConstraintValidatorContext constraintValidatorContext) { if ( group == null ) { return true; } constraintValidatorContext.disableDefaultConstraintViolation(); for ( int index = 0; index < group.persons.size(); index++ ) { Person person = group.persons.get( index ); if ( !group.gender.equals( person.gender ) ) { constraintValidatorContext .buildConstraintViolationWithTemplate( "constraints.CompatiblePersons.gender.message" ) .addNode( "persons" ) .addNode( null ).inIterable().atIndex( index ) .addConstraintViolation(); return false; } } return true; } }
@Override public boolean isValid(PageRangeSelection value, ConstraintValidatorContext context) { if (value != null) { List<PageRange> ranges = new ArrayList<PageRange>(value.getPageSelection()); for (int i = 0; i < ranges.size(); i++) { PageRange range = ranges.get(i); for (int j = i + 1; j < ranges.size(); j++) { PageRange current = ranges.get(j); if (range.intersects(current)) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("Invalid page ranges, found an intersection between %s and %s", range, current)).addNode("page ranges").addConstraintViolation(); return false; } } } } return true; }
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) { if ( disableDefaultError ) { constraintValidatorContext.disableDefaultConstraintViolation(); } if ( errorMessages != null ) { for ( Map.Entry<String, String> entry : errorMessages.entrySet() ) { if ( entry.getKey() == null ) { constraintValidatorContext.buildConstraintViolationWithTemplate( entry.getValue() ).addConstraintViolation(); } else { constraintValidatorContext.buildConstraintViolationWithTemplate( entry.getValue() ) .addNode( entry.getKey() ) .addConstraintViolation(); } } } return false; }
@Override public boolean isValid(SingleOutputTaskParameters value, ConstraintValidatorContext context) { if (nonNull(value) && nonNull(value.getOutput()) && ArrayUtils.isNotEmpty(extensions)) { String fileName = value.getOutput().getDestination().getName(); if (hasAllowedExtension(fileName)) { return true; } context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("The output '%s' is not one of the expected types: %s", fileName, ArrayUtils.toString(extensions))).addNode("taskOutput").addConstraintViolation(); return false; } return true; }
@Override public boolean isValid(SingleOutputTaskParameters value, ConstraintValidatorContext context) { if (nonNull(value) && nonNull(value.getOutput()) && ArrayUtils.isNotEmpty(extensions)) { String fileName = value.getOutput().getDestination().getName(); if (hasAllowedExtension(fileName)) { return true; } context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("The output '%s' is not one of the expected types: %s", fileName, ArrayUtils.toString(extensions))).addNode("taskOutput").addConstraintViolation(); return false; } return true; }