Typo3扩展:如果他在字段中输入了不需要的输入,则阻止BE用户保存记录

时间:2017-11-28 09:26:47

标签: typo3

基本上我试图阻止BE用户输入错误的内容。因此我使用外部评估类(https://docs.typo3.org/typo3cms/TCAReference/8-dev/ColumnsConfig/Type/Input.html)。

现在我的问题是:我实现的功能改变输入并设置属性的更改

public function evaluateFieldValue($value, $is_in, &$set)
{
  if(...){
  $value = 'Wrong input';
  }
  return $value;
}

但这不是我的目标。我希望BE用户在保存记录时获得错误消息窗口(例如'错误输入')。我该怎么做?

2 个答案:

答案 0 :(得分:1)

我将提供一个输入路径,而不是一个完整的解决方案。

ext_localconf.php

$signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher');
$signalSlotDispatcher->connect(
    'TYPO3\CMS\Backend\Controller\EditDocumentController', 'initAfter',
    'YourVendor\YourExtension\Hooks\Backend\EditDocSlot', 'initAfter');

YourVendor\YourExtension\Hooks\Backend\EditDocSlot.php

namespace YourVendor\YourExtension\Hooks\Backend;

use TYPO3\CMS\Backend\Controller\EditDocumentController;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

class EditDocSlot
{
    /**
     *
     * @param EditDocumentController $ref
     */
    public function initAfter(EditDocumentController $ref)
    {
        /** @var PageRenderer $pageRenderer */
        $pageRenderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Page\PageRenderer::class);
        $pageRenderer->addJsFile(ExtensionManagementUtility::extRelPath('your_extension') . 'Resources/Public/JavaScript/FormEngineValidation.js');
    }
}

your_extension/Resources/Public/JavaScript/FormEngineValidation.js

require(['jquery', "TYPO3/CMS/Backend/FormEngineValidation"], function ($, FormEngineValidation) {
    // extend FormEngineValidation
    // check FormEngineValidation.processValue and
    // check FormEngineValidation.validateField
    // and create a new eval ???!!! :)
});

一些javascript补丁信息 http://me.dt.in.th/page/JavaScript-override/

答案 1 :(得分:0)

如果您不希望保存新值,可以将函数 evaluateFieldValue 中的 $ set 的值设置为false。

对于错误消息,您可以使用TYPO3 Flash messages。这将在后端输出一条消息(红色表示ERROR,黄色表示警告,绿色表示OK)。这是在TYPO3后端显示消息的标准方式。

此示例不接受字符串“aaa”,它将在保存时被丢弃,并将显示错误消息。

/**
 * Server-side validation/evaluation on saving the record
 *
 * @param string $value The field value to be evaluated
 * @param string $is_in The "is_in" value of the field configuration from TCA
 * @param bool $set Boolean defining if the value is written to the database or not.
 * @return string Evaluated field value
 */
public function evaluateFieldValue($value, $is_in, &$set)
{    
    if ("aaa" === $value) {
        // input is to be discarded!
        $this->flashMessage("evaluateFieldValue", 
            "wrong input", 
            \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
        $set = False;
    }
    return $value;
}

function flashMessage($messagetitle, $messagetext, $severity=\TYPO3\CMS\Core\Messaging\FlashMessage::ERROR)
{
    $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
        \TYPO3\CMS\Core\Messaging\FlashMessage::class,
        $messagetext,
        $messagetitle, 
        $severity, 
        true 
   );

   $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
       \TYPO3\CMS\Extbase\Object\ObjectManager::class);
   $flashMessageService = $objectManager->get(
           \TYPO3\CMS\Core\Messaging\FlashMessageService::class);
   $messageQueue = $flashMessageService->getMessageQueueByIdentifier();
   $messageQueue->addMessage($message);
}