集合字段类型未创建表单元素



我正在尝试创建一个表单,每次单击"添加新框"链接时都会添加一个新文本框。我通读了以下示例。http://symfony.com/doc/current/reference/forms/types/collection.html

基本上,我遵循了书中的例子。但是当页面呈现并且我单击链接时,没有任何反应。有什么想法吗?谢谢。

这是我的控制器。

 public function createAction() {
    $formBuilder = $this->createFormBuilder();
    $formBuilder->add('emails', 'collection', array(
        // each item in the array will be an "email" field
        'type'   => 'email',
        'prototype' => true,
        'allow_add' => true,
        // these options are passed to each "email" type
        'options'  => array(
            'required'  => false,
            'attr'      => array('class' => 'email-box')
        ),
    ));
    $form = $formBuilder->getForm();
    return $this->render('AcmeRecordBundle:Form:create.html.twig', array(
        'form' => $form->createView(),
    ));
}

这是观点。

 <form action="..." method="POST" {{ form_enctype(form) }}>
{# store the prototype on the data-prototype attribute #}
<ul id="email-fields-list" data-prototype="{{ form_widget(form.emails.get('prototype')) | e }}">
{% for emailField in form.emails %}
    <li>
        {{ form_errors(emailField) }}
        {{ form_widget(emailField) }}
    </li>
{% endfor %}
</ul>
<a href="#" id="add-another-email">Add another email</a>
</form>
<script type="text/javascript">
// keep track of how many email fields have been rendered
var emailCount = '{{ form.emails | length }}';
jQuery(document).ready(function() {
    jQuery('#add-another-email').click(function() {
        var emailList = jQuery('#email-fields-list');
        // grab the prototype template
        var newWidget = emailList.attr('data-prototype');
        // replace the "$$name$$" used in the id and name of the prototype
        // with a number that's unique to our emails
        // end name attribute looks like name="contact[emails][2]"
        newWidget = newWidget.replace(/$$name$$/g, emailCount);
        emailCount++;
        // create a new list element and add it to our list
        var newLi = jQuery('<li></li>').html(newWidget);
        newLi.appendTo(jQuery('#email-fields-list'));
        return false;
    });
})
</script>

这个问题可以通过参考以下链接来解决。

https://github.com/beberlei/AcmePizzaBundle

在这里,您将找到正在实现的相同功能。

我也经历过这个。

对这个问题和我发现的另一个问题的回答和示例也没有回答我的问题。

以下是我以某种通用方式做到的。

在通用中,我的意思是,我添加到表单中的任何collection只需要遵循表单模板循环(例如在宏中),仅此而已!

使用哪种约定

  • HTML 来自 Twitter Bootstrap 2.0.x
  • Javascript 代码已经在 $(document).ready();
  • 遵循 Symfony 2.0.x 教程
  • 使用 MopaBootstrapBundle

窗体类型类

class OrderForm extends AbstractType
{
// ...
public function buildForm(FormBuilder $builder, array $options)
{
// ...
  $builder
  ->add('sharingusers', 'collection', array(
          'type' => new UserForm(),
          'allow_add' => true,
          'allow_delete' => true,
          'by_reference' => false,
          'required'=> false
  ));
// ...
}
}

JavaScript

/* In the functions section out of document ready */
/**
 * Add a new row in a form Collection
 *
 * Difference from source is that I use Bootstrap convention
 * to get the part we are interrested in, the input tag itself and not
 * create a new .collection-field block inside the original.
 * 
 * Source: http://symfony.com/doc/current/cookbook/form/form_collections.html
 */
function addTagForm(collectionHolder, newBtn) {
    var prototype = collectionHolder.attr('data-prototype');
    var p = prototype.replace(/$$name$$/g, collectionHolder.children().length);
    var newFormFromPrototype = $(p);
    var buildup = newFormFromPrototype.find(".controls input");
    var collectionField = $('<div class="collection-field"></div>').append(buildup);
    newBtn.before(collectionField);
}

/* ********** */
$(document).ready(function(){

   /* other initializations */

    /**
     * Form collection behavior
     *
     * Inspired, but refactored to be re-usable from Source defined below
     *
     * Source: http://symfony.com/doc/current/cookbook/form/form_collections.html
     */
    var formCollectionObj = $('form .behavior-collection');
    if(formCollectionObj.length >= 1){
        console.log('run.js: document ready "form .behavior-collection" applied on '+formCollectionObj.length+' elements');
        var addTagLink = $('<a href="#" class="btn"><i class="icon-plus-sign"></i> Add</a>');
        var newBtn = $('<div class="collection-add"></div>').append(addTagLink);
        formCollectionObj.append(newBtn);
        addTagLink.on('click', function(e) {
            e.preventDefault();
            addTagForm(formCollectionObj, newBtn);
        });
    }

   /* other initializations */
});

表单模板

这里的技巧是我会使用原始{{ form_widget(form }}但我需要在视图表单中添加一些特定的内容,但我无法使其更短。

我尝试只编辑目标字段,发现它有点复杂

这是我是如何做到的:

            {# All form elements prior to the targeted field #}
            <div class="control-collection control-group">
                <label class="control-label">{{ form_label(form.sharingusers) }}</label>
                <div class="controls behavior-collection" data-prototype="{{ form_widget(form.sharingusers.get('prototype'))|escape }}">
                {% for user in form.sharingusers %}
                    {{ form_row(user) }}
                {% endfor %}
                </div>
            </div>
            {{ form_rest(form) }}

相关内容

  • 没有找到相关文章

最新更新