开发者

Get database field for custom select form field in Zend

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?'
));
开发者_如何学JAVA

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?


From what I can see, in your Zend_View_Helper_FormDate::_build($value, $label, $selected) you have 3rd param that is used to add selected="selected" to your HTML but inside your formDate method your only passing the first 2 arguments ie. $this->_build('MM', 'MM');

You will have to pass the value in the 3rd param. Also, -10 pts for using Zend namespace for custom elements. That's a big no-no. :P

EDIT

Replace In Helper:

$value = array_map('strval', (array) $value);
//Zend_Debug::dump($value);
$olddata = explode("-",$value[0]);  
//Zend_Debug::dump($olddata);

With:

if ( !empty($value)) {
    $value = array_map('strval', (array) $value);
    $olddata = explode("-",$value[0]);
} else {
    $olddata = array('','','');
}

Then

protected function _build($value, $label, $selected)

With:

protected function _build($value, $label, $selected = null)

Add to your custom element:

public function setValue($value)
{
    /** Assuming format Y-m-d and need m-d-Y**/
    list($year,$month,$day) = explode('-', $value);
    $this->data = array($month,$day,$year);
    // Almost forgot
    return $this;
}

Note: Was only poking fun at you about the Zend namespace.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