Results 1 to 2 of 2

Thread: Populating form: automatically generate multi-fields for values array

  1. #1
    Exception e is offline Junior Member
    Join Date
    May 2009
    Posts
    4

    Default Populating form: automatically generate multi-fields for values array

    Suppose I want to have a series of text inputs that should be sent in array (mytext[]). How do you force zf to generate mytext elements when you populate the form with a set of values?

    Suppose I've this:
    [PHP]$this->addElement('text', 'mytext', array(
    'label' => 's'
    ,'isArray' => true
    ));

    // in controller:
    $form->populate(array('mytext' => array("0"=> '342', "1" => "123", "2" => "dd")));
    [/PHP]
    results in
    Warning: htmlspecialchars() expects parameter 1 to be string, array given in (...) \library\Zend\View\Abstract.php on line 849
    This doesn't work either
    [PHP]$this->addElement('text', 'mytext', array(
    'label' => 's'
    ,'isArray' => true
    ,'value' => array('a', 'b', 'c')
    ));[/PHP]

    Why can you specify an element to be an array when you can't do anything with it?
    Ok, try an alternative route:

    [PHP]$sub = new Zend_Form_SubForm();
    $sub->addElement('text', '0', array(
    'label' => 's'
    ));
    $sub->addElement('text', '1', array(
    'label' => 's'
    ));
    $this->addSubForm($sub, 'mytext');

    // in controller:
    $form->populate(array('mytext' => array("0"=> '342', "1" => "123", "2" => "dd")));[/PHP]


    this shows the first two fields, but the third ("dd") isn't shown.

    I want (and expect) zf to generate mytext-elements for every value I pass it. In the browser I add extra mytext fields dynamically via javascript.
    This is a common use case. I obviously miss something, but what?

    Any help is greatly appreciated!!!

    PS: is the correct forum for Zend_Form?

  2. #2
    Exception e is offline Junior Member
    Join Date
    May 2009
    Posts
    4

    Default possible solution

    I've discovered this way works, but I feel that it could be generalized.
    I am surprised that this use case is not provided for in the library.

    Here is the rough code:

    [PHP]function init() {
    $sub = new Zend_Form_SubForm();
    $this->addSubForm($sub, 'mytext');
    }

    function populate(array $values) {
    $mytext = $values['mytext'];

    foreach (array_keys($mytext) as $key) {
    $this->getSubForm('mytext')->addElement(
    'text', (string) $key, array(
    )
    );
    }

    return parent:opulate($values);
    }[/PHP]

Similar Threads

  1. Populating multi select form element for Editing data
    By php_reddy in forum Core Infrastructure
    Replies: 1
    Last Post: 04-28-2010, 04:04 PM
  2. Zend_Form, Zend_Form_SubForm, array notation and ajax generated fields
    By Arjuna in forum General Q&A on Zend Framework
    Replies: 0
    Last Post: 10-30-2009, 05:29 PM
  3. Help with _getAllParams and Multi-Select Return Values
    By johnlamont in forum Model-View-Controller (MVC)
    Replies: 3
    Last Post: 06-21-2009, 01:04 PM
  4. Zend_Search_Lucene multi-value fields
    By s3000xl in forum Mail, Formats & Search
    Replies: 10
    Last Post: 03-04-2008, 04:13 PM
  5. Replies: 4
    Last Post: 11-21-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •