Fawad Ghafoor
Fawad Ghafoor

Reputation: 6207

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');
 ?>

Upvotes: 0

Views: 685

Answers (4)

RockyFord
RockyFord

Reputation: 8519

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.

Upvotes: 1

RockyFord
RockyFord

Reputation: 8519

Further how can i add css classes to each and every element in my form and

when the form elements are displayed they will have id's and the class will reflect the required setting. For example this a stock Zend_Form_Element_Text:

<dt id="name-label"><label for="name" class="required">Page Name:</label></dt>
<dd id="name-element">
<input type="text" name="name" id="name" value="" size="40"></dd>
<dt id="headline-label"><label for="headline" class="required">Headline:</label></dt>

This behavior can be alter through the use of decorators, but I'll let someone else tackle that topic.

also how to add my own error messages.plz take one element and add custom error messages and css class.Thanking you all.

most Zend_Validation classes allow you to specify custome messages.

Another Example:

$id = new Zend_Form_Element_Text('id');
        $id->setLabel('Employee Number:')
                 ->setOptions(array('size' => '6'))
                 ->setRequired(TRUE)
                 ->addValidator('regex', TRUE, array(
                     'pattern' => '/^[0-9]{6}$/',
                     'messages' => array(
                         Zend_Validate_Regex::INVALID => '\'%value\' is not a valid Employee Number.',
                         Zend_Validate_Regex::NOT_MATCH => '\'%value\' does not match, requires a 6 digit Employee Number'
                         )))
                 ->addFilter('Digits')
                 ->addFilter('HtmlEntities')
                 ->addFilter('StringTrim')
                 ->removeDecorator('HtmlTag');

Upvotes: 1

Seth Battin
Seth Battin

Reputation: 2851

Since your error is happening in 'application\controllers\LoginController.php', it would seem that you are you running a Zend application. If so, you should never need to call require() or require_once(). The zend autoloader will do it for you.

Your code should work as it is. Are you correctly setting the framework into your include path? Your index.php (the real one in your web root) should call set_include_path({zend's library here}) before creating your application object.

Upvotes: 1

Fawad Ghafoor
Fawad Ghafoor

Reputation: 6207

require_once 'Zend/Form/Element/Text.php';

Upvotes: 0

Related Questions