[英]TYPO3 model validation
Controller: 控制器:
/**
* @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);
}
Model fields: 型号字段:
/**
* @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;
if I use follow view fields validating is working, error class added to input and error text shown: 如果我使用跟随视图字段验证有效,则将错误类添加到输入,并显示错误文本:
<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>
if I add this field, validation not working: 如果我添加此字段,则验证无效:
<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>
I am confused, I tried everything! 我很困惑,我尝试了一切! Also I noticed if I don't pass userList - validation works!
我也注意到如果我不通过userList-验证有效! I understood where is a problem.
我知道哪里出了问题。 Object BookedDate contains object User, but User contains Storage of BookedDates.
对象BookedDate包含对象User,但是User包含BookedDates的存储。 This breaks validation.
这破坏了验证。 How solve it ?
怎么解决呢?
<f:validationResults>
part, described here: https://docs.typo3.org/typo3cms/ExtbaseGuide/Fluid/ViewHelper/Form/ValidationResults.html <f:validationResults>
部分,如下所述: https : //docs.typo3.org/typo3cms/ExtbaseGuide/Fluid/ViewHelper/Form/ValidationResults.html https://forge.typo3.org/issues/84475 This is the answer. https://forge.typo3.org/issues/84475这就是答案。 My problem happened because I have circular relations, and typo3 validator stoped validate the same property after it was validated in other child object.
发生我的问题是因为我具有循环关系,而且typo3验证程序在其他子对象中对同一属性进行验证后停止验证该属性。
ext_localconf.php: 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: 类\\ 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.