Mondblut
Mondblut

Reputation: 329

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

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?

Upvotes: 0

Views: 1027

Answers (2)

Sybille Peters
Sybille Peters

Reputation: 3229

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

Upvotes: 0

Andrei L
Andrei L

Reputation: 3681

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/

Upvotes: 2

Related Questions