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 <[email protected]> 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.

Reply via email to