Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

There is no special requirement for constraint-validators. As soon as the bean-validation module has been added to a project it's possible to use dependency injection in std. constraint-validators.

Code Block
java
java
titleInject beans in constraint validatorsjava
public class UserNameValidator implements ConstraintValidator<UserName, String>
{
    @Inject
    private UserService userService;

    public void initialize(UserName userName)
    {
    }

    public boolean isValid(String userName, ConstraintValidatorContext constraintValidatorContext)
    {
        return this.userService.validateUserName(userName);
    }
}

...

CODI produces injectable (dependent) proxies which dynamically lookup the cached ValidatorFactory for creating new instances dynamically. So you don't have to care about correct caching of different bean-validation artifacts. E.g. every call of Validator#validate will automatically resolve the cached ValidatorFactory, creates a new Validator (because it isn't allowed to cache it) and call #validate on it. Everything a user has to do is:

Code Block
java
java
titleInject bean-validation artifactsjava
public class DemoBean
{
    @Inject
    public DemoBean(@Advanced Validator validator)
    {
        this.validator = validator;
        validate(); //uses the injected Validator - e.g.: validator.validate(this);
    }
    //...
}

...

Note
titleHint

This feature is currently only available in combination with the JSF module.

Code Block
java
java
titleSimple usage of the invalidValue placeholderjava
@Constraint(validatedBy = DifferentNameValidator.class)
@Target(TYPE)
@Retention(RUNTIME)
public @interface DifferentName
{
    String message() default "The same name '{invalidValue}' isn't allowed.";

    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}


@ApplicationScoped
public class DifferentNameValidator extends ClassLevelConstraintValidator<DifferentName, Person>
{
    private static final long serialVersionUID = 3851988368625335444L;

    @Inject
    private ValidationService validationService;

    private String invalidValue;

    @Override
    protected boolean isValidInstance(Person person, ConstraintValidatorContext constraintValidatorContext)
    {
        boolean result = this.validationService.isValid(person);

        if(result)
        {
            this.invalidValue = null;
        }
        else
        {
            this.invalidValue = person.getFirstName();
        }
        return result;
    }

    @Override
    protected Serializable getInvalidValue()
    {
        return this.invalidValue;
    }
}