Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Slice 3.0 introduces a simple validation mechanism which allows you to check if a model meets certain requirements or, in other words, is valid. Thanks to validation tag, a component whose model is not valid may not be displayed on a page.

Basic interfaces

The validation mechanism is based on two interfaces:

Validatable

If a class should be a subject to validation it should implement com.cognifide.slice.validation.api.Validatable. The interface introduces only one method: validate, which takes one argument: com.cognifide.slice.validation.api.ValidationResultBuilder. The argument allows you to define a validation messages of different levels. The levels include:

...

Code Block
import org.apache.commons.lang.StringUtils;
import com.cognifide.slice.mapper.annotation.JcrProperty;
import com.cognifide.slice.mapper.annotation.SliceResource;
import com.cognifide.slice.validation.api.Validatable;
import com.cognifide.slice.validation.api.ValidationResultBuilder;

@SliceResource
public class TextModel implements Validatable {

    @JcrProperty
    private String text;

    @Override
    public void validate(ValidationResultBuilder result) {
        if (StringUtils.isBlank(text)) {
            result.addErrorMessage("Text cannot be empty");
        }
    }

    public String getText() {
        return text;
    }
}

Validator

The actual validation of an object is performed by com.cognifide.slice.validation.api.Validator interface. It defines one method: validate, which takes Validatable object as an argument. It returns ValidationResult as a result of validation. ValidationResult can be used for reading a result of validation and validation messages.

...

Code Block
import com.cognifide.slice.api.model.InitializableModel;
import com.cognifide.slice.api.provider.ModelProvider;
import com.cognifide.slice.mapper.annotation.JcrProperty;
import com.cognifide.slice.mapper.annotation.SliceResource;
import com.cognifide.slice.validation.api.Validator;
import com.cognifide.app.components.configuration.image.ImageConfigurationModel;
import com.google.inject.Inject;

@SliceResource
public class ImageModel implements InitializableModel {

    @JcrProperty
    private String configPath;

    private int size;

    private final ModelProvider modelProvider;

    private final Validator validator;
   
    @Inject
    public ImageModel(final ModelProvider modelProvider, final Validator validator) {
        this.modelProvider = modelProvider;
        this.validator = validator;
    }

    @Override
    public void afterCreated() {
        ImageConfigurationModel configurationModel = modelProvider.get(ImageConfigurationModel.class, configPath);
        if (validator.validate(configurationModel).isValid()) {
            size = configurationModel.getDefaultSize();
        } else {
            ...
        }
    }
    ...
}

Validation tag

To render a validation message to an end-user (author), you need to use a dedicated JSP tag - validate tag defined in the http://cognifide.com/jsp/slice/validation taglib. The example below shows an simple usage of it.

...

AttributeDescriptionRequired?
objectobject that needs to be validated(tick)
titleHeading of validation messages displayed to authors, "Validation messages:" by default(error)
displayErrorsDefines if error messages should be displayed or not. The boolean value which should be based on CQ mode(error)
varVariable where validation result will be stored for further use(error)

Blank values approach

There is a special case of validation where we'd like to inform authors that the component has not been edited yet and it needs author's attention. This is especially useful when an author drags and drops a new component onto paragraph system, and the component needs to render itself. Usually, such component cannot pass validation without being edited first what's resulting in displaying error messages. This can be, in turn, misleading for authors.

...