eZ Publish Developer/Specialist / Blog / Enhanced content edit handler for validation rules in eZ Publish 4.1

blog

A blog about developing websites with the eZ Publish Content Management System. It contains material such as news, how-to articles, tutorials, template and PHP code, and specific solutions.

Stay up-to-date and subscribe to the blog's RSS feed.

maingfx

Enhanced content edit handler for validation rules in eZ Publish 4.1

One of the new features in the upcoming eZ Publish 4.1 release is an enhanced content edit handler, which enables you to implement custom validation rules. Based on Kristof Coomans' "Object validation" extension, eZ Publish 4.1 will include the possibility to insert custom validation methods in the content object storing process.

There are many potential uses for this enhanced handler, including cross-attribute validation; enforcement of storage quotas when uploading files or creating content; CAPTCHA checks without needing to create datatypes; and much more.

Let's see how it works in practice. We use the custom edit handler with one additional method responsible for validation - “validateInput”. You can learn more about the custom edit handler here.

In its simplest form, it looks like this:

 
class MyCustomValidatorHandler extends eZContentObjectEditHandler
{
 
    function __construct()
    {
    }
 
 
    function fetchInput( $http, &$module, &$class, $object, &$version, $contentObjectAttributes, $editVersion, $editLanguage, $fromLanguage )
    {
    }
 
    static function storeActionList()
    {
    }
 
    function publish( $contentObjectID, $contentObjectVersion )
    {
    }
 
    function validateInput( $http, &$module, &$class, $object, &$version, $contentObjectAttributes, $editVersion, $editLanguage, $fromLanguage, $validationParameters )
    {
        $result = array( 'is_valid' => true, 'warnings' => array() );
        
        return $result;
    }
}

The handler must be placed in: extension/<extension-name>/content/<extension-name>handler.php. As always, the custom edit handler must be registered in a content.ini.append.php file as follows:

[EditSettings]
ExtensionDirectories[]= mycustomvalidator

Now a few notes about the validateInput method. The result is an array where one item is an is_valid boolean value, informing the system about the validation status; the second item is a warnings array where you can put relevant information about what went wrong.

Example:

 
    function validateInput( $http, &$module, &$class, $object, &$version, $contentObjectAttributes, $editVersion, $editLanguage, $fromLanguage, $validationParameters )
    {
        $result = array( 'is_valid' => true, 'warnings' => array() );
        $isValid =& $result['is_valid'];
        $warnings =& $result['warnings'];
 
        $phrase = $http->postVariable( 'ExampleTextField' );
 
        if( $phrase == '' )
        {
            $warnings[] = array( 'text' => ezi18n( 'extension/mycustomvalidator', 'The verification word is not valid' ) );
            $isValid = false;
        }
        return $result;
    }

When the validation fails (in this simple case, the user has left a required text field empty), the "The verification word is not valid" text is displayed to user.

Comments

Can I make custom remove or addAssignment handler?

Hello, Łukasz!

Is there a way of creation of user's additional action on standard action, such as ActionRemove or AddAssignmentButton?