2016-09-21 31 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

感謝您提供解決方案 –