-- OakBehringer <[EMAIL PROTECTED]> wrote
(on Wednesday, 19 March 2008, 10:37 PM -0700):
> OK. So, say I have an action, the createFolderAction, which creates a folder
> on a filesystem. It creates an instance of Zend_Form, adds a text element to
> allow the user to specify the folder name, and adds a validator that
> verifies that the folder does not already exist before creating it. If the
> validation fails, the user gets a message that says something like "folder
> already exists, pick a new name".
> 
> Now, a new action is being added to the same controller, the
> uploadFileAction. It creates an instance of Zend_Form, adds a file element
> to it, and a select element to choose what folder to put the file in. Yay.
> Oh, there is one more field, and that's the one that matters. This is a text
> element that allows the end user to create a, you guessed it, new folder to
> upload the file to.
> 
> In order to avoid duplicate code, it seems like I should delegate [from the
> uploadFileAction] to the createFolderAction to handle the optional folder
> creation. I understand that you can push actions into the action stack or
> forward from one action to the next. Unless I'm missing something, this
> creates a problem for me:
> 
> When I create an instance of Zend_Form in uploadFileAction. It has three
> elements: 
> 
> file element - the file to be uploaded
> select element - the folder to upload it into
>  - or - text element - new folder to create and upload to (if "new" is
> selected in the select folder element, this value is used)
> 
> That's pretty much the problem... I can't really forward to the
> createFolderAction from here, because that will create an different instance
> of Zend_Form / text (foldername) element. It will see the posted value, but
> if it does not validate the error will not be returned to the correct form,
> etc. 

Move the logic to create the folder into a separate, non-action method
in your controller; that way you can call it from your
uploadFileAction() when necessary, and not need to go through the other
controller logic in createFolderAction.

    class FooController extends Zend_Controller_Action
    {
        public function createFolderAction()
        {
            $form = new FolderForm();

            // ...
            if (!$form->isValid($data)) {
                $this->view->form = $form;
                return;
            }

            $this->createFolder($form->getValue('path'));

            // ...
        }

        public function createFolder($path)
        {
            // ...
        }

        public function uploadFileAction()
        {
            $form = new UploadFileForm();
            
            // ...
            if (!$form->isValid($data)) {
                $this->view->form = $form;
                return;
            }

            if (!empty($form->getValue('folder'))) {
                $this->createFolder($form->getValue('path'));
            }

            // ...
        }
    }

You should also consider having your uploadFile form extend the
createFolder form, and have it simply add the extra element. This will
encourage re-use, and also make certain that the validations for the
folder stay the same between the two forms. 

-- 
Matthew Weier O'Phinney
PHP Developer            | [EMAIL PROTECTED]
Zend - The PHP Company   | http://www.zend.com/

Reply via email to