开发者

Variable number of fields on a form using Zend_Form in the Zend Framework

I have a db table and a linked table "site" is defined in the first and in the second a number of values varying from one to many can be added that point back to the "site". The user can open the "site" field in a form with the linked data then showing in fields so the user can edit the values.

e.g. "Site One" has one related field.

"Site Two" has three related fields.

"Site Three" has two related fields. Etc.

If i was hand cranking it i would just read the data in and create the extra fields but i am building it with Zend_Form which appears to want to know the number of fields in advance. I am using a model in 开发者_如何学运维the models directory with an _form_xxx.phtml in the scripts directory (a partial?).

I am newish to Zend and feeling my way a bit so is there a way in Zend_Form of doing this as i would seem to have to rewrite php and phtml otherwise?

Are sub forms, Zend_Form_SubForm's, a way to go?

As Zend is flexible i assume at the worst i can write the component in the plain vanilla way but i would like to use the tools in the framework if possible.


What makes you think that the Zend_Form should be static? You can dynamically create the fields inside your form at runtime as you see fit. Just put the logic that adds your dynamic form elements in the constructor of your form class (which extends Zend_Form).

ie.

class YourSplendidForm extends Zend_Form
{
    public function __construct($options = null, $yourParams)
    {
        parent::__construct($options);

        $this->setName('your_form_name');
        $this->setAction('/your/form/action/');

        // Add your fields here using addElement - or create a subform and add fields to that one and use addSubForm to add the subform


    }
}

Then simply pass the form instance to your view (from the controller) and process it as needed (either through a simple <?php echo $this->form; ?> or custom rendering as you see fit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