Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//******* 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
*************/