Re: [fw-general] Best architecture for Zend_Form
On Mon, Jan 19, 2009 at 11:30 AM, GJ Bogaerts wrote: > > > Mark Wright-3 wrote: >> >> I would recommend storing it in the registry, unless you have reasons >> to tether it to the controller. >> > > Well, that was my initial idea as well, but I can't seem to get the registry > to remember the form instance. When I set it in the initial controller and I > do a Zend_Debug::dump on the registry, I see it loud and clear. But then in > the next request (after posting) it's gone... really strange. The registry aims to implement the registry pattern which is not about persistence. ;-) If you really need the form everywhere instance, you will have to set it up in your bootstrap and assign it into your registry there. But I wouldn't recommend that really. Till
Re: [fw-general] Best architecture for Zend_Form
Mark Wright-3 wrote: > > I would recommend storing it in the registry, unless you have reasons > to tether it to the controller. > Well, that was my initial idea as well, but I can't seem to get the registry to remember the form instance. When I set it in the initial controller and I do a Zend_Debug::dump on the registry, I see it loud and clear. But then in the next request (after posting) it's gone... really strange. Mark Wright-3 wrote: > > As for editing the form I would recommend creating methods in the form > class that do all of that. > Those are excellent suggestions. Thanks. I did as you suggested. -- View this message in context: http://www.nabble.com/Best-architecture-for-Zend_Form-tp21539087p21540317.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] Best architecture for Zend_Form
I would recommend storing it in the registry, unless you have reasons to tether it to the controller. As for editing the form I would recommend creating methods in the form class that do all of that. For example: if (!is_null($this->loggedInUser)) { $form->loggedIn($this->loggedInUser['userId']); } else { $form->notLoggedIn(); } Then in the form class: public function loggedIn($userId) { $this->getElement('user_id')->setValue($userId); $this->removeElement('naam'); $this->removeElement('website'); $this->removeElement('antispam'); } public function notLoggedIn() { $this->getElement('user_id')->setValue(''); } The loggedIn() and notLoggedIn() methods (or whatever you want to call them) will alter the form for the proper context. Now if you ever need to make the same change to the form some place else (like when the form is being processed) it is easily accomplished with a simple method call. You could also pass $this->loggedInUser to the form class constructor so that it can determine whether or not the elements naam, website and antispam should even be created. Or you could extend populate() so that when the form is being processed you don't need your controller to determine if the user is logged in. If the user_id key does not have a null value it will know to call loggedIn() and then parent::populate(). Mark sorry for sending twice GJ, I always forget to hit reply all On Mon, Jan 19, 2009 at 2:02 AM, GJ Bogaerts wrote: > > Hi all, > > I have a controller which sets up (amends) a form, which is a subclass of > Zend_Form: mainly the controller sets some values and it removes or adds > some elements, depending on whether or not the user is logged in. This is > the code that sets up the form: > >$form = new VK_Forms_User_Comment(); >if (!is_null($this->loggedInUser)) { > > $form->getElement('user_id')->setValue($this->loggedInUser['userId']); >$form->removeElement('naam'); >$form->removeElement('website'); >$form->removeElement('antispam'); >} else { >$form->getElement('user_id')->setValue(''); >} > > $form->getElement('bijdrage_id')->setValue($this->bericht['bijdrage_id']); >$form->getElement('ip')->setValue($_SERVER['REMOTE_ADDR']); > > Now this form posts to another controller. I'm having some trouble deciding > on the best architecture: how do I access this amended form from the > controller that is being posted to? Obviously, I cannot access > VK_Forms_User_Comment directly, because of the changes that were made. So > far, I've solved it by making the form a public static variable of the > initial controller that instantiates and amends the form, and I pick it up > again in the controller that is being posted to; but is that really the best > way to go? > > Thanks for any input! > -- > View this message in context: > http://www.nabble.com/Best-architecture-for-Zend_Form-tp21539087p21539087.html > Sent from the Zend Framework mailing list archive at Nabble.com. > > -- Have fun or die trying - but try not to actually die.
[fw-general] Best architecture for Zend_Form
Hi all, I have a controller which sets up (amends) a form, which is a subclass of Zend_Form: mainly the controller sets some values and it removes or adds some elements, depending on whether or not the user is logged in. This is the code that sets up the form: $form = new VK_Forms_User_Comment(); if (!is_null($this->loggedInUser)) { $form->getElement('user_id')->setValue($this->loggedInUser['userId']); $form->removeElement('naam'); $form->removeElement('website'); $form->removeElement('antispam'); } else { $form->getElement('user_id')->setValue(''); } $form->getElement('bijdrage_id')->setValue($this->bericht['bijdrage_id']); $form->getElement('ip')->setValue($_SERVER['REMOTE_ADDR']); Now this form posts to another controller. I'm having some trouble deciding on the best architecture: how do I access this amended form from the controller that is being posted to? Obviously, I cannot access VK_Forms_User_Comment directly, because of the changes that were made. So far, I've solved it by making the form a public static variable of the initial controller that instantiates and amends the form, and I pick it up again in the controller that is being posted to; but is that really the best way to go? Thanks for any input! -- View this message in context: http://www.nabble.com/Best-architecture-for-Zend_Form-tp21539087p21539087.html Sent from the Zend Framework mailing list archive at Nabble.com.