【问题标题】:zend form giving errorzend 形式给出错误
【发布时间】:2023-04-02 12:05:01
【问题描述】:

我是 ZF 的新手。我已经制作了一个基本上制作表单的函数,这是代码

 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;
}

这是错误

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

第 68 行指的是这一行

 $username = new Zend_Form_Element_Text('username');

进一步我如何向表单中的每个元素添加 css 类,以及如何添加我自己的错误消息。请使用一个元素并添加自定义错误消息和 CSS 类。谢谢大家。 已编辑这是我的 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');
 ?>

【问题讨论】:

  • 您是在尝试将 Zend Framework 用作 Zend Framework mvc 应用程序,还是在尝试使用 Zend Framework 的零碎部分?还有你用的是什么版本?
  • @RockyFord 先生,我想将 Zend Framework 作为一个 mvc 应用程序。我启动了我的应用程序,但由于我是 ZF 的新手,所以我不知道。但我的应用程序真的很大
  • 您使用的是哪个版本的 Zend Framework?
  • @RockyFord Zend 框架 1.11 版

标签: zend-framework zend-form zend-form-element


【解决方案1】:

这是 Zend_Tool 1.11 版创建的标准 index.php 文件的样子:

// 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();

这连同 application.ini 都是使 Zend Framework 工作所必需的。

//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

看起来你的 index.php 中的很多东西应该在你的 application.ini 或 bootstrap.php 中。我认为这就是您的自动加载器无法工作的原因。

//bootstrap.php

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

}

帮助自己并完成一些教程,这些教程将帮助您在几分钟内设置 ZF 应用程序。
Zend Framework Quickstart
Rob Allen's Zf 1.11 tutorial
您可以在一两个小时内一起完成这些工作,但它们将为 ZF 设置和基本功能提供坚实的基础。

【讨论】:

  • 我没有 bootstrap.php。我应该添加它先生吗??
  • application.ini 放在哪里??
  • @xainee khan 将您的 application.ini 放在 /application/configs 并在 /application 创建 bootstrap.php。我更新了答案以添加引导程序和链接。
【解决方案2】:

由于您的错误发生在 'application\controllers\LoginController.php' 中,看起来您正在运行 Zend 应用程序。如果是这样,您永远不需要调用 require() 或 require_once()。 Zend 自动加载器会为你做这件事。

您的代码应该可以正常工作。您是否将框架正确设置到包含路径中?在创建应用程序对象之前,您的 index.php(您的 Web 根目录中的真实文件)应该调用 set_include_path({zend's library here})。

【讨论】:

  • 我正在编辑我的问题并发布我的 index.php,请查看它是否有效
【解决方案3】:

进一步我如何为表单中的每个元素添加 css 类 和

当显示表单元素时,它们将具有 id,并且类将反映所需的设置。例如这是一个股票 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>

这种行为可以通过使用装饰器来改变,但我会让其他人解决这个问题。

还有如何添加我自己的错误消息。请取一个元素并添加 自定义错误消息和 css 类。谢谢大家。

大多数 Zend_Validation 类允许您指定客户消息。

另一个例子:

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

    • 这行得通,但我应该为每个元素包括每个类吗?我相信会有办法摆脱这个?任何 1 plz 帮助我被卡住了,因为我说我是 ZF 的新手.plz 帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多