获取验证器中的设置-typo3

Asw*_*y S 1 typo3 flux extbase

我有一个带有后端配置选项的扩展。我需要在 AddAction 和 UpdateAction 中验证电话号码。我可以在后端配置电话号码格式(例如美国电话号码/印度电话号码等)。我如何在验证器中获取设置?我有一个自定义验证器来验证电话号码。这是我的代码

    <?php
    namespace vendor\Validation\Validator;

    class UsphonenumberValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
    {   


         protected $supportedOptions = array(
               'pattern' => '/^([\(]{1}[0-9]{3}[\)]{1}[ ]{1}[0-9]{3}[\-]{1}[0-9]{4})$/'
          );


          public function isValid($property) { 
                $settings = $this->settings['phone'];
                $pattern = $this->supportedOptions['pattern'];
                $match = preg_match($pattern, $property);

                if ($match >= 1) {
                    return TRUE;
                } else {
                $this->addError('Phone number you are entered is not valid.', 1451318887);
                    return FALSE;
                }

    }
} 
Run Code Online (Sandbox Code Playgroud)

$settings 返回 null

Are*_*ijk 6

如果extbase configuration默认情况下未实现您的扩展的 ,您应该使用\TYPO3\CMS\Extbase\Configuration\ConfigurationManager.

以下是如何获取扩展程序设置的示例:

<?php
namespace MyVendor\MyExtName\Something;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Object\ObjectManager;

class Something {

    /**
     * @var string
     */
    static protected $extensionName = 'MyExtName';

    /**
     * @var null|array
     */
    protected $settings = NULL;

    /**
     * Gets the Settings
     *
     * @return array
     */
    public function getSettings() {
        if (is_null($this->settings)) {
            $this->settings = [];
            /* @var $objectManager \TYPO3\CMS\Extbase\Object\ObjectManager */
            $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
            /* @var $configurationManager \TYPO3\CMS\Extbase\Configuration\ConfigurationManager */
            $configurationManager = $objectManager->get(ConfigurationManager::class);
            $this->settings = $configurationManager->getConfiguration(
                ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
                self::$extensionName
            );
        }
        return $this->settings;
    }

}
Run Code Online (Sandbox Code Playgroud)

我建议您一般实现此类功能。因此,您可以检索任何扩展的任何配置作为扩展内的服务或类似的内容。

祝你好运!