Custom Validations
Another new feature available as of Smartsite 7.8 is custom validation on specific fields. This feature is available in any Custom DataEditor implementation, as well as within Edit Item for every contenttype field by means of setting the appropriate CTFP('s).
Implementation
To implement custom validation, create a class and implement the interface Smartsite.Manager.DataEditor.ICustomValidation.
This interface defines a single method, Validate, which will be called to execute the custom validation.
It must return a boolean indicating whether or not the field is valid.
The function has two arguments:
- CustomValidationContext context
- string exceptionMessage (out parameter)
The context object contains the Default Instance, the fieldName and the primary key of the record.
(Because the fieldName is available as property, it is possible to implement custom validation for multiple fields within a single class.)
When a field fails to validate, the exceptionMessage should be used to provide the user with the reason why it failed to validate.
Configuration
Custom validation is configured as a property within the DataEditor.xml of a specific (custom) DataEditor action.
Specify the property customvalidation, and set it's value to one or more (use the pipe character "|" as separator) fully qualified names of classes implementing the custom validation(s) (See example below).
Custom validation for contenttype fields within Edit Item can be set using the CTFP cms:customvalidation.
(In this case, the DataEditor.xml is generated on the fly and the customvalidation property is added to the field definition when there's a cms:customvalidation CTFP present.)
Custom validation mode
When generating the XForms, the custom validation is added to the XForms definition using an DataEditor extension function.
However, there are two ways the custom validation can be called:
- onchange
The custom validation will only be called when the value of the field changes (xforms-value-changed event handling) - always
The custom validation will be called every time any field changes (XForms constraint handling)
Use onchange (which is the default) when the custom validation does not depend on the value of any other field, otherwise use always.
The custom validation mode can be set using the property customvalidationmode or the CTFP cms:customvalidationmode.