在验证器中获取设置 - typo3

时间:2016-09-21 12:08:31

标签: 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;
                }

    }
} 
  

$ settings返回null

1 个答案:

答案 0 :(得分:3)

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

}

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

祝你好运!