English 中文(简体)
报表2
原标题:Symfony 2 Form, field collection doesn t display

I would like to create a form which can add multiple etape. I create the form like this:

页: 1

 namespace RBOTryBundleFormType;

 use SymfonyComponentFormAbstractType;
 use SymfonyComponentFormFormBuilder;

 use RBOTryBundleEntityTry;

 class TryType extends AbstractType {
   public function buildForm(FormBuilder $builder, array $options)
   {
     $builder->add( etapes ,  collection , array(
         type  => new EtapeType(), 
         allow_add  => true,
         allow_delete  => true,
         prototype  => true,
         label  =>  Etapes 
    ));
  iii
  public function getName()
{
    return  try ;
iii

public function getDefaultOptions(array $options)
{
    return array(
         data_class  =>  RBOTryBundleEntityTry ,
         csrf_protection  => true,
         csrf_field_name  =>  _token ,
        // a unique key to help generate the secret token
         intention        =>  try_item ,
    );
iii
 iii

// EtapeType

<?php

namespace RBOTryBundleFormType;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilder;

use RBOTryBundleEntityEtape;

class EtapeType extends AbstractType {

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add( name ,  text );
iii

public function getDefaultOptions(array $options)
{
    return array(
         data_class  =>  RBOTryBundleEntityEtape ,
    );
iii

public function getName()
{
    return  etape ;
iii

iii

// Display in twig template

{{ form_row(form.etapes) iiiiii

实体 提炼有一种财产排泄物,是一种阿雷拉角(由建筑商界定)

这部法典根本不希望贴上标签。 我是否误解了什么?

事先感谢你

问题回答

I have always used form_widget to render embedded forms, but I think form_row will work fine too. The important thing you should look at is if the embedded form container has a "data-prototype" attribute. Then, you must add a script to the page where you are displaying the form. In my case, I use Mootools, but I gues you can easily translate this script to jQuery or any other javascript framework:

这些是我的js案的内容(如果是“数据-格式”的属性,你应替换“XXX”)。

window.addEvent(  domready , function() {
var add = function() {
    var collectionHolder = $( XXX );
    var prototype = collectionHolder.getAttribute( data-prototype );
    form = prototype.replace(/$$name$$/g, collectionHolder.getChildren().length);
    var cont = Elements.from(form);
    collectionHolder.adopt( cont );
}
var remove = function() {
    var collectionHolder = $( XXX );
    var child = collectionHolder.getLast();
    child.dispose();
}
$$( a.add-link ).addEvent( click , function(e){
    e.stop();
    add();
iii
$$( a.remove-link ).addEvent( click , function(e){
    e.stop();
    remove();
iii

iii

这一文字非常简单,只是增加了新的内容或删除了收集工作的最后内容。 此外,为了增加/消除联系,请在您的意见中增加几点。

<ul class="actions">
<li>
    <a href="#" class="add-link">
        Add
    </a>
</li>
<li>
    <a href="#" class="remove-link">
        Remove last
    </a>
    </li>
</ul>

I hope it helps, I m new to Symfony and I m still learning, but it worked for me :)

我这样做的方式是:

{% for etape in form.etapes %}
  {{ form_widget(etape.name) }}
{% endfor %}

我可能错了,因为我不使用过前程。 但是,这一想法对我来说是分管的。

In PHP Templates..

    `<?php foreach ($form[ etapes ] as $etapesField) : ?>
<?php echo $view[ form ]->errors($etapesField) ?>
<?php echo $view[ form ]->widget($etapesField) ?>
<?php endforeach; ?>`

Ok, 2篇:

1 - The regular way to display your form in a twig template:

    {{ form_row(form) }}

The templating system is able to cascade (sorry for the approximate english), meaning that all sub-fields in a form will be rendered as well as its encapsulating form. Thus, no need to render the children directly by calling form.etapes.

2- 我认为,你的问题确实在于深思熟虑。 如果你采取这样的方式:

    $form = $this->createForm(new TryType()));

甚至这样:

    $model = new Try();
    $model->setEtapes(array());

    $form = $this->createForm(new TryType(), $model));

那么,你的形式是无所作为的,因为其显示的Etape油田的数量直接取决于你向你的模型提供的主要情况。 如果你提供一个空洞的阵列,就不会有主宰场。 在显示空洞单一领域时,你应做的是:

    $model = new Try();
    $model->setEtapes(array(new Etape())); // Empty Etape

    $form = $this->createForm(new TryType(), $model));

因此,你可以像你所收集的一样,增加以下领域:

    $model = new Try();
    $model->setEtapes(array(
        new Etape( Étape 1 ),
        new Etape( Étape 2 ),
        ...
    ));

    $form = $this->createForm(new TryType(), $model));

由于你正在使用一个子级,所以这样做;

$builder
    ->add(
         etapes ,
         collection ,
        array(
             type           => new EtapeType(),
             allow_add      => true,
             allow_delete   => true,
        )
    )
    ->getForm()
;

And how you display in twig doesn t really matter. It depends on what you want. form_row, form_widget all should work properly.





相关问题
Is HashMap in Java collision safe

I am developing a parser that needs to put key value pairs in hashmap. A key can have multiple values which I can do in this way HashMap<String,ArrayList<String>> . What happens if the ...

iterating over map and array simultaneously in a for loop

I am having some trouble creating a for loop within a constructor to iterate over a map and an array at the same time. Here, it is indicated that this cannot be done with an enhanced for loop. I have ...

PLSQL Collections - how to use table of records?

I m new to PL/SQL and I m trying to use a table of records, but I don t know how to use this feature. What is the problem? DECLARE TYPE TIP IS RECORD ( F1 ...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

Concurrent modification whilst traversing a ruby Hash

Suppose you had this: def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName = i[1].first hash.delete(key) if productName==nameToDelete end end I m not sure it ...

热门标签