Search code examples
typo3fluxextbase

get settings in validator - typo3


I have an extension with backend configuration options.I need to validate a phone number in AddAction and UpdateAction.I can configure the phone number format in backend (say us phone number/indian phone number etc).How can i get the settings in validator? I have a custom validator to validate phone numbers.Here is my code

    <?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 returns null


Solution

  • In cases where the extbase configuration of your extension isn't implemented by default you should retrieve it yourself by using the \TYPO3\CMS\Extbase\Configuration\ConfigurationManager.

    Here is an example how you can obtain the settings of your extension:

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

    I recommend that you implement such functionality in general. So you could retrieve any configuration of any extension as a Service inside your extension or something similar to this.

    Good luck!