|
View:
New views
5 Messages
—
Rating Filter:
Alert me
|
|
|
Zend_Form : Subforms, dynamic element creation and best practices
|
|
|
|
Re: Zend_Form : Subforms, dynamic element creation and best practices-- Bill P. <maxarbos@...> 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 | matthew@... Zend Framework | http://framework.zend.com/ |
|
|
|
Re: Zend_Form : Subforms, dynamic element creation and best practicesI left out that we are also including : $this->setDecorators(array( 'FormElements', array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')), array('Description', array('placement' => 'prepend')), 'Form' )); in the __construct, but moving this all to the init() doesnt seem to load the decorators in time. I was pretty much doing the same things, but included foreach($children as $key=>$val){ $field.$key = $this->addElement('text', 'fieldname'.$key, etc....) $group[]='fieldname'.$key; } $this->addDisplayGroup( $number_group, 'groupname' ....) and ended up getting error of: 'No valid elements specified for display group' So once I got rid of the '$field.$key =' it all worked correctly. Do you have any other updated resources for best practices or structure of a large site using this framework? thanks. |
|
|
|
Re: Zend_Form : Subforms, dynamic element creation and best practices-- maxarbos <maxarbos@...> wrote
(on Tuesday, 01 July 2008, 10:10 AM -0700): > > > > Matthew Weier O'Phinney-3 wrote: > > > >> > >> 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().....); > >> } > >> > >> } > >> > >> > > > > I left out that we are also including : > > $this->setDecorators(array( > 'FormElements', > array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')), > array('Description', array('placement' => 'prepend')), > 'Form' > )); > > in the __construct, but moving this all to the init() doesnt seem to load > the decorators in time. It _should_ work... there's a test for exactly that situation in the testbed. If it's not working, post the simplest possible use case where it fails to the tracker so I can try and verify. > Matthew Weier O'Phinney-3 wrote: > > > >> 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')); > > } > > > > > > I was pretty much doing the same things, but included > foreach($children as $key=>$val){ > $field.$key = $this->addElement('text', 'fieldname'.$key, etc....) > > $group[]='fieldname'.$key; > } > > $this->addDisplayGroup( > $number_group, 'groupname' ....) > > > and ended up getting error of: > > 'No valid elements specified for display group' > > > So once I got rid of the '$field.$key =' it all worked correctly. > > > Do you have any other updated resources for best practices or structure of a > large site using this framework? > > thanks. > > -- > View this message in context: http://www.nabble.com/Zend_Form-%3A-Subforms%2C-dynamic-element-creation-and-best-practices-tp18203896p18220883.html > Sent from the Zend Framework mailing list archive at Nabble.com. > -- Matthew Weier O'Phinney Software Architect | matthew@... Zend Framework | http://framework.zend.com/ |
|
|
|
Re: Zend_Form : Subforms, dynamic element creation and best practices
So since all of my elements are being created in the init() method and that seems to run even before the __construct , how to I get the value to $children ? If i call $renderform = new TestForm() from my Controller, the form is already built by the time I try to set the $children by $renderform->children = array('aaa', 'bbb', 'ccc'); I have been following the tutorials at: http://blog.astrumfutura.com/archives/360-Example-Zend-Framework-Blog-Application-Tutorial-Part-6-Introduction-to-Zend_Form-and-Authentication-with-Zend_Auth.html Is this still the recommended way to create a form with my needs? |
| Free embeddable forum powered by Nabble | Forum Help |