Saturday 14 July 2012

Pimcore, Helpers and Forms

It's taken me a while to get around to this, but I'm finally getting somewhere with this. Here's the code I've got. Underneath is an explanation of everything.

I've assumed you're using the formbuilder plugin for Pimcore with this code.
    
        
    public function init()
    {        
        parent::init();
        $this->_helper->addPath(PIMCORE_WEBSITE_PATH . '/views/helpers/cru', 'Website_Helper_Cru');
    }
    
    public function formpageAction()
    {
        // Make sure I want to display the form
        // the useForm property is a checkbox, so I always get true/false
        if ($this->document->getProperty('useForm') && !empty($this->document->getProperty('formName'))) {
            // Put everything in a try/catch statement, as there is some buggy code here
            try {
                $this->formHelper = $this->_helper->getHelper('Forms');
                $form = $this->formHelper->getForm($this->document->getProperty('formName'));  

                // Do I have a submitted form to worry about?
                if ($this->getRequest()->isPost()) {
                    if (!$form->isValid($_POST)) {
                    }
                }
                // We are going to display the form
                $this->formHelper->setDecorators($form, 'default');
                $this->view->form = $form;
            } catch (Exception $e) {
                // Log the error if I had one
                Logger::error($e->getMessage());
            }
        }
        $this->enableLayout();
    }


Ok. This is (hopefully) obviously all inside your controller.

When the controller is initialised, add an extra path to the helpers paths. Now, instead of just looking inside Zend, my controller will now look for helpers in the /website/views/helpers/cru/ folder.

Now, in my document that is using the formpage action, I look for 2 properties. A checkbox called useForm (ticked on) and a formName (I have to know which form to load!).

If both of those properties exist and have a value, add on a formHelper to the action so it can do things.

This class is called Website_Helper_Cru_Forms and the filename is Forms.php. I'm sure you can figure out which folder it's in.

I haven't fleshed out the validation of the form. I imagine I'll end up adding something in the formHelper class to do this.

I add on the decorators using my helper. I haven't yet got a good way of doing this, I plan on using .ini files but I'll get around to this later.

And finally, add the form to the view. In the view you simply call echo $this->form to print it out. If you've set up the decorators and validators correctly everything displays in a nice pretty format :)

I've also wrapped the whole thing in a try/catch statement. There are quite a few ways this could break at present as I don't do much error checking yet. That's another thing on the to do list.

Let me know of any obvious errors you can see with this code in the comments below. I hope this helps somebody!

No comments:

Post a Comment