【问题标题】:get settings in validator - typo3在验证器中获取设置 - 错字3
【发布时间】:2017-01-29 16:22:16
【问题描述】:

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

【问题讨论】:

  • 您的验证在哪里?你说你需要验证的值,但你的代码没有显示任何验证尝试。
  • @pduersteler 我更新了我的问题

标签: typo3 flux extbase


【解决方案1】:

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

}

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

祝你好运!

【讨论】:

  • 感谢您提供解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-24
  • 2020-06-04
  • 1970-01-01
相关资源
最近更新 更多