我正试图按照以下文档覆盖Twig中的一个表单小部件:http://symfony.com/doc/current/cookbook/form/form_customization.html#method-1-内部模板即形式
但我有点迷路了。这是我想要分解的选择字段:
{{ form_widget(edit_form.activities) }}
以下是文档中的覆盖过程:
{% form_theme edit_form _self %}
{%- block choice_widget_options -%}
{% for group_label, choice in options %}
{%- if choice is iterable -%}
<optgroup label="{{ choice_translation_domain is same as(false) ? group_label : group_label|trans({}, choice_translation_domain) }}">
{% set options = choice %}
{{- block('choice_widget_options') -}}
</optgroup>
{%- else -%}
{% set attr = choice.attr %}
<option value="{{ choice.value }}" {{ block('attributes') }}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}</option>
{%- endif -%}
{% endfor %}
{%- endblock choice_widget_options -%}
目的是能够在选择框中选择多个字段,并通过我的控制器发布一组id。
你们知道怎么做吗?
用Twig做这件事没用。所以我在FormType方面做了。
解决方案:使用PRE_SET_DATA。我使用"数据"选项覆盖表单。
这是给你的代码:
class TagType extends AbstractType
{
/** @var $em EntityManager */
private $em;
/**
* TagType constructor.
*
* @param EntityManager $entityManager
*/
public function __construct(EntityManager $entityManager)
{
$this->em = $entityManager;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', [
'label' => 'Tags',
])
->add('activities', 'entity', [
'label' => 'Affecter à des activitées',
'mapped' => false,
'class' => 'App'TagBundle'Entity'Activity'Activity',
'required' => false,
'multiple' => true,
'attr' => [
'data-js' => 'chosen',
'data-placeholder' => 'Affecter à des activitées',
],
]);
$builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'));
}
/**
* @param FormEvent $event
*/
public function onPreSetData(FormEvent $event)
{
$tag = $event->getData();
$form = $event->getForm();
/** Doctrine'ORM'PersistentCollection $taggingCollection */
$taggingCollection = $tag->getTagging();
if ($taggingCollection == null)
return; // leave if no tag (probably a new action)
$activitiesIds = array();
/** @var 'App'TagBundle'Entity'Activity'Tagging $tagging */
foreach( $taggingCollection as $tagging){
$activitiesIds[] = $tagging->getResourceId();
}
$em = $this->em;
$activities = new ArrayCollection();
foreach($activitiesIds as $activityId) {
$activity = $em->getRepository('AppTagBundle:Activity'Activity')->find($activityId);
$activities->add($activity);
}
$form->add('activities', 'entity', [
'label' => 'Editer les activitées affectées',
'mapped' => false,
'class' => 'App'TagBundle'Entity'Activity'Activity',
'required' => false,
'multiple' => true,
'attr' => [
'data-js' => 'chosen',
'data-placeholder' => 'Affecter à des activitées',
],
'data' => $activities
]);
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'data_class' => 'App'TagBundle'Entity'Activity'Tag',
]);
}
/**
* @return string
*/
public function getName()
{
return 'App_TagBundle_activity_tag';
}
}
Firstly check if path to your template is correct: app/Resources/views/Form/fields.html.twig
Next, in the template where you are using the form, you have to specify your customized template to be used
{% form_theme form 'AppBundle:Form:fields.html.twig' %}
{{ form_widget(edit_form.activities) }}