0

我知道 symfony 1.4 表单系统不是最好的,给很多人带来很多麻烦,但我需要完成这项任务,我需要帮助。

我需要将子表单嵌入到嵌入主表单的子表单中,并通过 Ajax 进行。

基本上我有 aForm、bForm 和 cForm 并且 aForm 嵌入 bForm 和 bForm 可以有一个或多个 cForm。

当我从每个表单的 configure() 函数中正常嵌入它们时,它可以工作。但是当我尝试使用 Ajax 将多个 cForms 嵌入到 bForm 中时,我无法绑定它们。

这是有效的版本。一切都很好嵌入并且验证正常。

class BookingForm extends BaseBookingForm
{
  public function configure()
  {     
      $this->embedRelation('Journey as journey');
  }
}

[...]

class JourneyForm extends BaseJourneyForm
{
  public function configure()
  {
      $pickup_form = new JourneyItineraryForm();
      $this->embedForm('pickup_form', $pickup_form);

  }
} 

[...]

class JourneyItineraryForm extends BaseJourneyItineraryForm
{
  public function configure()
  {
  }
}

[...]

现在,如果我尝试通过 ajax 嵌入 JourneyItineraryForm,我设法让小部件显示在模板中,但无法绑定它们。它对我说意外的额外表单字段名为“航点”。

查看下面的代码:

class JourneyForm extends BaseJourneyForm
{
      public function configure()
      {
          $waypoint_form = new JourneyItineraryForm();
          $this->embedForm('waypoint', $waypoint_form);

      }

      public function addNewWaypoint($number)
      {
       /*
       * Called from actions.class.php after an ajax request
       */

       $new_waypoints = new BaseForm();

       for($i=0; $i <= $number; $i+=1)
       {
         $waypoint = new JourneyItinerary();
         $waypoint_form = new JourneyItineraryForm($waypoint);
         $new_waypoints->embedForm($i,$waypoint_form);
       }

      $this->embedForm('waypoint', $new_waypoints);
      }

    public function bind(array $taintedValues = null, array $taintedFiles = null)
    {

      $new_occurrences = new BaseForm();
      foreach($taintedValues['waypoint'] as $key => $new_occurrence)
      {
        $occurrence = new JourneyItinerary();      
        $occurrence_form = new JourneyItineraryForm($occurrence);
        $new_occurrences->embedForm($key,$occurrence_form);
      }

      $this->embedForm('waypoint',$new_occurrences);

      parent::bind($taintedValues, $taintedFiles);
    }
 } 

我的模板我设法显示航点小部件,例如

$form['journey']['waypoint'][0]['field_name']->renderRow();

我还尝试从 BookingForm 覆盖 bind 方法,但我不知道我是否正确:

public function bind(array $taintedValues = null, array $taintedFiles = null)
{   
    $new_occurrences = new sfForm();
    foreach($taintedValues['journey']['waypoint'] as $key => $new_occurrence)
    {
      $occurrence = new JourneyItinerary();      
      $occurrence_form = new JourneyItineraryForm($occurrence);

      $new_occurrences->embedForm($key,$occurrence_form);
    }

    $this->embedForm('journey',$new_occurrences);

    parent::bind($taintedValues, $taintedFiles);
}

我遵循了本教程: http ://tech.cibul.net/embedded-forms-with-symfony-1-4-and-jquery/ 并阅读了官方文档http://symfony.com/legacy/doc/more -with-symfony/1_4/en/06-Advanced-Forms

非常感谢任何帮助:) 谢谢。

4

1 回答 1

0

你可以试试这个插件ahDoctrineEasyEmbeddedRelationsPlugin,它简单快速。但是你不能在嵌入中嵌入,但在你的情况下,你有一种形式 2 嵌入形式。

于 2013-03-20T09:24:25.590 回答