Skip to content

Instantly share code, notes, and snippets.

@ixti
Created May 15, 2010 10:44
Show Gist options
  • Save ixti/402131 to your computer and use it in GitHub Desktop.
Save ixti/402131 to your computer and use it in GitHub Desktop.
<?php
// form initialization
$form->addElement('zipAndTheCity', 'origin');
// some work with form etc.
$zip = $form->getElement('origin')->getValue('zip');
$city = $form->getElement('origin')->getValue('city');
?>
<?php
/** Zend_Form_Element_Xhtml */
require_once 'Zend/Form/Element/Xhtml.php';
class My_Form_Element_ZipAndTheCity extends Zend_Form_Element_Xhtml
{
public $helper = 'zipAndTheCity';
protected $_isArray = true;
public function getValue($key = null)
{
$value = parent::getValue();
if (!is_array($value)) {
$value = array(null, null);
} else {
// make sure array will has both values
while (2 > count($value)) {
$value[] = null;
}
}
// add aliases
$value['zip'] = $value[0];
$value['city'] = $value[1];
if (array_key_exists($key, $value)) {
return $value[$key];
}
return $value;
}
}
<?php
/** Zend_View_Helper_FormElement */
require_once 'Zend/View/Helper/FormElement.php';
class My_View_Helper_ZipAndTheCity extends Zend_View_Helper_FormElement
{
public function zipAndTheCity($name, $value = array(), $attribs = null)
{
$info = $this->_getInfo($name, $value, $attribs);
extract($info);
if (!is_array($value)) {
$value = array(null, null);
} else {
// make sure array will has both values
while (2 > count($value)) {
$value[] = null;
}
}
// build the element
$disabled = '';
if ($disable) {
// disabled
$disabled = ' disabled="disabled"';
}
// XHTML or HTML end tag?
$endTag = ' />';
if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
$endTag= '>';
}
$xhtml = '<input type="text"'
. ' name="' . $this->view->escape($name) . '"'
. ' id="' . $this->view->escape($id . '-zip') . '"'
. ' value="' . $this->view->escape($value[0]) . '"'
. $disabled
. $this->_htmlAttribs($attribs)
. $endTag
. '<input type="text"'
. ' name="' . $this->view->escape($name) . '"'
. ' id="' . $this->view->escape($id . '-city') . '"'
. ' value="' . $this->view->escape($value[1]) . '"'
. $disabled
. $this->_htmlAttribs($attribs)
. $endTag
. '</span>';
return $xhtml;
}
}
@ixti
Copy link
Author

ixti commented May 15, 2010

Hmm... Never thought gist removes paths in filenames :(( So slashes were replaced with underscores in filenames.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment