Skip to content
This repository
Browse code

Merge branch 'form/select-date' of github.com:bakura10/zf2 into form/…

…select-date
  • Loading branch information...
commit c3a7d8c99773a566691c194cb8bb90f19badb79a 2 parents 57c7d09 + d5c62e0
Michaël Gallego authored August 27, 2012
118  library/Zend/Form/Element/DateSelect.php
... ...
@@ -0,0 +1,118 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_Form
  9
+ */
  10
+
  11
+namespace Zend\Form\Element;
  12
+
  13
+use DateTime;
  14
+use Zend\Form\Form;
  15
+use Zend\Validator\ValidatorInterface;
  16
+use Zend\Validator\Date as DateValidator;
  17
+
  18
+class DateSelect extends MonthSelect
  19
+{
  20
+    /**
  21
+     * Select form element that contains values for day
  22
+     *
  23
+     * @var Select
  24
+     */
  25
+    protected $dayElement;
  26
+
  27
+    /**
  28
+     * Constructor. Add the day select element
  29
+     *
  30
+     * @param  null|int|string  $name    Optional name for the element
  31
+     * @param  array            $options Optional options for the element
  32
+     */
  33
+    public function __construct($name = null, $options = array())
  34
+    {
  35
+        parent::__construct($name, $options);
  36
+
  37
+        $this->dayElement = new Select('day');
  38
+    }
  39
+
  40
+    /**
  41
+     * @return Select
  42
+     */
  43
+    public function getDayElement()
  44
+    {
  45
+        return $this->dayElement;
  46
+    }
  47
+
  48
+    /**
  49
+     * @param mixed $value
  50
+     * @return void|\Zend\Form\Element
  51
+     */
  52
+    public function setValue($value)
  53
+    {
  54
+        parent::setValue($value);
  55
+        $this->dayElement->setValue($value['day']);
  56
+    }
  57
+
  58
+    /**
  59
+     * Prepare the form element (mostly used for rendering purposes)
  60
+     *
  61
+     * @param Form $form
  62
+     * @return mixed
  63
+     */
  64
+    public function prepareElement(Form $form)
  65
+    {
  66
+        parent::prepareElement($form);
  67
+
  68
+        $name = $this->getName();
  69
+        $this->dayElement->setName($name . '[day]');
  70
+    }
  71
+
  72
+    /**
  73
+     * Get validator
  74
+     *
  75
+     * @return ValidatorInterface
  76
+     */
  77
+    protected function getValidator()
  78
+    {
  79
+        if (null === $this->validator) {
  80
+            $this->validator = new DateValidator(array('format' => 'Y-m-d'));
  81
+        }
  82
+
  83
+        return $this->validator;
  84
+    }
  85
+
  86
+    /**
  87
+     * Should return an array specification compatible with
  88
+     * {@link Zend\InputFilter\Factory::createInput()}.
  89
+     *
  90
+     * @return array
  91
+     */
  92
+    public function getInputSpecification()
  93
+    {
  94
+        return array(
  95
+            'name' => $this->getName(),
  96
+            'required' => false,
  97
+            'filters' => array(
  98
+                array(
  99
+                    'name'    => 'Callback',
  100
+                    'options' => array(
  101
+                        'callback' => function($date) {
  102
+                            // Convert the date to a specific format
  103
+                            if (is_array($date)) {
  104
+                                $date = $date['year'] . '-' . $date['month'] . '-' . $date['day'];
  105
+                            }
  106
+
  107
+                            return $date;
  108
+                        }
  109
+                    )
  110
+                )
  111
+            ),
  112
+            'validators' => array(
  113
+                $this->getValidator(),
  114
+            )
  115
+        );
  116
+    }
  117
+}
  118
+
213  library/Zend/Form/Element/MonthSelect.php
... ...
@@ -0,0 +1,213 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_Form
  9
+ */
  10
+
  11
+namespace Zend\Form\Element;
  12
+
  13
+use Zend\Form\Element;
  14
+use Zend\Form\ElementPrepareAwareInterface;
  15
+use Zend\Form\Form;
  16
+use Zend\InputFilter\InputProviderInterface;
  17
+use Zend\Validator\ValidatorInterface;
  18
+use Zend\Validator\Regex as RegexValidator;
  19
+
  20
+class MonthSelect extends Element implements InputProviderInterface, ElementPrepareAwareInterface
  21
+{
  22
+    /**
  23
+     * Select form element that contains values for month
  24
+     *
  25
+     * @var Select
  26
+     */
  27
+    protected $monthElement;
  28
+
  29
+    /**
  30
+     * Select form element that contains values for year
  31
+     *
  32
+     * @var Select
  33
+     */
  34
+    protected $yearElement;
  35
+
  36
+    /**
  37
+     * Min year to use for the select (default: current year - 100)
  38
+     *
  39
+     * @var int
  40
+     */
  41
+    protected $minYear;
  42
+
  43
+    /**
  44
+     * Max year to use for the select (default: current year)
  45
+     *
  46
+     * @var int
  47
+     */
  48
+    protected $maxYear;
  49
+
  50
+    /**
  51
+     * @var ValidatorInterface
  52
+     */
  53
+    protected $validator;
  54
+
  55
+
  56
+    /**
  57
+     * Constructor. Add two selects elements
  58
+     *
  59
+     * @param  null|int|string  $name    Optional name for the element
  60
+     * @param  array            $options Optional options for the element
  61
+     */
  62
+    public function __construct($name = null, $options = array())
  63
+    {
  64
+        parent::__construct($name, $options);
  65
+
  66
+        $this->monthElement = new Select('month');
  67
+        $this->yearElement = new Select('year');
  68
+
  69
+        $this->maxYear = date('Y');
  70
+        $this->minYear = $this->maxYear - 100;
  71
+    }
  72
+
  73
+    /**
  74
+     * Accepted options for DateSelect:
  75
+     * - min_year: min year to use in the year select
  76
+     * - max_year: max year to use in the year select
  77
+     *
  78
+     * @param array|\Traversable $options
  79
+     * @return DateSelect
  80
+     */
  81
+    public function setOptions($options)
  82
+    {
  83
+        parent::setOptions($options);
  84
+
  85
+        if (isset($options['min_year'])) {
  86
+            $this->setMinYear($options['min_year']);
  87
+        }
  88
+
  89
+        if (isset($options['max_year'])) {
  90
+            $this->setMaxYear($options['max_year']);
  91
+        }
  92
+
  93
+        return $this;
  94
+    }
  95
+
  96
+    /**
  97
+     * @return Select
  98
+     */
  99
+    public function getMonthElement()
  100
+    {
  101
+        return $this->monthElement;
  102
+    }
  103
+
  104
+    /**
  105
+     * @return Select
  106
+     */
  107
+    public function getYearElement()
  108
+    {
  109
+        return $this->yearElement;
  110
+    }
  111
+
  112
+    /**
  113
+     * @param  int $minYear
  114
+     * @return DateSelect
  115
+     */
  116
+    public function setMinYear($minYear)
  117
+    {
  118
+        $this->minYear = $minYear;
  119
+        return $this;
  120
+    }
  121
+
  122
+    /**
  123
+     * @return int
  124
+     */
  125
+    public function getMinYear()
  126
+    {
  127
+        return $this->minYear;
  128
+    }
  129
+
  130
+    /**
  131
+     * @param  int $maxYear
  132
+     * @return DateSelect
  133
+     */
  134
+    public function setMaxYear($maxYear)
  135
+    {
  136
+        $this->maxYear = $maxYear;
  137
+        return $this;
  138
+    }
  139
+
  140
+    /**
  141
+     * @return int
  142
+     */
  143
+    public function getMaxYear()
  144
+    {
  145
+        return $this->maxYear;
  146
+    }
  147
+
  148
+    /**
  149
+     * @param mixed $value
  150
+     * @return void|\Zend\Form\Element
  151
+     */
  152
+    public function setValue($value)
  153
+    {
  154
+        $this->monthElement->setValue($value['month']);
  155
+        $this->yearElement->setValue($value['year']);
  156
+    }
  157
+
  158
+    /**
  159
+     * Prepare the form element (mostly used for rendering purposes)
  160
+     *
  161
+     * @param Form $form
  162
+     * @return mixed
  163
+     */
  164
+    public function prepareElement(Form $form)
  165
+    {
  166
+        $name = $this->getName();
  167
+        $this->monthElement->setName($name . '[month]');
  168
+        $this->yearElement->setName($name . '[year]');
  169
+    }
  170
+
  171
+    /**
  172
+     * Get validator
  173
+     *
  174
+     * @return ValidatorInterface
  175
+     */
  176
+    protected function getValidator()
  177
+    {
  178
+        return new RegexValidator('/^[0-9]{4}\-(0?[1-9]|1[012])$/');
  179
+    }
  180
+
  181
+    /**
  182
+     * Should return an array specification compatible with
  183
+     * {@link Zend\InputFilter\Factory::createInput()}.
  184
+     *
  185
+     * @return array
  186
+     */
  187
+    public function getInputSpecification()
  188
+    {
  189
+        return array(
  190
+            'name' => $this->getName(),
  191
+            'required' => false,
  192
+            'filters' => array(
  193
+                array(
  194
+                    'name'    => 'Callback',
  195
+                    'options' => array(
  196
+                        'callback' => function($date) {
  197
+                            // Convert the date to a specific format
  198
+                            if (is_array($date)) {
  199
+                                $date = $date['year'] . '-' . $date['month'];
  200
+                            }
  201
+
  202
+                            return $date;
  203
+                        }
  204
+                    )
  205
+                )
  206
+            ),
  207
+            'validators' => array(
  208
+                $this->getValidator(),
  209
+            )
  210
+        );
  211
+    }
  212
+}
  213
+
95  library/Zend/Form/View/Helper/FormDateSelect.php
... ...
@@ -0,0 +1,95 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_Form
  9
+ */
  10
+
  11
+namespace Zend\Form\View\Helper;
  12
+
  13
+use DateTime;
  14
+use IntlDateFormatter;
  15
+use Zend\Form\ElementInterface;
  16
+use Zend\Form\Exception;
  17
+use Zend\Form\View\Helper\FormMonthSelect as FormMonthSelectHelper;
  18
+
  19
+/**
  20
+ * @category   Zend
  21
+ * @package    Zend_Form
  22
+ * @subpackage View
  23
+ */
  24
+class FormDateSelect extends FormMonthSelectHelper
  25
+{
  26
+    /**
  27
+     * Render a date element that is composed of three selects
  28
+     *
  29
+     * @param  ElementInterface $element
  30
+     * @throws Exception\DomainException
  31
+     * @return string
  32
+     */
  33
+    public function render(ElementInterface $element)
  34
+    {
  35
+        $name = $element->getName();
  36
+        if ($name === null || $name === '') {
  37
+            throw new Exception\DomainException(sprintf(
  38
+                '%s requires that the element has an assigned name; none discovered',
  39
+                __METHOD__
  40
+            ));
  41
+        }
  42
+
  43
+        $selectHelper = $this->getSelectElementHelper();
  44
+        $pattern      = $this->parsePattern();
  45
+
  46
+        $daysOptions   = $this->getDaysOptions($pattern['day']);
  47
+        $monthsOptions = $this->getMonthsOptions($pattern['month']);
  48
+        $yearOptions   = $this->getYearsOptions($element->getMinYear(), $element->getMaxYear());
  49
+
  50
+        $dayElement   = $element->getDayElement()->setAttribute('options', $daysOptions);
  51
+        $monthElement = $element->getMonthElement()->setAttribute('options', $monthsOptions);
  52
+        $yearElement  = $element->getYearElement()->setAttribute('options', $yearOptions);
  53
+
  54
+        $markup = array();
  55
+        $markup[$pattern['day']]   = $selectHelper->render($dayElement);
  56
+        $markup[$pattern['month']] = $selectHelper->render($monthElement);
  57
+        $markup[$pattern['year']]  = $selectHelper->render($yearElement);
  58
+
  59
+        $markup = sprintf(
  60
+            '%s %s %s %s %s',
  61
+            $markup[array_shift($pattern)],
  62
+            array_shift($pattern), // Delimiter
  63
+            $markup[array_shift($pattern)],
  64
+            array_shift($pattern), // Delimiter
  65
+            $markup[array_shift($pattern)]
  66
+        );
  67
+
  68
+        return $markup;
  69
+    }
  70
+
  71
+    /**
  72
+     * Create a key => value options for days
  73
+     *
  74
+     * @param string  $pattern Pattern to use for days
  75
+     * @return array
  76
+     */
  77
+    public function getDaysOptions($pattern)
  78
+    {
  79
+        $keyFormatter   = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'dd');
  80
+        $valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
  81
+        $date           = new DateTime('1970-01-01');
  82
+
  83
+        $result = array();
  84
+        for ($day = 1; $day <= 31; $day++) {
  85
+            $key   = $keyFormatter->format($date);
  86
+            $value = $valueFormatter->format($date);
  87
+            $result[$key] = $value;
  88
+
  89
+            $date->modify('+1 day');
  90
+        }
  91
+
  92
+        return $result;
  93
+    }
  94
