Studio Developer Manual / Version 2307
Table Of ContentsWrite requests that violate hard constraints of your content type model can be aborted when a validator fails. Typical use cases include:
Preventing a client from uploading an image that is too large.
Making sure that a content item does not link to itself directly.
Caution
Blocking writes is not normally useful for text properties, because text values are saved continuously as the user enters data, and a write interceptor might not be able to operate appropriately during the first saves. For blobs or link lists, the impact on the user experience is typically less of a problem. In any case, you need to make sure that the user experience is not impacted negatively.
For implementing immediate validation, you can create an instance of the class
ValidatingContentWriteInterceptor
as a Spring bean and populate its
validators
property with a list of PropertyValidator
objects. When the
validators are configured to report an error issue, an offending write will not be executed
(that is, the requested value will not be saved).
A configuration that limits the size of images in the data
property of
CMPicture
content items to 1 Mbyte might look like this (class names are wrapped for
layout reasons):
@Bean ValidatingContentWriteInterceptor myValidatingContentWriteInterceptor(MaxBlobSizeValidator myMaxBlobSizeValidator) { ValidatingContentWriteInterceptor validatingContentWriteInterceptor = new ValidatingContentWriteInterceptor(); validatingContentWriteInterceptor.setType("CMPicture"); validatingContentWriteInterceptor.setValidators( Collections.singletonList(myMaxBlobSizeValidator))); return validatingContentWriteInterceptor; } @Bean MaxBlobSizeValidator myMaxBlobSizeValidator() { MaxBlobSizeValidator maxBlobSizeValidator = new MaxBlobSizeValidator(); maxBlobSizeValidator.setProperty("data"); maxBlobSizeValidator.setMaxSize(1000000); return maxBlobSizeValidator; }
Example 9.98. Configuring Immediate Validation
Remember that the validators become active during creation, too, so that an immediate validator might validate initial values set by an earlier write interceptor.