1 <?php
2 3 4 5 6 7 8
9
10 class EditableDateField extends EditableFormField {
11
12 static $singular_name = 'Date Field';
13
14 static $plural_name = 'Date Fields';
15
16 function getFieldConfiguration() {
17 $default = ($this->getSetting('DefaultToToday')) ? $this->getSetting('DefaultToToday') : false;
18 $label = _t('EditableFormField.DEFAULTTOTODAY', 'Default to Today?');
19
20 return new FieldSet(
21 new CheckboxField($this->getSettingName("DefaultToToday"), $label, $default)
22 );
23 }
24
25 function populateFromPostData($data) {
26 $fieldPrefix = 'Default-';
27
28 if(empty($data['Default']) && !empty($data[$fieldPrefix.'Year']) && !empty($data[$fieldPrefix.'Month']) && !empty($data[$fieldPrefix.'Day'])) {
29 $data['Default'] = $data['Year'] . '-' . $data['Month'] . '-' . $data['Day'];
30 }
31
32 parent::populateFromPostData($data);
33 }
34
35 36 37 38 39 40
41 public function getFormField() {
42
43 Requirements::javascript(THIRDPARTY_DIR .'/jquery-ui/jquery.ui.core.js');
44 Requirements::javascript(THIRDPARTY_DIR .'/jquery-ui/jquery.ui.datepicker.js');
45 Requirements::javascript(THIRDPARTY_DIR .'/jquery-ui/i18n/jquery-ui-i18n.js');
46 Requirements::javascript(THIRDPARTY_DIR .'/jquery-ui/i18n/jquery.ui.datepicker-'.i18n::get_lang_from_locale(i18n::get_locale()).'.js');
47
48 $dateFormat = DateField_View_JQuery::convert_iso_to_jquery_format(i18n::get_date_format());
49
50 Requirements::customScript(<<<JS
51 (function($) {
52 $(document).ready(function() {
53 $('input[name^=EditableDateField]').attr('autocomplete', 'off').datepicker({ dateFormat: '$dateFormat' });
54 });
55 })(jQuery);
56 JS
57 , 'UserFormsDate');
58
59 // css for jquery date picker
60 Requirements::css(THIRDPARTY_DIR .'/jquery-ui-themes/smoothness/jquery-ui-1.8rc3.custom.css');
61
62 $default = ($this->getSetting('DefaultToToday')) ? date('d/m/Y') : $this->Default;
63
64 return new DateField( $this->Name, $this->Title, $default);
65 }
66
67 /**
68 * Return the validation information related to this field. This is
69 * interrupted as a JSON object for validate plugin and used in the
70 * PHP.
71 *
72 * @see http://docs.jquery.com/Plugins/Validation/Methods
73 * @return Array
74 */
75 public function getValidation() {
76 return array_merge(
77 parent::getValidation(),
78 array(
79 'date' => true
80 )
81 );
82 }
83 }
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-