TYPO3模型验证

时间:2018-07-28 12:56:35

标签: php typo3 typo3-8.x

控制器:

    /**
     * @param \Mvnaz\Corn\Domain\Model\BookedDate|null $book
     * @validate $book
     */
    public function newBookFormAction(\Mvnaz\Corn\Domain\Model\BookedDate $book = null)
    {
        $userList = $this->userRepository->buildList();
        if($book){
//            $user = $book->getUser();
//            $book->setSortCsvIndex($user->getCornSortCsvIndex());

        }

        else
            $book = new BookedDate();

        $this->view->assign("book", $book);
        $this->view->assign("userList", $userList);
    }

模型字段:

 /**
     * @var float
     * @validate NotEmpty
     */
    protected $volume;

    /**
     * @var string
     * @validate NotEmpty
     */
    protected $date;

    /**
     * @var int
     */
    protected $sortCsvIndex;

    /**
     * @var \Mvnaz\Corn\Domain\Model\User
     * @validate NotEmpty
     */
    protected $user = null;

如果我使用跟随视图字段进行验证,则将错误类添加到输入,并显示错误文本:

<div class="form-group">
    <label for="volume">
        <f:translate key="volume"/>
    </label>
    <f:form.textfield property="volume" class="form-control" id="volume" />
    <f:render partial="ValidatingErrors" arguments="{field: 'book.volume'}"/>
</div>


<label for="date">
    <f:translate key="date"/>
</label>
<div class='input-group date' id='date-picker-admin'>
    <f:form.textfield property="date" class="form-control" id="date" />
    <span class="input-group-addon">
        <span class="glyphicon glyphicon-time"></span>
    </span>
</div>

如果我添加此字段,则验证无效:

<div class="form-group">
    <label for="user">
        <f:translate key="user"/>
    </label>
    <f:form.select property="user" options="{userList}" id="user" class="form-control"/>
</div>

我很困惑,我尝试了一切!我也注意到如果我不通过userList-验证有效!我知道哪里出了问题。对象BookedDate包含对象User,但是User包含BookedDates的存储。这破坏了验证。怎么解决呢?

2 个答案:

答案 0 :(得分:0)

  1. 对于Extbase操作,您需要先使用PHPDoc块来描述参数。您的功能为空
  2. 在您的Fluid模板中,您需要<f:validationResults>部分,如下所述:https://docs.typo3.org/typo3cms/ExtbaseGuide/Fluid/ViewHelper/Form/ValidationResults.html
  3. 请仔细阅读以下验证文档:https://docs.typo3.org/typo3cms/ExtbaseFluidBook/9-CrosscuttingConcerns/2-validating-domain-objects.html

答案 1 :(得分:0)

https://forge.typo3.org/issues/84475这就是答案。发生我的问题是因为我具有循环关系,而且typo3验证程序在其他子对象中对同一属性进行验证后停止验证该属性。

ext_localconf.php:

$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects']['TYPO3\\CMS\\Extbase\\Validation\\Validator\\GenericObjectValidator'] = array(
    'className' => 'Mvnaz\\Corn\\Xclass\\GenericObjectCustomValidator'
);

Classes \ Xclass \ GenericObjectCustomValidator.php:

<?php
namespace Mvnaz\Corn\Xclass;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use \TYPO3\CMS\Extbase\Reflection\ObjectAccess;

/**
 * A generic object validator which allows for specifying property validators
 */
class GenericObjectCustomValidator extends \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator
{
    /**
     * Checks if the given value is valid according to the validator, and returns
     * the Error Messages object which occurred.
     *
     * @param mixed $value The value that should be validated
     * @return \TYPO3\CMS\Extbase\Error\Result
     * @api
     */
    public function validate($value)
    {
        $this->result = $this->result ?: new \TYPO3\CMS\Extbase\Error\Result();
        if ($this->acceptsEmptyValues === false || $this->isEmpty($value) === false) {
            if (!is_object($value)) {
                $this->addError('Object expected, %1$s given.', 1241099149, [gettype($value)]);
            } elseif ($this->isValidatedAlready($value) === false) {
                $this->isValid($value);
            }
        }

        return $this->result;
    }

}