我需要创建字段,作为选项获得正则字符串。
所以,我创建了PatternType字段:
public function getDefaultOptions(array $options)
{
$defaultOptions = array(
'data' => null,
'data_class' => null,
'trim' => true,
'required' => true,
'read_only' => false,
'max_length' => null,
'pattern' => null,
'property_path' => null,
'by_reference' => true,
'error_bubbling' => false,
'regexp' => false,
'error_mapping' => array(),
'label' => null,
'attr' => array(),
'invalid_message' => 'This value is not valid',
'invalid_message_parameters' => array()
);
$class = isset($options['data_class']) ? $options['data_class'] : null;
// If no data class is set explicitly and an object is passed as data,
// use the class of that object as data class
if (!$class && isset($options['data']) && is_object($options['data'])) {
$defaultOptions['data_class'] = $class = get_class($options['data']);
}
if ($class) {
$defaultOptions['empty_data'] = function () use ($class) {
return new $class();
};
} else {
$defaultOptions['empty_data'] = '';
}
$patt = $options['regexp'];
unset($options['regexp']);
$defaultOptions['validation_constraint'] = new Regex(
array(
'pattern' => $patt,
'match' => true,
'message' => 'Niewłaściwy format'
)
);
var_dump($defaultOptions);
return $defaultOptions;
}
var_dump返回格式良好的设置数组,其中包含regex对象-但是当表单生成验证不起作用时-传递任何值。知道为什么吗?
你为什么要这样做?已经有一个正则表达式验证器。只需在验证器中使用一个普通的文本字段。
如果你需要一个没有模型类绑定的表单,请阅读相应的章节
好了,我发现了问题所在—您只能将验证器常量添加到根表单对象(其他symfony完全忽略)。因此,我需要的是简单地获得根形式,添加那里validator_constant与validator_group选项集。然后为validator_group指定合适的字段