【问题标题】:symfony 1.4 Embed a subform in another subform with ajaxsymfony 1.4 使用 ajax 将子表单嵌入到另一个子表单中
【发布时间】:2013-03-15 15:12:56
【问题描述】:

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

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

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

当我从每个表单的 configure() 函数中正常嵌入它们时,它会起作用。但是当我尝试使用 Ajax 将多个 cForm 嵌入到 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 中的绑定方法,但我不知道我是否正确:

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

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

【问题讨论】:

    标签: php ajax forms symfony-1.4


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 2013-01-29
      相关资源
      最近更新 更多