【问题标题】:How to pre-validate whole models in TYPO3 Flow如何在 TYPO3 Flow 中预验证整个模型
【发布时间】:2016-11-21 11:59:54
【问题描述】:

我们构建一个 TYPO3 Flow 模型,如下所示:

/*                                                                        *
 * 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;
    }

}
?>

在大多数情况下,您在 ActionController 中使用 public function addModelname(\Some\Where\Domain\Model\Modelname $modelname){...}。在这种情况下,我们需要在另一个模型中使用它。但是我们没有像在 ActionControllers 中那样使用 validatorResolver 进行自动验证。有没有一种简单的方法可以通过注释验证来预先验证整个模型对象?

【问题讨论】:

    标签: php validation typo3-flow


    【解决方案1】:

    我说的是validatorResolver,并没有完全阅读文档。可以在http://flowframework.readthedocs.io/en/stable/TheDefinitiveGuide/PartIII/Validation.html#validating-domain-models找到解决方案

    我的问题的改编示例是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-30
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      • 2011-04-02
      • 2013-06-22
      • 2013-06-20
      相关资源
      最近更新 更多