0
votes

I want to create multi-level embed form in symfony 1.4.

I have three forms:

1 - Question form, 2 - Option form and 3 - answer form.

In that, one question have many options fields and one option have many answers fields.

When I tried to add one more field in option criteria by rendering the HTML for answer fields using ajax and try to save form that time I am getting error Unexpected extra form field named "1"

And I have embedded Option form in Question form and Answer form in Option form respectively.

The below error is produce after submit form :

pick_type_option [0 [pick_type_answers [Unexpected extra form field named "1".]]]

Can any one please help me to find out the solution for this problem.

In advanced thanks

1

1 Answers

0
votes

As it's unknown how many Option forms will be there during construction of the form (inside configure(), you have to override bind() method and add as many as needed:

class QuestionForm extends sfForm {

  public function configure() {
    $this->embedFormArray('question', new OptionForm);
  }

  private $formArray = array();

  public function embedFormArray($name, \sfForm $form, $decorator = null, $innerDecorator = null, $options = array(), $attributes = array(), $labels = array()) {
    $this->formArray[] = array($name, $form, $decorator, $innerDecorator, $options, $attributes, $labels);
  }

  public function bind(array $taintedValues = null, array $taintedFiles = null) {
    foreach($this->formArray as $fa) {
      $c = (isset($taintedValues[$fa[0]]) ? count($taintedValues[$fa[0]]) : 0);
      array_splice($fa, 2, 0, [$c]);
      call_user_func_array(array($this, 'embedFormForEach'), $fa);
    }

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