Sonata Admin表单包含动态字段

时间:2014-07-22 01:15:56

标签: symfony sonata-admin symfony-sonata

我的EntryAdmin表单需要三个或四个额外字段用于用户定义的不同广告系列,并通过实体CampaignProperties处理,然后在选中相应的单选按钮时通过动态附加动态附加。

我有一个EntryProperty表,然后应该存储entry_id,property_id和value。

我的目的是在Entry实体的postPersist事件中保存额外的字段。

我已经扩展了edit.html.twig,它可以在表单上附加字段,但是现在我因为额外字段的持久性而被卡住了错误'此表单不应包含额外的字段。'< /强>

这是代码: EntryAdmin表单部分

$formMapper
        ->add('channel', 'choice', array('attr' => array('class' => 'channels'), 'expanded' => true, 'choices' => $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository('AcmeMyBundle:Entry')->allowedChannels($security_context))) /* checks user-allowed campaigns to load extra campaign fields on check */
        ->add('name')
        ->add('tel', null, array('label' => 'Telephone'))
        ->add('email')
        ->add('deviceType', 'choice', array('expanded' => true, 'choices' => array('Smartphone', 'Feature phone')))
        ->add('createdAt', 'sonata_type_datetime_picker', array('label' => 'Posted on'))
        ->with('Items for sale')
            ->add('listings','sonata_type_collection', array(
            // Prevents the "Delete" option from being displayed
            'type_options' => array('delete' => false)
        ), array(
            'edit' => 'inline',
            'inline' => 'table',
            'sortable' => 'position',
        ))
        ->end()
        ->with('Photos')
            ->add('images','sonata_type_collection', array(
            // Prevents the "Delete" option from being displayed
            'type_options' => array('delete' => false)
        ), array(
            'edit' => 'inline',
            'inline' => 'table',
            'sortable' => 'position',
        ))
        ->end()
        ;

通过ajax加载额外字段twig。

{% block fields %}
{% if(channel.properties) %}
    <div class="box box-success">
        <div class="box-header">
            <h4 class="box-title">
                Additional information
            </h4>
        </div>
        <div class="box-body">
            <div class="sonata-ba-collapsed-fields">
                {% for property in channel.properties %}
                    <div id="sonata-ba-field-container-{{ uniqId }}_{{ property.name }}" class="form-group">
                        <label for="{{ uniqId }}['properties'][{{ property.id }}]" class="control-label required">
                                {{ property.label }}
                        </label>
                        <div class=" sonata-ba-field sonata-ba-field-standard-natural">
                            {% if(property.type == 'Text') and (property.length < 255) or (property.type == 'Number') %}
                                <input type="text" class=" form-control" maxlength="255" required="required" name="{{ uniqId }}['properties'][{{ property.id }}]" id="{{ uniqId }}_{{ property.name }}"> 
                            {% endif %}
                            {% if(property.type == 'Text') and (property.length > 255) %}
                                <textarea class="form-control" required="required" name="{{ uniqId }}['properties'][{{ property.id }}]" id="{{ uniqId }}_{{ property.name }}"></textarea> 
                            {% endif %}
                            {% if(property.type == 'Boolean') %}
                                <ul class="list-unstyled channels" id="{{ uniqId}}_properties_{{ property.name }}">
                                    <li>
                                        <label class="">
                                            <div class="iradio_minimal" style="position: relative;" aria-checked="false" aria-disabled="false">
                                                <input type="radio" value="0" required="required" name="{{ uniqId }}['properties'][{{ property.id }}]" id="{{ uniqId}}_properties_{{ property.name }}_0" style="position: absolute; opacity: 0;">
                                                <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: none repeat scroll 0% 0% rgb(255, 255, 255); border: 0px none; opacity: 0;"></ins>
                                            </div>
                                            <span>No </span>
                                        </label>
                                    </li>
                                    <li>
                                        <label class="">
                                            <div class="iradio_minimal" style="position: relative;" aria-checked="false" aria-disabled="false">
                                                <input type="radio" value="1" required="required" name="{{ uniqId }}['properties'][{{ property.id }}]" id="{{ uniqId}}_properties_{{ property.name }}_1" style="position: absolute; opacity: 0;">
                                                <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: none repeat scroll 0% 0% rgb(255, 255, 255); border: 0px none; opacity: 0;"></ins>
                                            </div>
                                            <span>Yes </span>
                                        </label>
                                    </li>
                                </ul>
                            {% endif %}
                            {% if(property.type == 'Date') %}
                                <div class="form-group">
                                    <div class='input-group date' id='{{ uniqId}}_properties_{{ property.name }}_div' data-date-format="YYYY-MM-DD HH:mm">
                                        <input type="text" class="sonata-medium-date form-control" name="{{ uniqId }}['properties'][{{ property.id }}]" id="{{ uniqId}}_properties_{{ property.name }}" />
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                                        </span>
                                    </div>
                                </div>
                                <script type="text/javascript">
                                    jQuery(function ($) {
                                        $('#{{ uniqId}}_properties_{{ property.name }}_div').datetimepicker();
                                    });
                                </script>
                            {% endif %}
                        </div>
                    </div>
                {% endfor %}
            </div>
        </div>
    </div>
{% endif %}
{% endblock %}

任何有想法如何解决这个问题的人?

1 个答案:

答案 0 :(得分:1)

我认为这涵盖了您的问题

  $builder->add('tags', 'collection', array(
    'type'         => new TagType(),
    'allow_add'    => true, <-- This Bit
));

来源:http://symfony.com/doc/current/cookbook/form/form_collections.html#allowing-new-tags-with-the-prototype

2016年5月5日更新

通过更新后的Symfony 3.1 文档,您可能需要这样的内容:

$builder->add('tags', CollectionType::class, array(
    'entry_type'   => TagType::class,
    'allow_add'    => true,
));