summaryrefslogtreecommitdiff
path: root/platform/www/lib/plugins/bureaucracy/helper/fielddate.php
blob: 6feae1cb2be401fa0e5914c9ea8c54d69f6c9610 (plain)
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
<?php
/**
 * Class helper_plugin_bureaucracy_fielddate
 *
 * A date in the format YYYY-MM-DD, provides a date picker
 */
class helper_plugin_bureaucracy_fielddate extends helper_plugin_bureaucracy_fieldtextbox {
    /**
     * Arguments:
     *  - cmd
     *  - label
     *  - ^ (optional)
     *
     * @param array $args The tokenized definition, only split at spaces
     */
    public function initialize($args) {
        parent::initialize($args);
        $attr = array(
            'class' => 'datepicker edit',
            'maxlength'=>'10'
        );
        if(!isset($this->opt['optional'])) {
            $attr['required'] = 'required';
            $attr['class'] .= ' required';
        }
        $this->tpl = form_makeTextField('@@NAME@@', '@@VALUE@@', '@@DISPLAY@@', '@@ID@@', '@@CLASS@@', $attr);
    }

    /**
     * Validate field input
     *
     * @throws Exception when empty or wrong date format
     */
    protected function _validate() {
        parent::_validate();

        $value = $this->getParam('value');
        if (!is_null($value) && !preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
            throw new Exception(sprintf($this->getLang('e_date'),hsc($this->getParam('display'))));
        }
    }
}