title: symfony 1.4 Embed a subform in another subform with ajax
body: <p>I know symfony 1.4 Form system is not the best and is causing many people a lot of troubles, but I need to get finished with this task and I need help.</p>
  
  <p>I need to embed a subform into a subform that is embedded in a main form, and do it via Ajax.</p>
  
  <p>Basically I have aForm, bForm and cForm and aForm embeds bForm and bForm can have one or more cForm.</p>
  
  <p>When I embed them normally from the configure() function of each form it works. But when I try to use Ajax to embed multiple cForms into bForm I fail to bind them.</p>
  
  <p>This is the version that works. Everything is embedding ok and validating ok.</p>
  
  <pre><code>class BookingForm extends BaseBookingForm
  {
    public function configure()
    {     
        $this-&gt;embedRelation('Journey as journey');
    }
  }
  
  [...]
  
  class JourneyForm extends BaseJourneyForm
  {
    public function configure()
    {
        $pickup_form = new JourneyItineraryForm();
        $this-&gt;embedForm('pickup_form', $pickup_form);
  
    }
  } 
  
  [...]
  
  class JourneyItineraryForm extends BaseJourneyItineraryForm
  {
    public function configure()
    {
    }
  }
  
  [...]
  </code></pre>
  
  <p>Now, if I try to embed JourneyItineraryForm via ajax I managed to get the widgets displayed in the template but it fails to bind them. It says to me <strong>Unexpected extra form field named "waypoint".</strong></p>
  
  <p>View code below:</p>
  
  <pre><code>class JourneyForm extends BaseJourneyForm
  {
        public function configure()
        {
            $waypoint_form = new JourneyItineraryForm();
            $this-&gt;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 &lt;= $number; $i+=1)
         {
           $waypoint = new JourneyItinerary();
           $waypoint_form = new JourneyItineraryForm($waypoint);
           $new_waypoints-&gt;embedForm($i,$waypoint_form);
         }
  
        $this-&gt;embedForm('waypoint', $new_waypoints);
        }
  
      public function bind(array $taintedValues = null, array $taintedFiles = null)
      {
  
        $new_occurrences = new BaseForm();
        foreach($taintedValues['waypoint'] as $key =&gt; $new_occurrence)
        {
          $occurrence = new JourneyItinerary();      
          $occurrence_form = new JourneyItineraryForm($occurrence);
          $new_occurrences-&gt;embedForm($key,$occurrence_form);
        }
  
        $this-&gt;embedForm('waypoint',$new_occurrences);
  
        parent::bind($taintedValues, $taintedFiles);
      }
   } 
  </code></pre>
  
  <p>I my template I manage to display the waypoints widgets like</p>
  
  <pre><code>$form['journey']['waypoint'][0]['field_name']-&gt;renderRow();
  </code></pre>
  
  <p>I also tried to overwrite the bind method from BookingForm but I don't know if I did it correctly:</p>
  
  <pre><code>public function bind(array $taintedValues = null, array $taintedFiles = null)
  {   
      $new_occurrences = new sfForm();
      foreach($taintedValues['journey']['waypoint'] as $key =&gt; $new_occurrence)
      {
        $occurrence = new JourneyItinerary();      
        $occurrence_form = new JourneyItineraryForm($occurrence);
  
        $new_occurrences-&gt;embedForm($key,$occurrence_form);
      }
  
      $this-&gt;embedForm('journey',$new_occurrences);
  
      parent::bind($taintedValues, $taintedFiles);
  }
  </code></pre>
  
  <p>I followed this tutorial:
  <a href="http://tech.cibul.net/embedded-forms-with-symfony-1-4-and-jquery/" rel="nofollow">http://tech.cibul.net/embedded-forms-with-symfony-1-4-and-jquery/</a>
  and also read the official documentation <a href="http://symfony.com/legacy/doc/more-with-symfony/1_4/en/06-Advanced-Forms" rel="nofollow">http://symfony.com/legacy/doc/more-with-symfony/1_4/en/06-Advanced-Forms</a></p>
  
  <p>Any help is highly appreciated :)
  Thanks.</p>