+}
  95
+
10  library/Zend/Form/View/Helper/FormElement.php
@@ -58,6 +58,16 @@ public function render(ElementInterface $element)
58 58
             return $helper($element);
59 59
         }
60 60
 
  61
+        if ($element instanceof Element\MonthSelect) {
  62
+            $helper = $renderer->plugin('form_month_select');
  63
+            return $helper($element);
  64
+        }
  65
+
  66
+        if ($element instanceof Element\DateSelect) {
  67
+            $helper = $renderer->plugin('form_date_select');
  68
+            return $helper($element);
  69
+        }
  70
+
61 71
         $type    = $element->getAttribute('type');
62 72
         $options = $element->getAttribute('options');
63 73
 
269  library/Zend/Form/View/Helper/FormMonthSelect.php
... ...
@@ -0,0 +1,269 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_Form
  9
+ */
  10
+
  11
+namespace Zend\Form\View\Helper;
  12
+
  13
+use DateTime;
  14
+use IntlDateFormatter;
  15
+use Locale;
  16
+use Zend\Form\ElementInterface;
  17
+use Zend\Form\Exception;
  18
+
  19
+/**
  20
+ * @category   Zend
  21
+ * @package    Zend_Form
  22
+ * @subpackage View
  23
+ */
  24
+class FormMonthSelect extends AbstractHelper
  25
