简体   繁体   English

Zend表单元素的自动完成

[英]Auto-completion for Zend Form Elements

When creating form elements with Zend (using Zend Studio for Eclipse), I'd like some auto completion or hints. 使用Zend创建表单元素时(使用Zend Studio for Eclipse),我需要一些自动完成或提示。 Here's what I'm thinking. 这就是我在想的。 I'm sure these exist, but I don't know how to get them. 我确定它们存在,但是我不知道如何获得它们。

  • I type createElement and auto-completes gives me the signature createElement($type, $name) . 我输入createElement ,自动完成功能为我提供了签名createElement($type, $name) Great, I select it. 太好了,我选择了它。

  • but when I try to set the $type I don't get any hints like DateTextBox or ValidationTextBox . 但是当我尝试设置$type ,没有得到类似DateTextBoxValidationTextBox提示。 Being new, I see how this can be useful. 我是新手,我知道这有什么用。 What do you do to remember all the options? 您如何记住所有选项?

  • for the array of attributes like require , invalidMessage , I'd like to get a list of those to choose from, and/or auto-complete when I start typing one. 对于诸如requireinvalidMessage类的属性array ,我想获取要选择的属性的列表,和/或在我开始键入属性时自动完成。

    // Date field

    $date = $this->createElement('DateTextBox', 'date',

    array('require' => 'true', 'invalidMessage' => 'Invalid date format')

    );

    $date->setLabel('date')->setRequired(true);

That's not possible. 那是不可能的。 It's not how autocompletion works. 它不是自动补全的工作原理。 The hints you get are taken directly from ZF's code documentation. 您得到的提示直接来自ZF的代码文档。 Nothing more, nothing less. 没有更多,没有更少。 Everything you see as hints is taken directly from the DocBlock and method signature, eg 您看到的所有提示均直接来自DocBlock和方法签名,例如

   /**
     * Create an element
     *
     * Acts as a factory for creating elements. Elements created with this
     * method will not be attached to the form, but will contain element
     * settings as specified in the form object (including plugin loader
     * prefix paths, default decorators, etc.).
     *
     * @param  string $type
     * @param  string $name
     * @param  array|Zend_Config $options
     * @return Zend_Form_Element
     */
    public function createElement($type, $name, $options = null)

Eclipse can tell you to insert a string or an array and it will know that the method returns a Zend_Form_Element , but it cannot tell you what these strings should be. Eclipse可以告诉您插入一个字符串或一个数组,并且它将知道该方法返回Zend_Form_Element ,但是它不能告诉您这些字符串应该是什么。

The only place where I know something like what you describe exists is for CSS files. 我唯一知道类似您所描述的内容的地方是CSS文件。 For some reason, when I type in display: it will give me an autocomplete box with possible values for this declaration. 由于某种原因,当我键入display:它会给我一个自动完成框,其中包含此声明的可能值。 If you want more sophisticated autocomplete like this, consider filing this as a feature request to Zend. 如果您想要这样更复杂的自动完成功能,请考虑将其作为功能请求提交给Zend。

You have few options to help yourself, without waiting for any plugin: 您无需等待任何插件就可以选择一些帮助自己的方法:

  • learn it and remember ;) 学习并记住;)
  • extend your phpDoc blocks with all available options: 使用所有可用选项扩展您的phpDoc块:

Example (to be honest I don't know if Eclipse supports html in phpDoc or even any text after variable name in @param , but it works fine in Netbeans): 示例(老实说,我不知道Eclipse是否支持phpDoc中的html甚至@param中变量名之后的任何文本,但在Netbeans中它可以正常工作):

/**
 * [...]
 * @param  string $type Can be: <ul><li>DateTextBox</li><li>ValidationTextBox</li></ul>
 * @param  string $name Whatever
 * @param  array|Zend_Config $options Array with following keys: <ul><li>require</li><li>invalidMessage</li></ul>
 * @return Zend_Form_Element
 */
public function createElement($type, $name, $options = null)
  • extend Zend class and create your own methods to simplify your work 扩展Zend类并创建自己的方法以简化工作

Example: 例:

class My_Zend_Form_Element extends Zend_Form_Element    
{   
    public function createDateTextBox($name, $options = null)
    {
        return $this->createElement('DateTextBox', $name, $options);
    }
}
  • declare some well named constants and provide some hint in phpDoc 声明一些命名良好的常量,并在phpDoc中提供一些提示

Example: (type ZFE_OPTIONS and IDE should show hint with some constants to use as array keys) 示例:(类型ZFE_OPTIONS和IDE应该显示带有一些常量的提示,以用作数组键)

/**
 * Can be true or false
 */
define('ZFE_OPTIONS_REQUIRE','require');
  • create your own helper classes with methods to produce valid options array 创建带有方法的自己的帮助器类,以产生有效的选项数组

Example: 例:

class ZFE_Options
{
    protected $opts = array();

    /**
     * @param bool $req
     * @return ZFE_Options 
     */
    public function setRequired($req){
        $this->opts['require'] = (bool)$req;
        return $this;
    }

    /**
     * @param string $txt
     * @return ZFE_Options 
     */
    public function setInvalidMessage($txt){
        $this->opts['invalidMessage'] = (string)$txt;
        return $this;
    }

    /**
     * @return array
     */
    public function toArray(){
        return $this->opts;
    }
}

$zfe_options = new ZFE_Options();
$opts = $zfe_options
            ->setRequired(true)
            ->setInvalidMessage('Please provide valid email address')
            ->toArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM