繁体   English   中英

在验证器中获取设置-typo3

[英]get settings in validator - typo3

我有一个带有后端配置选项的扩展程序。我需要在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;
                }

    }
} 

$ settings返回null

如果您的扩展程序的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;
    }

}

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

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM