mrfr34k
mrfr34k

Reputation: 85

TYPO3 Custom Validation not working

My Custom Extbase Validation is not working. I get following Error.

Version TYPO3 7.6.16

I'm trying to validate the input.

Invalid validate annotation in VENDOR\ex\Domain\Model\GpSubscriber::salutation: Could not resolve class name for validator ""

My Model GpSubscriber.php

<?php
namespace VENDOR\ex\Domain\Model;

/**
* GpSubscriber
*/
class GpSubscriber extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
 * salutation
 *
 * @var string
 * @validate \VENDOR\ex\Domain\Validator\SalutationValidator
 */
protected $salutation = '';

My Validator SalutationValidator.php

<?php
namespace VENDOR\ex\Domain\Validator;


class SalutationValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
public function isValid($value) {
    if ($value == 'false') {
        $this->addError('error.');
        return FALSE;
    }
    return TRUE;
}
}

Where is my Problem?

Upvotes: 0

Views: 2709

Answers (3)

John Miller
John Miller

Reputation: 717

@validate is deprecated since version 9.3. In 7+ use @TYPO3\CMS\Extbase\Annotation\Validate instead.

Example:

use TYPO3\CMS\Extbase\Annotation as Extbase;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

class GpSubscriber extends AbstractEntity {

    /**
     * @var string
     * @Extbase\Validate("VENDOR\ex\Domain\Validator\SalutationValidator")
     */
    protected $title;

...
}

This way, you can still store your validator in Model/Validator directory.

If you choose to store your validator under Validation/Validator in your extension, you may choose to use @Extbase\Validate("VENDOR.ex:Salutation") or @Extbase\Validate("VENDOR\ex\Validation\Validator\SalutationValidator") to access your validator.

Notes:

  • Ensure your validator namespace in @Extbase\Validate doesn't start with a slash.
  • Ensure getters and setters for all defined properties.
  • Ensure all properties (fields from view) in your model (in this case class GpSubscriber) are defined.

More information:

Upvotes: 0

mrfr34k
mrfr34k

Reputation: 85

I dumped the autoload cache by disabling the extension and activated it again. After that everything was working

Upvotes: 4

Heinz Schilling
Heinz Schilling

Reputation: 2252

Try to move your class SalutationValidator to extension/Validation/Validator/SalutationValidator.php

<?php
namespace VENDOR\ex\Domain\Model;

/**
* GpSubscriber
*/
class GpSubscriber extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{

    /**
     * salutation
     *
     * @var string
     * @validate \VENDOR\ex\Validation\Validator\SalutationValidator
     */
    protected $salutation = '';

 

<?php
namespace VENDOR\ex\Validation\Validator;

class SalutationValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {

    /**
     * The given value is valid if it contains not more then max items
     *
     * @param mixed $value The value that should be validated
     * @return void
     */
    public function isValid($value) {
        if ($value == 'false') {
            $this->addError('error.');
            return FALSE;
        }
    return TRUE;
    }
}

Deactivate and activate again your extension and classes will reloaded.

Upvotes: 1

Related Questions