1
votes

I've been googling most of the day trying to find an answer to this, but I've finally admitted defeat.

I'm working on a form in Zend Framework that needs to be able to handle variable data lengths. I have a form with some general fields that specify some general parameters (item name, language, etc) that are easy enough to deal with, but I've also got a subform called parameters which holds a variable number of key/value lairs that let you add generic parameters into the data. Demonstration form follows:

<form>
    <input type="text" name="item_name" />
    <input type="text" name="item_lang" />
    <!-- etc -->
    <input type="text" name="parameters[1][key]" />
    <input type="text" name="parameters[1][value]" />
    <input type="text" name="parameters[2][key]" />
    <input type="text" name="parameters[2][value]" />
    <input type="text" name="parameters[3][key]" />
    <input type="text" name="parameters[3][value]" />
    <input type="text" name="parameters[4][key]" />
    <input type="text" name="parameters[4][value]" />
    <!-- and so on -->
</form>

Note: the above is a massive simplification of the actual form. It's also manually built instead of being generated by zend_form.

The number of parameters can differ and can be handled client side with javascript, but I'm really struggling to initialize a form when populating it from pre-existing data for update.

I might have 2 parameters stored per item, I might have 20, I might have none at all. So I need the form to have 2 or 20 or no parameters inputs depending on the initial state of the data. Unfortunately the data is not available in init () because it's not in the form until you call setDefaults () on it.

This means I can't do a foreach() on the initial state of the form to generate the appropriate number of input boxes for the already-existing data.

I'm sure I must be missing something obvious, but the Zend documentation is pretty dreadful and I can't find any examples of this use case. It surely can't be so uncommon that it's not supported in zend_form. How do I go about generating the form in a state that allows for the initial state of the form to be variable?

ETA: The init() method on my form looks something like this (simplified to match the example):

public function init ()
{
    parent::init ();

    $this -> addElement ('text', 'item_name');
    $this -> addElement ('text', 'item_lang');
    $this -> addSubForm (new Zend_Form_SubForm (), 'parameters');

    foreach ($phantom_data as $key => $val)
    {
        $params = new Zend_Form_SubForm ();
        $params -> addElement ('text', 'key');
        $params -> addElement ('text', 'value');
        $this -> parameters -> addSubForm ($params, $key);
    }
}
1

1 Answers

1
votes

You can make data avaialbe to form like

My_Form extends Zend_Form 
{

protected $_myCustomData; 

public function __construct($options = null,$myCustomData)
{
   $this->_myCustomData = $myCustomData;
    parent::__construct($options);  //Its important you call parent after above line or init will get call before initilizing customData
}

public function init()
{
  $this->_myCustomData ; //here you are free to use your custom data 

}

}