这是一个问题,我已经有一段时间了。请知道我还不是Symfony2专家,所以我可能在某个地方犯了一个菜鸟错误。
text
栏位1 :标准Symfony2栏位类型
Field2:compound
带有text
字段+checkbox
字段的自定义字段类型字段
我的目标:将约束添加到autoValue
字段中以处理autoValue's text input child
约束不起作用的原因可能是因为NotBlank
期望一个字符串值,并且此form字段的内部数据是一个数组array('input'=>'value', 'checkbox' => true)
。该数组值使用custom转换回字符串DataTransformer
。但是,我怀疑在针对已知约束验证该字段之后会发生这种情况。
就像您在下面的注释代码中看到的那样,我已经能够在文本输入上获得约束,但是仅当将其硬编码为autoValue的表单类型时,我才想针对主字段的约束进行验证。
我的(简化的)控制器和字段示例代码:
。
设置用于测试目的的快速表格。
<?php
//...
// $entityInstance holds an entity that has it's own constraints
// that have been added via annotations
$formBuilder = $this->createFormBuilder( $entityInstance, array(
'attr' => array(
// added to disable html5 validation
'novalidate' => 'novalidate'
)
));
$formBuilder->add('regular_text', 'text', array(
'constraints' => array(
new \Symfony\Component\Validator\Constraints\NotBlank()
)
));
$formBuilder->add('auto_text', 'textWithAutoValue', array(
'constraints' => array(
new \Symfony\Component\Validator\Constraints\NotBlank()
)
));
。
src /我的/组件/表单/类型/TextWithAutoValueType.php
<?php
namespace My\Component\Form\Type;
use My\Component\Form\DataTransformer\TextWithAutoValueTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class TextWithAutoValueType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('value', 'text', array(
// when I uncomment this, the NotBlank constraint works. I just
// want to validate against whatever constraints are added to the
// main form field 'auto_text' instead of hardcoding them here
// 'constraints' => array(
// new \Symfony\Component\Validator\Constraints\NotBlank()
// )
));
$builder->add('checkbox', 'checkbox', array(
));
$builder->addModelTransformer(
new TextWithAutoValueTransformer()
);
}
public function getName()
{
return 'textWithAutoValue';
}
}
src /我/组件/表格/数据转换工具/TextWithAutoValueType.php
<?php
namespace My\Component\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
class TextWithAutoValueTransformer
implements DataTransformerInterface
{
/**
* @inheritdoc
*/
public function transform($value)
{
return array(
'value' => (string) $value,
'checkbox' => true
);
}
/**
* @inheritdoc
*/
public function reverseTransform($value)
{
return $value['value'];
}
}
src /我的/ ComponentBundle /资源/config/services.yml
parameters:
services:
my_component.form.type.textWithAutoValue:
class: My\Component\Form\Type\TextWithAutoValueType
tags:
- { name: form.type, alias: textWithAutoValue }
src / My / ComponentBundle / Resources / views / Form / fields.html.twig
{% block textWithAutoValue_widget %}
{% spaceless %}
{{ form_widget(form.value) }}
{{ form_widget(form.checkbox) }}
<label for="{{ form.checkbox.vars.id}}">use default value</label>
{% endspaceless %}
{% endblock %}
。
我已经阅读文档和Google已有好几个小时了,无法弄清楚如何复制,绑定或引用在构建此表单时添加的原始约束。
->有人知道如何做到这一点吗?
->奖励积分;如何启用已添加到主窗体的绑定实体的约束?(通过对实体类的注释)
聚苯乙烯
抱歉,这成为一个漫长的问题,我希望我已成功阐明了我的问题。如果没有,请询问我更多细节!
我们可以从中得出的结论是,验证主要发生在类而不是表单类型上。那就是你所忽略的。您需要做的是:
validation.yml:
src/My/Bundle/Form/Model/TextWithAutoValue:
properties:
text:
- Type:
type: string
- NotBlank: ~
checkbox:
- Type:
type: boolean
编辑:
我假设您已经知道如何在另一个表单中使用表单类型。在定义验证配置时,可以使用一个非常有用的东西,称为验证组。这是一个基本示例(在validation.yml文件中,因为我对验证批注不太熟练):
src/My/Bundle/Form/Model/TextWithAutoValue:
properties:
text:
- Type:
type: string
groups: [ Default, Create, Edit ]
- NotBlank:
groups: [ Edit ]
checkbox:
- Type:
type: boolean
有一个groups参数可以添加到每个约束中。它是一个包含验证组名称的数组。当请求对对象进行验证时,可以指定要验证的组。然后,系统将在验证文件中查找应应用哪些约束。
默认情况下,对所有约束都设置“默认”组。这也是执行常规验证时使用的组。
当然,这是所有表单类型选项的标准行为。一个例子:
$formBuilder->add('auto_text', 'textWithAutoValue', array(
'label' => 'my_label',
'validation_groups' => array('Default', 'Edit'),
));
至于使用不同的实体,您可以按照与堆积表格相同的体系结构堆积数据类。在上面的示例中,使用textWithAutoValueType的表单类型必须具有一个data_class,该类具有'auto_text'属性和相应的getter / setter。
在验证文件中,有效约束将能够级联验证。具有Valid属性的属性将检测该属性的类,并尝试为该类查找相应的验证配置,并将其应用于相同的验证组:
src/My/Bundle/Form/Model/ContainerDataClass:
properties:
auto_text:
Valid: ~ # Will call the validation conf just below with the same groups
src/My/Bundle/Form/Model/TextWithAutoValue:
properties:
... etc
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句