Skip to content

forms organization

Harmen Janssen edited this page Aug 22, 2016 · 3 revisions

Code organisation

It's probably a good idea to define your forms in a central place. Not only ensures this noise-free editing of your forms as well as your controllers, it also allows you to reuse forms from one page to another. A good way to go about this is to create a new class in the App namespace:

/* @file: /library/App/Form/Login.php */
class App_Form_Login extends Garp_Form {
    public function init() {
        parent::init();
    
        $this->addElement('email', 'email', array(
            'label' => 'Your email',
            'required' => true
        ));

        $this->addElement('password', 'password', array(
            'label' => 'Your password', 
            'required' => true
        ));

	$this->addElement('submit', 'Login');
    }
}

Done. It's that easy. In your controller you can add similar code as before. The only difference is that you instantiate an App_Form_Login instead of a blank Garp_Form:

$form = new App_Form_Login();

Note that you should always call parent::init(), since that's where all the Garp_Form magic is.

Clone this wiki locally