在验证器中获取设置 - typo3
get settings in validator - typo3
我有一个后端配置的扩展 options.I 需要在 AddAction 中验证 phone 数字并且 UpdateAction.I 可以在后端配置 phone 数字格式(比如我们phone number/indian phone 数字等)。如何在验证器中获取设置?
我有一个自定义验证器来验证 phone numbers.Here 是我的代码
<?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
如果您的扩展的 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;
}
}
我建议您总体上实现此类功能。因此,您可以检索任何扩展的任何配置作为扩展内的服务或类似的东西。
祝你好运!
我有一个后端配置的扩展 options.I 需要在 AddAction 中验证 phone 数字并且 UpdateAction.I 可以在后端配置 phone 数字格式(比如我们phone number/indian phone 数字等)。如何在验证器中获取设置? 我有一个自定义验证器来验证 phone numbers.Here 是我的代码
<?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
如果您的扩展的 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;
}
}
我建议您总体上实现此类功能。因此,您可以检索任何扩展的任何配置作为扩展内的服务或类似的东西。
祝你好运!