簡體   English   中英

Symfony2使用動態形式更新實體字段

[英]Symfony2 update entity field with dynamic form

我正在嘗試使用一些動態生成的表單來更新我的實體,以執行修改。 值使用x-editable與ajax發送。 我的問題是我無法執行修改,因為不會提交表單,我也不知道為什么。

這是我當前的代碼:

// I can pass all those data using X-editable without a problem
$id         = 1;              // entity ID
$value      = 'someNewValue'; // new value
$type       = 'text';         // type of field
$schem      = array('AppBundle', 'membre', 'prenom'); // The stuff used to to work this out
$em         = $this->getDoctrine()->getManager();
$entity     = $em->getRepository($schem[0] . ':' . ucfirst($schem[1]) )->find($id); // Now I've got my entity

// I create a dynamic form with no CSRF protection (as I read in some other post)
$form = $this->createFormBuilder($entity, array(

        'csrf_protection' => false
    )
)->add($schem[2])
  ->getForm();

var_dump($form->isValid()); // false
$form->get($schem[2])->submit($value); // trying to submit the new value
var_dump($form->isValid()); // still false

var_dump($form->getErrorsAsString()); // Shows ''

這是我實體的目標字段(在此示例中)

/**
 * @var string
 * @ORM\Column(name="prenom", type="string", length=255)
 */
private $prenom;

謝謝你的幫助 !

好的,我找到了解決方案。 主要問題發生在我嘗試提交新值的行上

$form->get($schem[2])->submit($value);

我不知道為什么,但這根本不起作用。 實際上,我必須在表單本身上使用submit ,如下所示:

$form->submit( array($field => $value) );

其中$field是我實體中的更新字段,而$value是新值。 有關更多信息,請查閱文檔

暫無
暫無

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

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