+{
  26
+    /**
  27
+     * FormSelect helper
  28
+     *
  29
+     * @var FormSelect
  30
+     */
  31
+    protected $selectHelper;
  32
+
  33
+    /**
  34
+     * Date formatter to use
  35
+     *
  36
+     * @var int
  37
+     */
  38
+    protected $dateType = IntlDateFormatter::LONG;
  39
+
  40
+    /**
  41
+     * Pattern to use for Date rendering
  42
+     *
  43
+     * @var string
  44
+     */
  45
+    protected $pattern;
  46
+
  47
+    /**
  48
+     * Locale to use
  49
+     *
  50
+     * @var string
  51
+     */
  52
+    protected $locale;
  53
+
  54
+    /**
  55
+     * Render a month element that is composed of two selects
  56
+     *
  57
+     * @param \Zend\Form\ElementInterface $element
  58
+     * @throws \Zend\Form\Exception\DomainException
  59
+     * @return string
  60
+     */
  61
+    public function render(ElementInterface $element)
  62
+    {
  63
+        $name = $element->getName();
  64
+        if ($name === null || $name === '') {
  65
+            throw new Exception\DomainException(sprintf(
  66
+                '%s requires that the element has an assigned name; none discovered',
  67
+                __METHOD__
  68
+            ));
  69
+        }
  70
+
  71
+        $selectHelper = $this->getSelectElementHelper();
  72
+        $pattern      = $this->parsePattern();
  73
+
  74
+        // The pattern always contains "day" part and the first separator, so we have to remove it
  75
+        unset($pattern['day']);
  76
+        unset($pattern[0]);
  77
+
  78
+        $monthsOptions = $this->getMonthsOptions($pattern['month']);
  79
+        $yearOptions   = $this->getYearsOptions($element->getMinYear(), $element->getMaxYear());
  80
+
  81
+        $monthElement = $element->getMonthElement()->setAttribute('options', $monthsOptions);
  82
+        $yearElement  = $element->getYearElement()->setAttribute('options', $yearOptions);
  83
+
  84
+        $markup = array();
  85
+        $markup[$pattern['month']] = $selectHelper->render($monthElement);
  86
+        $markup[$pattern['year']]  = $selectHelper->render($yearElement);
  87
+
  88
+        $markup = sprintf(
  89
+            '%s %s %s',
  90
+            $markup[array_shift($pattern)],
  91
+            array_shift($pattern), // Delimiter
  92
+            $markup[array_shift($pattern)]
  93
+        );
  94
+
  95
+        return $markup;
  96
+    }
  97
+
  98
+    /**
  99
+     * Invoke helper as function
  100
+     *
  101
+     * Proxies to {@link render()}.
  102
+     *
  103
+     * @param \Zend\Form\ElementInterface $element
  104
+     * @param int                         $dateType
  105
+     * @param null|string                 $locale
  106
+     * @return FormDateSelect
  107
+     */
  108
+    public function __invoke(ElementInterface $element = null, $dateType = IntlDateFormatter::LONG, $locale = null)
  109
+    {
  110
+        if (!$element) {
  111
+            return $this;
  112
+        }
  113
+
  114
+        $this->setDateType($dateType);
  115
+
  116
+        if ($locale !== null) {
  117
+            $this->setLocale($locale);
  118
+        }
  119
+
  120
+        return $this->render($element);
  121
+    }
  122
+
  123
+    /**
  124
+     * @return string
  125
+     */
  126
+    public function getPattern()
  127
+    {
  128
+        if ($this->pattern === null) {
  129
+            $intl           = new IntlDateFormatter($this->getLocale(), $this->dateType, IntlDateFormatter::NONE);
  130
+            $this->pattern  = $intl->getPattern();
  131
+        }
  132
+
  133
+        return $this->pattern;
  134
+    }
  135
+
  136
+    /**
  137
+     * Parse the pattern
  138
+     *
  139
+     * @return array
  140
+     */
  141
+    protected function parsePattern()
  142
+    {
  143
+        $pattern    = $this->getPattern();
  144
+        $pregResult = preg_split('/([ -,.\/]+)/', $pattern, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  145
+
  146
+        $result = array();
  147
+        foreach ($pregResult as $value) {
  148
+            if (stripos($value, 'd') !== false) {
  149
+                $result['day'] = $value;
  150
+            } elseif (stripos($value, 'm') !== false) {
  151
+                $result['month'] = $value;
  152
+            } elseif (stripos($value, 'y') !== false) {
  153
+                $result['year'] = $value;
  154
+            } else {
  155
+                $result[] = $value;
  156
+            }
  157
+        }
  158
+
  159
+        return $result;
  160
+    }
  161
+
  162
+    /**
  163
+     * @param  int $dateType
  164
+     * @return FormDateSelect
  165
+     */
  166
+    public function setDateType($dateType)
  167
+    {
  168
+        // The FULL format uses values that are not used
  169
+        if ($dateType === IntlDateFormatter::FULL) {
  170
+            $dateType = IntlDateFormatter::LONG;
  171
+        }
  172
+
  173
+        $this->dateType = $dateType;
  174
+
  175
+        return $this;
  176
+    }
  177
+
  178
+    /**
  179
+     * @return int
  180
+     */
  181
+    public function getDateType()
  182
+    {
  183
+        return $this->dateType;
  184
+    }
  185
+
  186
+    /**
  187
+     * @param  string $locale
  188
+     * @return FormDateSelect
  189
+     */
  190
+    public function setLocale($locale)
  191
+    {
  192
+        $this->locale = $locale;
  193
+        return $this;
  194
+    }
  195
+
  196
+    /**
  197
+     * @return string
  198
+     */
  199
+    public function getLocale()
  200
+    {
  201
+        if ($this->locale === null) {
  202
+            $this->locale = Locale::getDefault();
  203
+        }
  204
+
  205
+        return $this->locale;
  206
+    }
  207
+
  208
+    /**
  209
+     * Create a key => value options for months
  210
+     *
  211
+     * @param string $pattern Pattern to use for months
  212
+     * @return array
  213
+     */
  214
+    public function getMonthsOptions($pattern)
  215
+    {
  216
+        $keyFormatter   = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'MM');
  217
+        $valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
  218
+        $date           = new DateTime('1970-01-01');
  219
+
  220
+        $result = array();
  221
+        for ($month = 1; $month <= 12; $month++) {
  222
+            $key   = $keyFormatter->format($date);
  223
+            $value = $valueFormatter->format($date);
  224
+            $result[$key] = $value;
  225
+
  226
+            $date->modify('+1 month');
  227
+        }
  228
+
  229
+        return $result;
  230
+    }
  231
+
  232
+    /**
  233
+     * Create a key => value options for years
  234
+     * NOTE: we don't use a pattern for years, as years written as two digits can lead to hard to
  235
+     * read date for users, so we only use four digits years
  236
+     *
  237
+     * @param int $minYear
  238
+     * @param int $maxYear
  239
+     * @return array
  240
+     */
  241
+    public function getYearsOptions($minYear, $maxYear)
  242
+    {
  243
+        $result = array();
  244
+        for ($i = $maxYear; $i >= $minYear; --$i) {
  245
+            $result[$i] = $i;
  246
+        }
  247
+
  248
+        return $result;
  249
+    }
  250
+
  251
+    /**
  252
+     * Retrieve the FormSelect helper
  253
+     *
  254
+     * @return FormRow
  255
+     */
  256
+    protected function getSelectElementHelper()
  257
+    {
  258
+        if ($this->selectHelper) {
  259
+            return $this->selectHelper;
  260
+        }
  261
+
  262
+        if (method_exists($this->view, 'plugin')) {
  263
+            $this->selectHelper = $this->view->plugin('formselect');
  264
+        }
  265
+
  266
+        return $this->selectHelper;
  267
+    }
  268
+}
  269
