symfony 1.4使用ajax将子表单嵌入另一个子表单中

时间:2013-03-15 15:12:57

标签: php ajax forms symfony-1.4

我知道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,我设法将模板显示在模板中,但它无法绑定它们。它告诉我名为“waypoint”的意外额外表单字段。

查看以下代码:

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

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

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此插件ahDoctrineEasyEmbeddedRelationsPlugin,它简单快捷。但你不能嵌入嵌入,但在你的情况下,你有一种形式2嵌入形式。