I have a form that works. I created a new form element that is a multi-select field for date. It combines day, month, and year into one form element like so:
Form Item:
$this->addElement('date', 'new_date', array(
'label' => 'Expires as New on?'
));
Form Element:
<?php
require_once 'Zend/Form/Element/Multi.php';
class Zend_Form_Element_Date extends Zend_Form_Element_Multi
{
public $helper = 'formDate';
protected $_registerInArrayValidator = false;
private $data;
public function isValid ($data) {
$this->data=$data;
return true;
}
public function getValue () {
return $this->data[2].'-'.$this->data[0].'-'.$this->data[1];
}
}
Helper Element:
<?php
require_once 'Zend/View/Helper/FormElement.php';
class Zend_View_Helper_FormDate extends Zend_View_Helper_FormElement
{
public function formDate($name, $value = null, $attribs = null,
$options = null, $listsep = "<br />\n")
{
$info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
extract($info); // name, id, value, attribs, options, listsep, disable
// force $value to array so we can compare multiple values
// to multiple options.
$value = array_map('strval', (array) $value);
//Zend_Debug::dump($value);
$olddata = explode("-",$value[0]);
//Zend_Debug::dump($olddata);
// check if element may have multiple values
$multiple = '';
if (substr($name, -2) == '[]') {
// multiple implied by the name
$multiple = ' multiple="multiple"';
}
if (isset($attribs['multiple'])) {
// Attribute set
if ($attribs['multiple']) {
// True attribute; set multiple attribute
$multiple = ' multiple="multiple"';
// Make sure name indicates multiple values are allowed
if (!empty($multiple) && (substr($name, -2) != '[]')) {
$name .= '[]';
}
} else {
// False attribute; ensure attribute not set
$multiple = '';
}
unset($attribs['multiple']);
}
// now start building the XHTML.
$disabled = '';
if (true === $disable) {
$disabled = ' disabled="disabled"';
}
// Build the surrounding select element first.
$xhtml = '<select'
. ' name="' . $this->view->escape($name) . '[]"'
. ' id="' . $this->view->escape($id) . '"'
. $multiple
. $disabled
. $this->_htmlAttribs($attribs)
. ">\n ";
// build the list of options
// month
$month = array();
$month[] = $this->_build('MM', 'MM');
for($i = 1; $i<= 12; $i++){
$m = date("M", mktime(0,0,0,$i));
$month[] = $this->_build($i<10?'0'.$i:$i, $m, $olddata[1]);
}
$day = array();
$day[] = $this->_build('DD', 'DD');
for($i = 1; $i<=31; $i++){
$day[] = $this->_build($i<10?'0'.$i:$i, $i<10?'0'.$i:$i, $olddata[2]);
}
$year = array();
$year[] = $this->_build('YYYY','YYYY');
for($i = (int)date("Y"); $i < (int)date("Y")+11; $i++){
$year[] = $this->_build($i, $i, $olddata[0]);
}
// add the options to the xhtml and close the select
$month = $xhtml.implode("\n ", $month) . "\n</select>";
$day = $xhtml.implode("\n ", $day) . "\n</select>";
$year = $xhtml.implode("\n ", $year) . "\n</select>";
return $month.$day.$year;
}
protected function _build($value, $label, $selected)
{
$opt = '<option'
. ' value="' . $this->view->escape($value) . '"'
. ' label="' . $this->view->escape($label) . '"';
if ($value == $selected) {
$opt .= ' selected="selected"';
}
$opt .= '>' . $this->view->escape($label). "</option>";
return $opt;
}
}
I have been able to save the data from this form element into the database. Yet when I attempt to load the form, all form elements receive the database values except my custom form element. Can anyone direct me to what I am doing wrong?