Ninju
Ninju

Reputation: 2530

Zend form autoloading

I have a problem implementing forms in Zend framework 1.I added form in application/forms/CustomForm.php.And it looks like this.

class CustomForm extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');

        $id = $this->createElement('hidden','id');
        $firstname = $this->createElement('text','firstname');
        $firstname->setLabel('First Name:')
                    ->setAttrib('size',50);
        $lastname = $this->createElement('text','lastname');
        $lastname->setLabel('Last Name:')
                ->setAttrib('size',50);
        $username = $this->createElement('text','username');
        $username->setLabel('Username:')
                ->setAttrib('size',50);
        $email = $this->createElement('text','email');
        $email->setLabel('Email:')
                ->setAttrib('size',50);
        $password = $this->createElement('password','password');
        $password->setLabel('Password:')
                    ->setAttrib('size',50);

        $password2 = $this->createElement('password','password2');
        $password2->setLabel('Confirm Password::')
                    ->setAttrib('size',50);
        $register = $this->createElement('submit','register');
        $register->setLabel("Register")
                ->setIgnore(true);

        $this->addElements(array(
            $firstname,
            $lastname,
            $username,
            $email,
            $password,
            $password2,
            $id,
            $register
        ));
    }
}

and include path in index.php look like this

set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    realpath(APPLICATION_PATH . '/../application'),
    realpath(APPLICATION_PATH . '/../application/forms'),
)));

But when i call $form = new CustomForm();, in usercontroller.php,i'm getting this "Fatal error: Class 'CustomForm' not found in /var/www/demoapp/application/controllers/UserController.php on line 250".What might be the problem?

Upvotes: 1

Views: 40

Answers (1)

kejsu
kejsu

Reputation: 384

Wouldn't it be better to register your form classes in autoloader? http://framework.zend.com/manual/1.12/en/zend.loader.autoloader-resource.html

you can also do it in the application/Bootstrap.php file, STH LIKE::

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initResourceAutoloader()
    {
        $autoLoader = new Zend_Loader_Autoloader_Resource(array(
            'basePath'  => dirname(__FILE__),
            'namespace' => 'App'
        ));

        $autoLoader->addResourceType('form'     , 'forms'       , 'Form');'Grid');
    return $autoLoader;
}

Upvotes: 1

Related Questions