Search code examples
zend-frameworkzend-formzend-form-element

zend form giving error


I am new to ZF.i have made a function that basically make a Form this is code

 require_once 'Zend/Form.php';
  function getLoginForm(){
$username = new Zend_Form_Element_Text('username');
$username->setLabel('Username:')
        ->setRequired(true);

$password = new Zend_Form_Element_Password('password');
$password->setLabel('Password:')
        ->setRequired(true);

$submit = new Zend_Form_Element_Submit('login');
$submit->setLabel('Login');

$loginForm = new Zend_Form();
$loginForm->setAction('/login/index/')
        ->setMethod('post')
        ->addElement($username)
        ->addElement($password)
        ->addElement($submit);
 return $loginForm;
}

this is the error

Fatal error: Class 'Zend_Form_Element_Text' not found in C:\xampp\htdocs\phoggi\application\controllers\LoginController.php on line 68

line 68 refers to this line

 $username = new Zend_Form_Element_Text('username');

Further how can i add css classes to each and every element in my form and also how to add my own error messages.plz take one element and add custom error messages and css class.Thanking you all. EDITED this is my index.php

<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);
$rootDir = dirname(dirname(__FILE__));
set_include_path($rootDir . '/library' . PATH_SEPARATOR . get_include_path());
require_once 'Zend/Controller/Front.php';
require_once 'Zend/Registry.php';
require_once 'Zend/Paginator.php';
include_once 'Zend/Db/Adapter/Pdo/Mysql.php';
require_once 'Zend/View.php';
require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
$params = array('host'         => 'localhost',
        'username'  => 'root',
        'password'    => '',
        'dbname'        => 'xyz'
       );

     $DB      = new Zend_Db_Adapter_Pdo_Mysql($params);
       $DB->setFetchMode(Zend_Db::FETCH_OBJ);
     Zend_Registry::set('DB',$DB);

 $view = new Zend_View();
 $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
 $viewRenderer->setView($view);
 Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
 Zend_Controller_Front::run('../application/controllers');
 ?>

Solution

  • here is what a standard index.php file looks like as created by Zend_Tool version 1.11:

    // Define path to application directory
    defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
    
    // Define application environment
    defined('APPLICATION_ENV')
        || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
    
    // Ensure library/ is on include_path
    set_include_path(implode(PATH_SEPARATOR, array(
        realpath(APPLICATION_PATH . '/../library'),
        get_include_path(),
    )));
    
    /** Zend_Application */
    require_once 'Zend/Application.php';
    
    // Create application, bootstrap, and run
    $application = new Zend_Application(
        APPLICATION_ENV,
        APPLICATION_PATH . '/configs/application.ini'
    );
    $application->bootstrap()
                ->run();
    

    this along with the application.ini are all that are required to make Zend Framework work.

    //application.ini
    
    [production]
    phpSettings.display_startup_errors = 0
    phpSettings.display_errors = 0
    includePaths.library = APPLICATION_PATH "/../library"
    bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
    bootstrap.class = "Bootstrap"
    appnamespace = "Application"
    resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
    resources.frontController.params.displayExceptions = 0
    
    ;database setup
    resources.db.adapter = PDO_MYSQL
    resources.db.params.host = localhost
    resources.db.params.username = username
    resources.db.params.password = password
    resources.db.params.dbname = databasename
    
    resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
    [staging : production]
    
    [testing : production]
    phpSettings.display_startup_errors = 1
    phpSettings.display_errors = 1
    
    [development : production]
    phpSettings.display_startup_errors = 1
    phpSettings.display_errors = 1
    resources.frontController.params.displayExceptions = 1
    

    it looks like alot of the stuff in your index.php should be either in your application.ini or in your bootstrap.php. I think that's why your autoloader doesn't work.

    //bootstrap.php
    
    <?php
    
    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {
    
    }
    

    help yourself and run through a couple of tutorials that will help you to setup a ZF app in a very few minutes.
    Zend Framework Quickstart
    Rob Allen's Zf 1.11 tutorial
    Together you can get through these in an hour or two but they will provide a solid basis for ZF setup and basic functions.