Typo3 extension: Prevent the BE user to save a record if he made an undesired input in a field

爷,独闯天下 提交于 2019-12-11 14:48:00

问题


Basically I try to prevent the BE user to input something wrong. Thatfore i use an external evaluation class (https://docs.typo3.org/typo3cms/TCAReference/8-dev/ColumnsConfig/Type/Input.html).

Now my problem is: The function i implemented changes the input and sets the change for the property

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

but that's not what i'm aiming for. I want the BE user to get an error message window (like 'wrong input') on saving the record. How do i do that?


回答1:


I will give an entry path, not a full solution.

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 ???!!! :)
});

Some javascript patching info http://me.dt.in.th/page/JavaScript-override/




回答2:


You can set the value of $set in the function evaluateFieldValue to false if you don't want the new value to be saved.

For an error message you can use TYPO3 Flash messages. This will output a message in the backend (red for ERROR, yellow for warning and green for OK). This is the standard way of showing messages in the TYPO3 backend.

This example does not accept the String "aaa", it will be discarded on saving and an error message will be displayed.

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


来源:https://stackoverflow.com/questions/47528011/typo3-extension-prevent-the-be-user-to-save-a-record-if-he-made-an-undesired-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!