silverstripe-userforms/code/model/formfields/EditableDateField.php
Damian Mooyman 5e0b7fdf7a Updated jquery validate plugin (dist files only)
Cleanup of validation script. Refactor into template to allow customisation of validation.
Explicitly set error element to 'span' as per new jquery-validate support
[ref: CWPBUG-110]
2014-05-22 10:34:11 +12:00

60 lines
1.6 KiB
PHP
Executable File

<?php
/**
* EditableDateField
*
* Allows a user to add a date field.
*
* @package userforms
*/
class EditableDateField extends EditableFormField {
private static $singular_name = 'Date Field';
private static $plural_name = 'Date Fields';
public function getFieldConfiguration() {
$default = ($this->getSetting('DefaultToToday')) ? $this->getSetting('DefaultToToday') : false;
$label = _t('EditableFormField.DEFAULTTOTODAY', 'Default to Today?');
return new FieldList(
new CheckboxField($this->getSettingName("DefaultToToday"), $label, $default)
);
}
public function populateFromPostData($data) {
$fieldPrefix = 'Default-';
if(empty($data['Default']) && !empty($data[$fieldPrefix.'Year']) && !empty($data[$fieldPrefix.'Month']) && !empty($data[$fieldPrefix.'Day'])) {
$data['Default'] = $data['Year'] . '-' . $data['Month'] . '-' . $data['Day'];
}
parent::populateFromPostData($data);
}
/**
* Return the form field
*
*/
public function getFormField() {
$defaultValue = ($this->getSetting('DefaultToToday')) ? date('Y-m-d') : $this->Default;
$field = new DateField( $this->Name, $this->Title, $defaultValue);
$field->setConfig('showcalendar', true);
return $field;
}
/**
* Return the validation information related to this field. This is
* interrupted as a JSON object for validate plugin and used in the
* PHP.
*
* @see http://docs.jquery.com/Plugins/Validation/Methods
* @return Array
*/
public function getValidation() {
return array_merge(parent::getValidation(), array(
'date' => true
));
}
}