2016-09-21 50 views
1

我有一个后端配置选项的扩展。我需要在AddAction和UpdateAction中验证电话号码。我可以在后端配置电话号码格式(比如我们的电话号码/印度电话号码等) 。如何在验证器中获取设置? 我有一个自定义的验证器来验证手机numbers.Here是我的代码得到验证器中的设置 - typo3

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

    } 
} 

$设置返回null

+0

您的验证在哪里?你说你需要验证的价值,但是你的代码没有显示任何验证的尝试。 – pduersteler

+0

@pduersteler我更新了我的问题 –

回答

2

在情况下,你的扩展的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; 
    } 

} 

我建议你在一般的实现这样的功能。所以你可以在你的扩展中检索任何扩展的配置作为一个服务或类似的东西。

祝你好运!

+0

感谢您提供解决方案 –