TYPO3扩展:如果用户输入错误值,防止记录被保存
我想在TYPO3中为TCA中的特定字段实施评估,以防止记录被保存在后端,如果BE用户输入限制值(例如,如果不输入任何内容或输入包含特定字符)。到目前为止,我只能改变eval函数中的值,如果它不被允许的话,但是如果BE用户在字段限制值时点击'保存',我想要保存记录不被保存。这甚至可以通过评估吗?TYPO3扩展:如果用户输入错误值,防止记录被保存
编辑:一个便宜的方法可以在eval函数中将$ value设置为NULL,如果输入是受限值的,但这绝对不是执行任务的优雅方法,因为它会抛出SQL错误混淆BE用户。
所以我基本上需要一种方法来防止TYPO3坚持资源库...或者可以设置录制回到它是在是用户在“保存”点击前的状态...
EDIT2:这就是我所拥有的......没有什么令人兴奋的,只是一个IPv4评估。但是,它只会将值更改为其他值,如果输入不是IPv4,它不会阻止创建或编辑记录。
<?php namespace Cjk\Icingaconfgen\Evaluation; 
use TYPO3\CMS\Core\Messaging\FlashMessage; 
use TYPO3\CMS\Core\Messaging\FlashMessageService; 
use TYPO3\CMS\Core\Utility\GeneralUtility; 
/** 
* Class for field value validation/evaluation to be used in 'eval' of TCA 
*/ 
class IPv4Evaluation 
{ 
    /** 
    * @param string $value 
    * @param string $is_in 
    * @param bool $set 
    * @return string 
    */ 
    public function evaluateFieldValue($value, $is_in, &$set) 
    { 
     if (!filter_var($value, FILTER_VALIDATE_IP)){ 
      $value = 'Fehlerhafte Eingabe (IPv4)'; 
      /** @var FlashMessage $message */ 
      $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', 
      'Fehlerhafte Eingabe: .conf Datei wird nicht erstellt/editiert. Neue services können nicht hinzugefügt oder editiert werden.', 
      \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, 
      TRUE 
      ); 
      /** @var $flashMessageService FlashMessageService */ 
      $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class); 
      $flashMessageService->getMessageQueueByIdentifier()->enqueue($message); 
      } 
      return $value; 
    } 
} 
所以基本上,并纠正我,如果我错了,EVAL发生的是用户的输入(他点击保存按钮后右)和记录的持久的关系。所以必须有一种方法来防止新数据的持续存在,不仅要随意改变它。
我希望这能让我的问题更清楚,我不知道还有什么可以写的来解释它。
回答:
我向你展示一个TCA验证的例子。如果验证失败,记录无法保存。
文件myextension /班/验证/识别/ MinReferencesValidator.php
<?php namespace Vendor\Myextension\Validation\Validator; 
/** 
* Validator for min references 
*/ 
class MinReferencesValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator { 
    /** 
    * @var array 
    */ 
    protected $supportedOptions = [ 
      'min' => [0, 'The minimum references to accept', 'integer'], 
    ]; 
    /** 
    * The given value is valid if it contains more then min items 
    * 
    * @param mixed $value The value that should be validated 
    * @return void 
    */ 
    public function isValid($value) { 
     if (!is_object($value)) { 
      $this->addError(
        $this->translateErrorMessage(
          'LLL:EXT:myextension/Resources/Private/Language/locallang.xlf:validator_object_notvalid', 
          'myextension' 
        ), 1489870657); 
      return; 
     } 
     $minimum = $this->options['min']; 
     $countItems = count($value); 
     if ($countItems < $minimum) { 
      $this->addError(
        $this->translateErrorMessage(
          'LLL:EXT: myextension/Resources/Private/Language/locallang.xlf:validator_min_references', 
          'myextension', 
          [ 
           $minimum 
          ] 
      ), 1489872300, [$minimum]); 
      return; 
     } 
    } 
} 
文件myextension /班/域/型号/ Youritem.php
/** * Image of supplier (image reference) 
* 
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> 
* @validate NotEmpty, \Vendor\Yourextension\Validation\Validator\MinReferencesValidator(min=1) 
*/ 
protected $images; 
以上是 TYPO3扩展:如果用户输入错误值,防止记录被保存 的全部内容, 来源链接: utcz.com/qa/263918.html



