如何访问自定义 Zend 表单元素?

发布于 2024-11-25 06:34:19 字数 817 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心清如水 2024-12-02 06:34:19

试试这个男士

 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 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;
       }
淡忘如思 2024-12-02 06:34:19

尝试检查一下里面的表格。可能是你调用的时候搞错了。
像:

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

与往事干杯 2024-12-02 06:34:19

如果您在全局配置文件中仅使用命名空间设置自动加载器,则应该能够避免任何复杂的自定义代码来进行设置。在我的 application.ini 中,我有以下内容:

appnamespace = "Application"

ZF 的资源加载器在 APPLICATION_PATH/forms 中有一个默认的表单位置。因此,根据上述内容,我的表单类名称以 Application_ 开头。要使用自定义表单元素,您可以创建 APPLICATION_PATH/forms/Element/Phone.php,并使用类名 Application_Form_Element_Phone。我刚刚尝试过这个,效果很好。如果类名中的 Application 前缀太长,您可以将其替换为较短的名称,例如 AppMy

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.

乖乖 2024-12-02 06:34:19

如前所述,您必须向自动加载器注册空名称空间。为此,您必须使用 Zend_Loader_Autoloader_Resource。您应该将其添加到应用程序 Bootstrap 中。注意:@user854029 已经提到了大部分内容,但忘记了 Form_Element_ 命名空间。

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

}

进一步说明,请考虑将自定义类移动到应用程序的library 目录。

编辑

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文