【发布时间】:2014-02-24 12:42:32
【问题描述】:
这是一个我已经思考了一段时间的问题。 请注意,我(还)不是 Symfony2 专家,所以我可能在某个地方犯了一个新手错误。
Field1:标准 Symfony2 text 字段类型
Field2:自定义字段类型 compoundfield 和 text 字段 + checkbox 字段)
我的目标:将约束添加到 autoValue 字段以处理 autoValue's text input child
约束不起作用的原因可能是因为NotBlank 需要一个字符串值,而这个表单字段的内部数据是一个数组array('input'=>'value', 'checkbox' => true)。这个数组值被转换回带有自定义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()
)
));
.
TextWithAutoValue 源文件
src/My/Component/Form/Type/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/My/Component/Form/DataTransformer/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/My/ComponentBundle/Resources/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 %}
.
问题
我已经阅读 docs 和 google 好几个小时了,不知道如何复制、绑定或引用在构建此表单时添加的原始约束。
-> 有谁知道如何做到这一点?
-> 用于奖励积分;如何启用已添加到主窗体绑定实体的约束? (通过实体类上的注释)
附言
抱歉,问题变得这么长,我希望我成功地把我的问题说清楚了。如果没有,请向我询问更多详细信息!
【问题讨论】:
-
+1。我有一个same question,但对此没有答案。
-
@byf-ferdy 谢谢,这听起来像是一个类似的问题,但我不确定它是否是 same。我相信我的案例可能有更多解决方法的选择,因为两者之间有一个自定义表单类型。
标签: php symfony constraints symfony-forms symfony-2.4