-- Bill P. <[EMAIL PROTECTED]> wrote
(on Monday, 30 June 2008, 01:40 PM -0700):
> I am trying to understand the best recommend way to implement a form using
> Zend_Form. There seem to be a number of different ways in implementing and
> rendering them.
> 
> Here is my situation:
> 
> We are developing an application and will be using a number of ZF components:
> _DB, _Auth, _ACL, _View, _Layout, _Controller, etc...  as well as _Form.
> 
> When we want to built and show a form, we call a model like: TestingForm.php
> from TestingController.php
> 
> The TestingController.php has a method:
> getTestingForm() {
>    $this->_myform = new TestingForm(array('action'=>testing/process', 'method'
> =>'post'));
> }
> 
> 
> The TestingForm.php is built as such:
> TestForm extends Zend_Form {
>    public function __construct($options=null) {
>           parent::__construct($options);
> 
>      $this->setAttrib('accept-charset', 'UTF-8');
> }

I'd place the above in setAttrib() call as the first call in your init()
method below, and get rid of __construct() entirely.

>    public function init() {
>      $name = $this->addElement('text', 'name', array().....);
> }
> 
> }
> 
> 
> 
> 
> So now on my display page with the form, I have an action
> indexAction() {
>      $this->view->testform = $this->getTestingForm();
> }
> 
> and the view is:
> <?php echo $this->testform; ?>
> 
> 
> =================
> 
> Is this recommened?

This is how I do most of my forms, and how I recommend doing forms in my
presentations and webinars.

> 2)  I want to be able to add elements dynamically to the form depending on a
> result set of data from the db.
> 
> Say a person has 4 children, I want to dynamically add four text elements to
> the form, one for each record retrieved from the db.
> 
> How can I do this with my setup?

Yes. Create an element for each child, and attach each with a different
form element name:

    foreach ($children as $key => $child) {
        $form->addElement('text', 'child' . $key, array('value' => $child'));
    }

-- 
Matthew Weier O'Phinney
Software Architect       | [EMAIL PROTECTED]
Zend Framework           | http://framework.zend.com/

Reply via email to