+
2  library/Zend/Form/View/HelperConfig.php
@@ -43,6 +43,7 @@ class HelperConfig implements ConfigInterface
43 43
         'formdate'               => 'Zend\Form\View\Helper\FormDate',
44 44
         'formdatetime'           => 'Zend\Form\View\Helper\FormDateTime',
45 45
         'formdatetimelocal'      => 'Zend\Form\View\Helper\FormDateTimeLocal',
  46
+        'formdateselect'         => 'Zend\Form\View\Helper\FormDateSelect',
46 47
         'formelement'            => 'Zend\Form\View\Helper\FormElement',
47 48
         'formelementerrors'      => 'Zend\Form\View\Helper\FormElementErrors',
48 49
         'formemail'              => 'Zend\Form\View\Helper\FormEmail',
@@ -52,6 +53,7 @@ class HelperConfig implements ConfigInterface
52 53
         'forminput'              => 'Zend\Form\View\Helper\FormInput',
53 54
         'formlabel'              => 'Zend\Form\View\Helper\FormLabel',
54 55
         'formmonth'              => 'Zend\Form\View\Helper\FormMonth',
  56
+        'formmonthselect'        => 'Zend\Form\View\Helper\FormMonthSelect',
55 57
         'formmulticheckbox'      => 'Zend\Form\View\Helper\FormMultiCheckbox',
56 58
         'formnumber'             => 'Zend\Form\View\Helper\FormNumber',
57 59
         'formpassword'           => 'Zend\Form\View\Helper\FormPassword',
42  tests/ZendTest/Form/Element/DateSelectTest.php
... ...
@@ -0,0 +1,42 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_Form
  9
+ */
  10
+
  11
+namespace ZendTest\Form\Element;
  12
+
  13
+use PHPUnit_Framework_TestCase as TestCase;
  14
