情况如下: 我有一个父表单,其中有一些字段是集合。
需要将一些选项传递给子表单,以便它知道是否显示某个字段,以及是否需要它。
这是一个片段:
$builder->add('scopesHistory', 'collection',
array(
'type' => new ProjectDetailScopeFormType(
array('required' => false, 'cssClass' => 'hidden')
),
'by_reference' => true,
'required' => true,
'attr' => array('class' => ' hidden')
)
)
子表格如下:
class ProjectDetailScopeFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$req = $options['required']; //not working
$css = $options['cssClass']; //not working
$builder->add(
'scope',
'text',
array(
'label' => 'Scope',
'required' => $req,
'attr' => array('class' => $css),
'label_attr' => array('class' => $css)
)
);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setRequired(array(
'required','cssClass'
));
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\ProjectDetailScope',
));
}
public function getName()
{
return 'ProjectDetailScope';
}
}
它会抛出错误:
The required option "cssClass" is missing.
我也试图消除这部分:
$resolver->setRequired(array('required', 'cssClass'));
但它会抛出:
Notice: Undefined index: cssClass
底层数据结构是一个ProjectDetail实体,它有一个ProjectDetailScope实体的数组集合作为子节点。
除了那个"小"细节。
问题与this one类似,但您没有嵌套表单。我不会给孩子打电话给创建表格。
修改
问题似乎是其中一条评论中提出的here问题,但问题仍然存在。按如下方式更改我的添加:
$builder->add('scopesHistory', 'collection', array(
'type' => new ProjectDetailScopeFormType(), 'options' =>
array('required'=>false, 'cssClass'=>'hidden'),
'by_reference' => true, 'required' => true,
'attr'=>array('class'=>' hidden')))
因为它给了我错误:
The option "cssClass" does not exist. Known options are: "action", "allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference", "cascade_validation", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "inherit_data", "intention", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "max_length", "method", "pattern", "post_max_size_message", "property_path", "read_only", "required", "translation_domain", "trim", "validation_groups", "virtual".
EDIT2
我忘了宣布:
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\ProjectDetailScope',
'required'=>true,
'cssClass'=>''
));
现在它已经解决了;)