How to pre-validate whole models in TYPO3 Flow

50 Views Asked by At

We build a TYPO3 Flow model like this:

/*                                                                        *
 * This script belongs to the Flow package "DS.Datenbank".                *
 *                                                                        *
 *                                                                        */

use TYPO3\Flow\Annotations as Flow;
use Some\Where\Domain\Model\User as USER;
use Doctrine\ORM\Mapping as ORM;

/**
 * @Flow\Entity
 */
class Modelname {

    /**
     * The text content 
     * @var string
     * @Flow\Validate(type="NotEmpty")
     * @Flow\Validate(type="String")
     */
    protected $content;

    /**
     * __construct
     *
     * @param $content the content
     */
    public function __construct($content, $depth){
        $this->content = $content;
    }

    /**
     * getContent
     *
     * Returns the content
     * @return $content
     */
    public function getContent(){
        return $this->content;
    }

    /**
     * setContent
     *
     * Set a new content
     * @param $content
     * @return void
     */
    public function setContent($content){
        $this->content = $content;
    }

}
?>

In most cases you use a public function addModelname(\Some\Where\Domain\Model\Modelname $modelname){...} in the ActionController. In this case we need it in another model. But we don't have the auto-validtion with the validatorResolver like in ActionControllers. Is there a simple way to pre-validate whole model objects with theire annotation validation?

1

There are 1 best solutions below

0
Pete On

I was talking about the validatorResolver and did not read the documentation completely. The solution can be found at http://flowframework.readthedocs.io/en/stable/TheDefinitiveGuide/PartIII/Validation.html#validating-domain-models

The adapted example of my problem would be:

$validatorResolver = new \TYPO3\Flow\Validation\ValidatorResolver();
$modelnameValidator = $validatorResolver->getBaseValidatorConjunction('Some.Where\Domain\Model\Modelname');
$result = $commentValidator->validate($modelname);