+use Zend\Form\Element\DateSelect as DateSelectElement;
  15
+use Zend\Form\Factory;
  16
+
  17
+class DateSelectTest extends TestCase
  18
+{
  19
+    public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttributes()
  20
+    {
  21
+        $element = new DateSelectElement();
  22
+
  23
+        $inputSpec = $element->getInputSpecification();
  24
+        $this->assertArrayHasKey('validators', $inputSpec);
  25
+        $this->assertInternalType('array', $inputSpec['validators']);
  26
+
  27
+        $expectedClasses = array(
  28
+            'Zend\Validator\Date'
  29
+        );
  30
+        foreach ($inputSpec['validators'] as $validator) {
  31
+            $class = get_class($validator);
  32
+            $this->assertTrue(in_array($class, $expectedClasses), $class);
  33
+            switch ($class) {
  34
+                case 'Zend\Validator\Date':
  35
+                    $this->assertEquals('Y-m-d', $validator->getFormat());
  36
+                    break;
  37
+                default:
  38
+                    break;
  39
+            }
  40
+        }
  41
+    }
  42
+}
74  tests/ZendTest/Form/Element/MonthSelectTest.php
... ...
@@ -0,0 +1,74 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_Form
  9
+ */
  10
+
  11
+namespace ZendTest\Form\Element;
  12
+
  13
+use PHPUnit_Framework_TestCase as TestCase;
  14
+use Zend\Form\Element\MonthSelect as MonthSelectElement;
  15
+use Zend\Form\Factory;
  16
+
  17
+class MonthSelectTest extends TestCase
  18
+{
  19
+    public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttributes()
  20
+    {
  21
+        $element = new MonthSelectElement();
  22
+
  23
+        $inputSpec = $element->getInputSpecification();
  24
+        $this->assertArrayHasKey('validators', $inputSpec);
  25
+        $this->assertInternalType('array', $inputSpec['validators']);
  26
+
  27
+        $expectedClasses = array(
  28
+            'Zend\Validator\Regex'
  29
+        );
  30
+        foreach ($inputSpec['validators'] as $validator) {
  31
+            $class = get_class($validator);
  32
+            $this->assertTrue(in_array($class, $expectedClasses), $class);
  33
+            switch ($class) {
  34
+                case 'Zend\Validator\Regex':
  35
+                    $this->assertEquals('/^[0-9]{4}\-(0?[1-9]|1[012])$/', $validator->getPattern());
  36
+                    break;
  37
+                default:
  38
+                    break;
  39
+            }
  40
+        }
  41
+    }
  42
+
  43
+    /**
  44
+     * Note about those tests: 2012-1 is not valid in HTML5 validation, but here we use selects, and in some
  45
+     * locales, the month may be expressed using only 1 digit, so this is valid here
  46
+     *
  47
+     * @return array
  48
+     */
  49
+    public function monthValuesDataProvider()
  50
+    {
  51
+        return array(
  52
+            //    value         expected
  53
+            array('2012-01',    true),
  54
+            array('2012-12',    true),
  55
+            array('2012-13',    false),
  56
+            array('2012-12-01', false),
  57
+            array('12-2012',    false),
  58
+            array('2012-1',     true),
  59
+            array('12-01',      false),
  60
+        );
  61
+    }
  62
+
  63
+    /**
  64
+     * @dataProvider monthValuesDataProvider
  65
+     */
  66
+    public function testMonthValidation($value, $expected)
  67
+    {
  68
+        $element = new MonthSelectElement('foo');
  69
+        $inputSpec = $element->getInputSpecification();
  70
+        $this->assertArrayHasKey('validators', $inputSpec);
  71
+        $monthValidator = $inputSpec['validators'][0];
  72
+        $this->assertEquals($expected, $monthValidator->isValid($value));
  73
+    }
  74
+}
58  tests/ZendTest/Form/View/Helper/FormDateSelectTest.php
... ...
@@ -0,0 +1,58 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_Form
  9
+ */
  10
+
  11
+namespace ZendTest\Form\View\Helper;
  12
+
  13
