如何访问自定义 Zend 表单元素?
我在 application/forms/elements 目录中创建了一个 Phone.php 文件。类签名如下: 类 Form_Element_Phone extends Zend_Form_Element_Xhtml
在我的 Bootstrap 中,我有以下内容:
$autoLoader = new Zend_Application_Module_Autoloader( array(
'namespace' => '',
'basePath' => APPLICATION_PATH ) );
return $autoLoader;
我认为当我在应用程序的表单对象中键入 $phone = new Form_Element_Phone( 'phone' );
时,这会自动加载自定义表单元素/forms 目录。 为什么这不起作用?由于 Bootstrap 文件中的代码,难道应用程序目录下的所有内容都不能以这种方式访问吗???我收到致命错误:未找到“Form_Element_Phone”类
错误。
我还在表单类的 init 函数中尝试了 $this->addElementPrefixPath('Form_Element', APPLICATION_PATH . '/forms/elements');
。但这并没有改变任何事情。我做错了什么?我预先感谢您的协助。
I created a Phone.php file in inside application/forms/elements directory. The class signature is as follows:class Form_Element_Phone
extends Zend_Form_Element_Xhtml
In my Bootstrap I have the following:
$autoLoader = new Zend_Application_Module_Autoloader( array(
'namespace' => '',
'basePath' => APPLICATION_PATH ) );
return $autoLoader;
I thought this would autoload the custom form element when I type $phone = new Form_Element_Phone( 'phone' );
in my form object in application/forms directory.
Why did this not work? Shouldn't everything under the application directory be accessible in this manner because of the code in the Bootstrap file??? I am getting Fatal error: Class 'Form_Element_Phone' not found
error.
I also tried $this->addElementPrefixPath('Form_Element', APPLICATION_PATH . '/forms/elements');
in the init function of my form class. But it did not change anything. What am I doing wrong? I thank you in advance for your assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个男士
Try this Men
尝试检查一下里面的表格。可能是你调用的时候搞错了。
像:
class Form_SomeForm extends Zend_Form
检查一下
当您索要表格时,请检查控制器
Try to check you form inside. Maybe you did mistake when call it.
Like:
class Form_SomeForm extends Zend_Form
Check it
And Check in Controller when you ask for the form
如果您在全局配置文件中仅使用命名空间设置自动加载器,则应该能够避免任何复杂的自定义代码来进行设置。在我的
application.ini
中,我有以下内容:ZF 的资源加载器在
APPLICATION_PATH/forms
中有一个默认的表单位置。因此,根据上述内容,我的表单类名称以Application_
开头。要使用自定义表单元素,您可以创建APPLICATION_PATH/forms/Element/Phone.php
,并使用类名Application_Form_Element_Phone
。我刚刚尝试过这个,效果很好。如果类名中的Application
前缀太长,您可以将其替换为较短的名称,例如App
或My
。If you set up your autoloader in your global config file with just a namespace, you should be able to avoid any complicated custom code to set this up. In my
application.ini
, I have the following:ZF's resource loader has a default location for forms in
APPLICATION_PATH/forms
. So with the above, my form class names start withApplication_
. To use a custom form element, you could createAPPLICATION_PATH/forms/Element/Phone.php
, and use the class nameApplication_Form_Element_Phone
. I tried this just now and it works great. If theApplication
prefix on your class names is too long, you could replace it with something shorter, likeApp
orMy
.如前所述,您必须向自动加载器注册空名称空间。为此,您必须使用
Zend_Loader_Autoloader_Resource
。您应该将其添加到应用程序Bootstrap
中。注意:@user854029 已经提到了大部分内容,但忘记了Form_Element_
命名空间。进一步说明,请考虑将自定义类移动到应用程序的
library
目录。编辑
As mentioned, you will have to register the empty namespace with the Autoloader. To do this, you will have to use
Zend_Loader_Autoloader_Resource
. You should add this to the applicationBootstrap
. Note: most of this was already mentioned by @user854029, but forgot theForm_Element_
namespace.On a further note, consider moving custom class to your Application's
library
directory.EDIT