簡體   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