Zend_Form_Element: create your element

0. Intro.

the development process, often need to use different custom selects, input, zagruzki files and so on. In this case, it is necessary to write additional handlers on the client side, as the ZF out of the box knows nothing besides standard form elements (captcha is an exception). In this article we will create a element based on facebook-like TextboxList plugin for jQuery that looks like this:

habr 1

1. Theory.

Actually, that would create a new item, you need to observe two conditions:
    the
  1. element class that inherit from Zend_Form_Element_Xhtml;
  2. the
  3. view helper that will be responsible for rendering;
  4. the
  5. element name must be in our namespace (in this case Eve_) and it should be setup correctly.

2. Practice.

2.1. Create Eve_Form_Element_TextboxList
the class Structure consists of the following main parts:
    the
  1. specify the view helper, which trendedit element;
  2. the
  3. set default options;
  4. the
  5. overload method to set (or combination) of options (optional)
  6. the
  7. other options\methods at this stage not needed, and can see into the Zend_Form_Element.
the Main objectives of the class element is the control validators\decorators\filters and treatment options with their subsequent transfer to the view helper.
The source class in listing with comments:
the
 
class Eve_Form_Element_TextboxList extends Zend_Form_Element_Xhtml 
{ 
/** 
* The name of the helper 
* @var string 
*/ 
public $helper = 'formTextboxList'; 

/** 
* Options by default 
* @var array 
*/ 
public $options = array( 
'js_main' => '/js/jquery.textboxlist.js', // path to the main plugin script 
'js_autocomplete' => '/js/jquery.textboxlist.autocomplete.js', // path to the plugin script to the plugin (support tips) 
'js_growinginput' => '/js/jquery.growinginput.js', // dependence of the script 
'use_autocompletion' = > '0', // use hints or not 
'autocomplete_script' => null, // backend, how to take tips 
'css_main' => '/css/textboxlist.css', // styles 
'css_autocomplete' => '/css/textboxlist.autocomplete.css', 
); 

/** 
* Calculates only the user-specified options and sets them 
* 
* @param array $options 
* @return Eve_Form_Element_TextboxList 
*/ 
public function setOptions(array $options) 
{ 
/** 
* Unlike the framework, this example selects only options that were specified 
* at the stage of the description of element 
*/ 
$diff = array_intersect_key($options, $this->options); 
$this->options = array_merge($this->options, $diff); 

/** 
* then all the custom options you want to remove, otherwise they will be in html as attributes of the element 
*/ 
foreach ($diff as $key => $option) { 
unset ($options[$key]); 
} 

parent::setOptions($options); 
return $this; 
} 

} 

2.2. Create a view helper Eve_View_Helper_FormTextboxList
the Class must have a method matching the name of the class that will be called automatically when you access the assistant.
The code in listing + comments:
 
class Eve_View_Helper_FormTextboxList extends Zend_View_Helper_FormElement 
{ 
/** 
* Generates a 'textboxList' element. 
* 
* @access public 
* 
* @param string|array $name If a string, the element name. If an 
* array, all other parameters are ignored, and the array elements 
* are extracted in  place  of added parameters. 
* 
* @param  mixed  $value The element value. 
* 
* @param array $attribs Attributes for the element tag. 
* 
* @return string The element XHTML. 
*/ 
public function formTextboxList($name, $value = ", $attribs = null, $options = null) 
{ 
$id = $name; 
$elemId = $this->view->escape($id); 

$xhtml = '<input type="text" name="' . $this->view->escape($name) . '" id="' . $this->view->escape($id) . '"'; 
// setting default values 
if (!empty($value)) { 
$xhtml .= 'value="' . $this->view->escape($value) . '"'; 
} 

// assigning attributes already to the html element 
$xhtml .= $this->_htmlAttribs($attribs); 
$xhtml .= '/>' . PHP_EOL; 

/** 
* It's worth noting that the code needs to be addition, because if the page will 
* more than one element, then there is no need to re-initialize. 
*/ 
// add content and end tag 
$xhtml .= $content . ($this->view->doctype()->isXhtml() ? '/>' : '>') . PHP_EOL; 

$this->view->headScript- > appendFile($options['js_growinginput']); 
$this->view->headScript- > appendFile($options['js_main']); 


$this->view->headLink- > appendStylesheet($options['css_main']); 
$this->view->headLink- > appendStylesheet($options['css_autocomplete']); 

$xhtml .= '<script type="text/javascript"> 
var tl_' . $elemId . '= new $.TextboxList("#' 
. $elemId. '", {unique: true, plugins: {autocomplete: {}}}); 
'; 

if ((int) $options['use_autocompletion'] == 1) { 
if (!$options['autocomplete_script']) { 
throw new Zend_View_Exception('No autocompletion backend is set for' . __CLASS__ . ' plugin.'); 
} else { 
$.getJSON('" . $options['autocomplete_script'] . "', null, function (data) { 
tl.plugins['autocomplete'].setValues(data); 
tl.getContainer().removeClass('textboxlist-loading'); 
});"; 
} 
} 
$xhtml .= '</script>'; 
return $xhtml; 
} 

} 

2.3. Initializes using the config.
It is no different from usual:
 
elements.categories.type = textboxList 
elements.categories.options.label = Categories 
elements.categories.options.use_autocompletion = 1 
elements.categories.options.autocomplete_script = /categories/ajax/get-all/ 

3. Outro.

Thus, was created as a separate element, which draws a completely new type inputo.
Jquery plugin can be found over there http://devthought.com/projects/jquery/textboxlist/.
The plugin has enough options that were not implemented under this component.And Yes, the plugin is not under a free license.

UPD:1. updated (see this comment
Article based on information from habrahabr.ru

Comments

Popular posts from this blog

Powershell and Cyrillic in the console (updated)

Active/Passive PostgreSQL Cluster, using Pacemaker, Corosync

Automatic deployment ElasticBeanstalk using Bitbucket Pipelines