Deprecation: #83167 - Replace @validate with @TYPO3\CMS\Extbase\Annotation\Validate

See forge#83167

Description

The @validate annotation has been marked as deprecated and should be replaced with the doctrine annotation @TYPO3\CMS\Extbase\Annotation\Validate.

Impact

Classes using @validate will trigger a PHP E_USER_DEPRECATED error.

Affected Installations

All extensions that use @validate

Migration

Use @TYPO3\CMS\Extbase\Annotation\Validate instead.

Examples:

The following examples show both the old and the new way of using validation annotations. Both versions can still be used in the same doc block, but you should start using the new way today.

use TYPO3\CMS\Extbase\Annotation as Extbase;

Note

Doctrine annotations are actual classes, so they can be either used via FQCN, imported via the use statement or even be aliased which is the preferred way. As doctrine annotations can only be used in the Extbase context (for now), the aliased version makes that perfectly clear even for people that are new to TYPO3.

Tip

When using PhpStorm, you can install the PHP Annotation plugin that recognizes the annotation classes and makes you jump directly into them. Also, it enables autocompletion for annotation options.

Validators for class properties

This is how annotations look like, that register validators without options

/**
 * @validate NotEmpty
 * @Extbase\Validate("NotEmpty")
 * @var Foo
 */
public $property;
/**
 * @validate NotEmpty
 * @Extbase\Validate(validator="NotEmpty")
 * @var Foo
 */
public $property;

This is how annotations look like, that register validators with options

/**
 * @validate StringLength(minimum=3, maximum=50)
 * @Extbase\Validate("StringLength", options={"minimum": 3, "maximum": 50})
 * @var Foo
 */
public $property;

Important

Registering multiple validators, separated by comma, is not possible any more. Instead, use one validator per line.

/**
 * @validate StringLength(minimum=3), StringLength(maximum=50)
 * @Extbase\Validate("StringLength", options={"minimum": 3})
 * @Extbase\Validate("StringLength", options={"maximum": 50})
 * @var Foo
 */
public $property;

Validators for method params

Important

When using validators for method params, you need to define what param the validator is registered for. Also, please note that the param name does no longer include the dollar sign.

/**
 * @validate $bar NotEmpty
 * @Extbase\Validate("NotEmpty", param="bar")
 * @var string $foo
 * @var string $bar
 */
public function method(string $foo, string $bar)
{
}

Full qualified validator class names and aliases

Of course it's still possible to reference validators by extension key, aliases and FQCN's.

/**
 * @Extbase\Validate("NotEmpty")
 * @Extbase\Validate("TYPO3.CMS.Extbase:NotEmpty")
 * @Extbase\Validate("TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator")
 * @Extbase\Validate("\TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator")
 */
protected $property;