簡體   English   中英

Symfony 2形成額外的字段

[英]Symfony 2 form extra fields

我通過AJAX更改了一些字段,當我試圖保存表單時,我發現了一個錯誤,即Extra fields are not allowed

如何在sf1.4中更改驗證器屬性,如validatorPass()
或者它可能改變形式以接受額外的字段?

我使用SonataAdminBundle來創建表單。

在將它們綁定到表單之前,您可以從請求數據中刪除多余的字段:

    // The JSON PUT data will include all attributes in the entity, even
    // those that are not updateable by the user and are not in the form.
    // We need to remove these extra fields or we will get a
    // "This form should not contain extra fields" Form Error
    $data = $request->request->all();
    $children = $form->all();
    $data = array_intersect_key($data, $children);
    $form->bind($data);

在我的情況下,解決方案非常簡單,只需將allow_add添加到您的示例下方的集合字段中

        ->add('Details', 'collection', array(
            'type' => new DetailsType(),
            'allow_add' => true,
            'allow_delete' => true,
            'label' => ' '
        ))

您還可以查看此問題的官方文檔http://symfony.com/doc/current/cookbook/form/form_collections.html

您需要做的第一件事是讓表單集合知道它將收到未知數​​量的標簽。 到目前為止,您已添加了兩個標記,並且表單類型期望接收兩個標記,否則將拋出錯誤:此表單不應包含額外字段。 要使其靈活,請將allow_add選項添加到集合字段中。

您無法添加額外的字段,因為它們未聲明為實體。 有一個解決方案可以繞過你的問題:

  • 創建一個動態表單,您可以在其中添加額外字段。

你有一個關於如何在github上工作的例子: https//github.com/Keirua/KeiruaProdCustomerDemoBundle

和這個地址的完整教程(但用法語):

http://blog.keiruaprod.fr/2012/01/18/formulaires-dynamiques-avec-symfony2/

PS:似乎Sonata使用這種方式添加字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM