How to access custom Zend Form Element?
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-&g开发者_如何学JAVAt;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.
Try this Men
public function _initAutoload()
{
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' =>APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'form' => array(
'path' => 'forms/',
'namespace' => 'Form_',
),
'model' => array(
'path' => 'models/',
'namespace' => 'Model_'
),
'validator' =>array(
'path' => 'validators/',
'namespace' => 'Validator_'
),
'plugin' => array(
'path' => 'plugins/',
'namespace' => 'Plugin_'
),
'helper' => array(
'path' => 'helpers/',
'namespace' => 'Helper_'
),
),
));
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH.'/modules/default' ));
return $modelLoader;
return $resourceLoader;
}
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
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:
appnamespace = "Application"
ZF's resource loader has a default location for forms in APPLICATION_PATH/forms
. So with the above, my form class names start with Application_
. To use a custom form element, you could create APPLICATION_PATH/forms/Element/Phone.php
, and use the class name Application_Form_Element_Phone
. I tried this just now and it works great. If the Application
prefix on your class names is too long, you could replace it with something shorter, like App
or My
.
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 application Bootstrap
. Note: most of this was already mentioned by @user854029, but forgot the Form_Element_
namespace.
protected _initAutoload()
{
// the __construct of this class registers this resource with Zend_Loader_Autoloader
new Zend_Loader_Autoloader_Resource(array(
// This base path prepends paths defined in the resourceTypes below
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'form' => array(
'path' => 'forms/',
'namespace' => 'Form_'
),
// the key 'element' is an arbitrary name I used, it's not special
'element' => array(
// Now we set the path we need to append to basePath set above
'path' => 'forms/elements',
// And now we have to declare the namespace
'namespace' => 'Form_Element_'
),
'model' => array(
'path' => 'models/',
'namespace' => 'Model_'
)
/** You can add the rest here as need **/
)
));
// Note: you don't have to return anything
}
On a further note, consider moving custom class to your Application's library
directory.
EDIT
protected _initAutoload()
{
//Removed Autoloader_Resoure and Replaced with Module_Autoloader
new Zend_Application_Module_Autoloader(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'element' => array(
'path' => 'forms/elements', // This is custom
'namespace' => 'Form_Element'
)
)
));
}
精彩评论