Symfony 3.0+:表单CollectionType字段不生成任何html输入

时间:2017-11-19 00:08:09

标签: forms symfony

我必须在我的项目中使用CollectionType字段,我怀疑有一个带有name =“langues []”的html输入,它将返回一个数组但是在从Symfony官方文档复制代码后,该字段未显示结果。只显示标签。

我写的代码:

public void readAllInvoice()
{
try{
File fXmlFile = new File("your file path here");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

    doc.getDocumentElement().normalize();


    int count=0;
NodeList nList = doc.getElementsByTagName("yourXML tree tag name here");

   for (int temp = 0; temp < nList.getLength(); temp++) {

    Node nNode = nList.item(temp);



    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

        Element eElement = (Element) nNode;
                   jTextField4.setText(eElement.getElementsByTagName("tag id you want to get data here").item(0).getTextContent());
                   jTextField5.setText(eElement.getElementsByTagName("tag id you want to get data here").item(0).getTextContent());

                     count=count+1;   
    }

}

 setjTextField();

}
catch(Exception e)
{

}


}

生成的html是我从浏览器元素检查器中获取的:

    $builder->add('langues', CollectionType::class, array(
        'entry_type' => TextType::class,
        'allow_add' => true,
        'prototype' => true,
        'prototype_data' => 'New Tag Placeholder',
    ))
    ;

那么,如何正确地实现CollectionType?

2 个答案:

答案 0 :(得分:0)

所以这个问题的答案是我们必须初始化属性 $ langues ,该属性被定义为实体中的一个数组,至少有一个集合字段。

// entity class :
private $langues = array('');

答案 1 :(得分:0)

为什么你没有关注documentation's example

class Task
{
    protected $description;

    protected $tags;

    public function __construct()
    {
        $this->tags = new ArrayCollection();
    }
}

控制器:

class TaskController extends Controller
{
    public function newAction(Request $request)
    {
        $task = new Task();

        // dummy code - this is here just so that the Task has some tags
        // otherwise, this isn't an interesting example
        $tag1 = new Tag();
        $tag1->setName('tag1');
        $task->getTags()->add($tag1);
        $tag2 = new Tag();
        $tag2->setName('tag2');
        $task->getTags()->add($tag2);
        // end dummy code

        //etc.
    }
}

所以在你的情况下

class Personnel
{    
    protected $langues;

    public function __construct()
    {
        $this->langues = new ArrayCollection();
    }
}