views:

124

answers:

2

When creating form elements with Zend (using Zend Studio for Eclipse), I'd like some auto completion or hints. 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). Great, I select it.

  • but when I try to set the $type I don't get any hints like DateTextBox or ValidationTextBox. 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.

    // Date field

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

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

    );

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

+1  A: 

That's not possible. It's not how autocompletion works. The hints you get are taken directly from ZF's code documentation. Nothing more, nothing less. Everything you see as hints is taken directly from the DocBlock and method signature, e.g.

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

The only place where I know something like what you describe exists is for CSS files. For some reason, when I type in display: it will give me an autocomplete box with possible values for this declaration. If you want more sophisticated autocomplete like this, consider filing this as a feature request to Zend.

Gordon
@jblue I dont know how hard this would be.
Gordon
@jblue you have to login to Zend.com. If your support subscription is still valid, you can open a ticket. Otherwise you just ask in their forum somewhere.
Gordon
+1  A: 

You have few options to help yourself, without waiting for any plugin:

  • learn it and remember ;)
  • extend your phpDoc blocks with all available options:

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):

/**
 * [...]
 * @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

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

Example: (type ZFE_OPTIONS and IDE should show hint with some constants to use as array keys)

/**
 * 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();
dev-null-dweller
Overwriting ZF's DocBlocks is pointless, because you will be losing the changes each time you update the library. The remainder feels like bloat to me, especially the constants approach.
Gordon
When updating library, that's a possibility that function parameters will change, so it's only natural to be careful with new versions ;). I also don't like constants, but they have two major advantages: available everywhere and can be auto-completed. http://www.php.net/manual/en/function.curl-setopt.php inspired me to use them like this
dev-null-dweller