Rudi
Rudi

Reputation: 13

Perform validation on domain model

Our business domain model has complex validation rules, which can only be executed when the model has been updated.

Something like this:

public classs MyDomainModel {
    public DomainValidationContext getValidationContext() {
       ...
    }
}

We don't want to extract these, since:

  1. it would mean duplication of the validation rules.
  2. List item some rules are only "soft-rules" which should result in warning messages without stopping further processing.

We considered duplicating the DomainModel in the Validationr updating the value and checking for validation errors/warnings.

public class SomeValidator implements Validator {
   @Inject
   private DomainEditContext domainEditContext;

   public void validate(final FacesContext context, final UIComponent component, final Object value) {
       MyDomainModel validationModel = domainWorkContext.getDomainModel().clone();
       validationModel.setSomeValue(value);
       DomainValidationContext dvc = validationModel.getValidationContext();
       ...
   }
}

However this is fairly error prone and processing intensive since a full deep copy of the domain model has to be made for every validation.

Is there any better way to perform validations post model update/invoke-application or prior to render-response?

Any other ideas are more than welcome.

Upvotes: 1

Views: 91

Answers (1)

dngfng
dngfng

Reputation: 1943

Have a look at JsfWarn it performs validations after model-update and invoke application.

Enabling you to retrieve the validation context straight from the updated model. Your warning validator will look something like this:

public class SomeValidator implements WarningValidator {
   @Inject
   private DomainEditContext domainEditContext;

   public void validate(FacesContext context, UIComponent component, ValidationResult validationResult) {
       MyDomainModel myModel = domainEditContext.getMyDomainModel();
       DomainValidationContext dvc = myModel.getValidationContext();
       if(dvc.isSomeValueInvalid()) {
          validationResult.setFacesMessage(....);
       }
   }
}

Similar to f:validator you have to add jw:warning to the targeted component.

<h:inputText id="someValue">
    <jw:warning validator="#{someValidator}" />
    <f:ajax event="change" render="..." />
</h:inputText>
<h:message for="someValue" />

Upvotes: 1

Related Questions