+use Zend\Form\Element\DateSelect;
  14
+use Zend\Form\View\Helper\FormDateSelect as FormDateSelectHelper;
  15
+
  16
+/**
  17
+ * @category   Zend
  18
+ * @package    Zend_Form
  19
+ * @subpackage UnitTest
  20
+ */
  21
+class FormDateSelectTest extends CommonTestCase
  22
+{
  23
+    public function setUp()
  24
+    {
  25
+        $this->helper = new FormDateSelectHelper();
  26
+        parent::setUp();
  27
+    }
  28
+
  29
+    public function testRaisesExceptionWhenNameIsNotPresentInElement()
  30
+    {
  31
+        $element = new DateSelect();
  32
+        $this->setExpectedException('Zend\Form\Exception\DomainException', 'name');
  33
+        $this->helper->render($element);
  34
+    }
  35
+
  36
+    public function testGeneratesThreeSelectsWithElement()
  37
+    {
  38
+        $element = new DateSelect('foo');
  39
+        $markup  = $this->helper->render($element);
  40
+        $this->assertContains('<select name="day"', $markup);
  41
+        $this->assertContains('<select name="month"', $markup);
  42
+        $this->assertContains('<select name="year"', $markup);
  43
+    }
  44
+
  45
+    public function testInvokeProxiesToRender()
  46
+    {
  47
+        $element = new DateSelect('foo');
  48
+        $markup  = $this->helper->__invoke($element);
  49
+        $this->assertContains('<select name="day"', $markup);
  50
+        $this->assertContains('<select name="month"', $markup);
  51
+        $this->assertContains('<select name="year"', $markup);
  52
+    }
  53
+
  54
+    public function testInvokeWithNoElementChainsHelper()
  55
+    {
  56
+        $this->assertSame($this->helper, $this->helper->__invoke());
  57
+    }
  58
+}
58  tests/ZendTest/Form/View/Helper/FormMonthSelectTest.php
... ...
@@ -0,0 +1,58 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_Form
  9
+ */
  10
+
  11
+namespace ZendTest\Form\View\Helper;
  12
+
  13
+use Zend\Form\Element\MonthSelect;
  14
+use Zend\Form\View\Helper\FormMonthSelect as FormMonthSelectHelper;
  15
+
  16
+/**
  17
+ * @category   Zend
  18
+ * @package    Zend_Form
  19
+ * @subpackage UnitTest
  20
+ */
  21
+class FormMonthSelectTest extends CommonTestCase
  22
+{
  23
+    public function setUp()
  24
+    {
  25
+        $this->helper = new FormMonthSelectHelper();
  26
+        parent::setUp();
  27
+    }
  28
+
  29
+    public function testRaisesExceptionWhenNameIsNotPresentInElement()
  30
+    {
  31
+        $element = new MonthSelect();
  32
+        $this->setExpectedException('Zend\Form\Exception\DomainException', 'name');
  33
+        $this->helper->render($element);
  34
+    }
  35
+
  36
+    public function testGeneratesTwoSelectsWithElement()
  37
+    {
  38
+        $element = new MonthSelect('foo');
  39
+        $markup  = $this->helper->render($element);
  40
+        $this->assertNotContains('<select name="day"', $markup);
  41
+        $this->assertContains('<select name="month"', $markup);
  42
+        $this->assertContains('<select name="year"', $markup);
  43
+    }
  44
+
  45
+    public function testInvokeProxiesToRender()
  46
+    {
  47
+        $element = new MonthSelect('foo');
  48
+        $markup  = $this->helper->__invoke($element);
  49
+        $this->assertNotContains('<select name="day"', $markup);
  50
+        $this->assertContains('<select name="month"', $markup);
  51
+        $this->assertContains('<select name="year"', $markup);
  52
+    }
  53
+
  54
+    public function testInvokeWithNoElementChainsHelper()
  55
+    {
  56
+        $this->assertSame($this->helper, $this->helper->__invoke());
  57
+    }
  58
+}

0 notes on commit c3a7d8c

Please sign in to comment.
Something went wrong with that request. Please try again.