1

i need help with my dynamically extensible zend form.

I have form with subform, which contains two elements:

<form>
  <fieldset class="itemGroup">
    <label>
      Question
      <input type="text"  name="items[questions][]" value="">
    </label>
    <label>
      Answer
      <input type="text"  name="items[answers][]" value="">
    </label>
  </fieldset>
</form>

I obtained it with following procedure:

$itemsSubform = new Zend_Form_SubForm();
$form->addSubForm($itemsSubform, 'items');
$itemsSubform->setElementsBelongTo('items');

$itemQuestion = new Zend_Form_Element_Text('questions', array(
    'label' => 'Question',
    'isArray' => true,
    'filters' => array(
        'stringTrim',
    ),
    'validators' => array(
        array('stringLength', array('max' => 255)),
    ),
));

$itemAnswer = new Zend_Form_Element_Text('answers', array(
    'label' => 'Answer',
    'isArray' => true,
    'filters' => array(
        'stringTrim',
    ),
    'validators' => array(
        array('stringLength', array('max' => 255)),
    ),
));

$itemsSubform->addDisplayGroup(array($itemQuestion, $itemAnswer), 'itemGroup');

If is needed, i just copy all fieldset for extend form by javascript.

All is working correct, until i submit form. During validation is no element validated, and during rendering form, populated by such data, i get error message from class Zend_View_Abstract that escaping value is array instead of string (this method is called during rendering element for escape its value).

For compltion, if i call $form->getValues(); after validation, (by javascript another fieldset is added) i get this:

Array
(
    [items] => Array
        (
            [questions] => Array
            (
                [0] => lorem
                [1] => dolor
            )

            [answers] => Array
            (
                [0] => ipsum
                [1] => sit
            )

        )

)

Could someone advise me how to behave to form? The ideal solution would be when form themselves validate each value separately and find how many times should he render fieldset (displayGroup).

the.ufon
  • 11
  • 1

1 Answers1

0

First your elements fail to validate because you set isArray => TRUE and the validator you chose evaluates strings.

Next you issue with populating the form I believe is likely due to the fact that you need to supply the data to populate in the same multidimensional array as the form produces (the arrays should map one to one).

The link below is to an example of how to build a form dynamically, you should be able to use it as a template to produce one to fill your needs.

Example of generating form elements with loop

Community
  • 1
  • 1
RockyFord
  • 8,529
  • 1
  • 15
  • 21