Report abuse

//******* my custom helper for a dayOfBirth select element
<?php
/*
 *     This file was created by Creative Web Group Syria , Developement Team
 *     website : http://www.creativewebgroup-sy.com
 *     email : info@creativewebgroup-sy.com
*/

/**
 *
 * @author Creative Web Group Syria
 */
class Msrofi_View_Helper_BirthElement
    extends Zend_View_Helper_FormElement
{
    protected $html = '';

    public function birthElement($name, $value = null, $attribs = null , $options = null)
    {
        $day = $month = $year = '';

        if (is_array($value)){
                $day = (isset($value['day'])) ? $value['day'] : '';
                $month = (isset($value['month'])) ? $value['month'] : '';
                $year = (isset($value['year'])) ? $value['year'] : '';
        }else{
            list($year, $month , $day) = split('-',$value);
        }

        $helper = new Zend_View_Helper_FormSelect();
        $helper->setView($this->view);

        $this->html .= $helper->formSelect($name . '[day]', $day , null, $options['day']);
        $this->html .= ' / ' .$helper->formSelect($name . '[month]', $month , null, $options['month']);
        $this->html .= ' / ' .$helper->formSelect($name . '[year]', $year , null, $options['year']);

        return $this->html;
    }

}
?>
//********* my extended form element 
<?php
class Msrofi_Form_Element_Birth extends Zend_Form_Element_Multi {

    public $helper = 'birthElement';

    protected $_dateFormat = '%year%-%month%-%day%';
    protected $_day = null;
    protected $_month = null;
    protected $_year = null;

    public function setDay($num)
    {
        $this->_day = $num;
        return $this;
    }

    public function setMonth($num)
    {
        $this->_month =  $num;
        return $this;
    }

    public function setYear($num)
    {
        $this->_year = $num;
        return $this;
    }

    public function setValue($value)
    {
        if (is_array($value)
                      && (isset($value['day'])
                          && isset($value['month'])
                          && isset($value['year'])
                      )
            ) {
            $this->setYear($value['year'])
                    ->setMonth($value['month'])
                    ->setDay($value['day']);

            $value = str_replace(
                array('%year%', '%month%', '%day%'),
                array($this->_year, $this->_month, $this->_day),
                $this->_dateFormat
            );

        }else {
            throw new Exception('Invalid date value provided');
        }

        return parent::setValue($value);
    }

    public function getValue()
    {
        if(!isset($this->_year) || !isset($this->_month) || !isset($this->_day)){

            $data = false;

        }else{

            $data= str_replace(
                array('%year%', '%month%', '%day%'),
                array($this->_year, $this->_month, $this->_day),
                $this->_dateFormat
            );
        }
        return $data ;
    }
}
?>
/*************
remember to add 
$view->addHelperPath('Msrofi/View/Helper','Msrofi_View_Helper');
to your view helper.

now you can create your element like this :
        $dateOfBirth = new Msrofi_Form_Element_Birth('dateOfBirth');
        $dateOfBirth->setLabel('Date Of Birth')
                ->addValidator('NotEmpty')
                //->addValidator(new Zend_Validate_Date('Y-n-j')) // see the note down
                ->addValidator(new Zend_Validate_Date(array('format' => 'Y-n-j'))) // or this which is in the new ZF code[1] http://bit.ly/asRHOM
                ->setRequired(true);
plus you can add the values like any select element 
using setMultiOptions function ..
************/
/***********
use this modified Zend_Validate_Date to validate your date
http://www.oeic.net/oeic/2007/09/06/date-validation-in-zend-framework/
************/
/*************
[1] thanks http://twitter.com/elazar for pointing me to the new code
*************/