简体   繁体   中英

How to write a Validator in TYPO3 CMS 7+?

I am about to write a validator in my TYPO3 CMS 7.6.x extension. I use the modern MVC struktur including Fluid (so not the old pi1 system). Here is my test validator (MyownValidator.php):

namespace My\Extension\Validation\Validator;
 /**
  * MyownValidator
  */
class MyownValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
    /**
     * ...
     */
    public function isValid($data){
        if( $data != 'hello world' ){
            $this->addError('ERRORMESSAGE', 1468329929);
            return FALSE;
        }
        return TRUE;    
    }

}

And in the Model Class I add the validation line:

/**
 * testattribute
 *
 * @var string
 * @validate Myown
 */
protected $testattribute = '';

After clearing the complete cache in the install tool nothing happens. I still can create objects of this model with any value for testattribute.

And then I had a look in the TCA configuration and found that:

'eval' => 'trim,required'

Is there any way to use the validation of the models like TYPO3 Fluid does? Or do I have to write Validators like in TYPO3 CMS 4?

My mistake. I have to give the full qualified classname and then it works fine.

/**
 * testattribute
 *
 * @var string
 * @validate NotEmpty, \My\Extension\Validation\Validator\MyownValidator
 */
protected $testattribute = '';

Well at least in the front end it shows the validation error. Is there a way to use it in the TCA as well?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM