mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-08-28 13:36:12 +02:00
BUGFIX Removed "numeric" class on day/month/year fields in DMYField, the auto-magic NumericField? javascript resetting thats triggered onkeyup() is just a bit too much for its own good (merged from branches/2.3-nzct) BUGFIX Removed hiddenfield dependency on DMYDateField, save into three distinct input fields and validate accordingly. All underlying logic already works with this notation. (merged from branches/2.3-nzct) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@82062 467b73ca-7a2a-4603-9d3b-597d59a354a9
110 lines
3.0 KiB
PHP
110 lines
3.0 KiB
PHP
<?php
|
|
/**
|
|
* Displays a date field with day, month and year boxes, with a calendar to select
|
|
* the date.
|
|
*
|
|
* @todo Add localization support, see http://open.silverstripe.com/ticket/2931
|
|
*
|
|
* @package forms
|
|
* @subpackage fields-datetime
|
|
*/
|
|
class DMYDateField extends CalendarDateField {
|
|
|
|
function setValue( $value ) {
|
|
if( is_array( $value ) && $value['Day'] && $value['Month'] && $value['Year'] )
|
|
$this->value = $value['Year'] . '-' . $value['Month'] . '-' . $value['Day'];
|
|
else if(is_array($value)&&(!$value['Day']||!$value['Month']||!$value['Year']))
|
|
$this->value = null;
|
|
else if(is_string($value))
|
|
$this->value = $value;
|
|
}
|
|
|
|
function Field() {
|
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/CalendarDateField.js");
|
|
|
|
$field = DateField::Field();
|
|
|
|
$id = $this->id();
|
|
$val = $this->attrValue();
|
|
|
|
if( preg_match( '/^\d{2}\/\d{2}\/\d{4}$/', $val ) ) {
|
|
$dateArray = explode( '/', $val );
|
|
|
|
$val = $dateArray[2] . '-' . $dateArray[1] . '-' . $dateArray[0];
|
|
}
|
|
|
|
$day = $month = $year = null;
|
|
if($val) {
|
|
$dateArray = explode( '-', $val );
|
|
|
|
$day = $dateArray[2];
|
|
$month = $dateArray[1];
|
|
$year = $dateArray[0];
|
|
}
|
|
|
|
$fieldName = $this->name;
|
|
|
|
return <<<HTML
|
|
<div class="dmycalendardate">
|
|
<input type="text" id="$id-day" class="day" name="{$fieldName}[Day]" value="$day" maxlength="2" />/
|
|
<input type="text" id="$id-month" class="month" name="{$fieldName}[Month]" value="$month" maxlength="2" />/
|
|
<input type="text" id="$id-year" class="year" name="{$fieldName}[Year]" value="$year" maxlength="4" />
|
|
<div class="calendarpopup" id="{$id}-calendar"></div>
|
|
</div>
|
|
HTML;
|
|
}
|
|
|
|
function validate($validator)
|
|
{
|
|
if(!empty ($this->value) && !preg_match('/^[0-90-9]{2,4}\-[0-9]{1,2}\-[0-90-9]{1,2}$/', $this->value))
|
|
{
|
|
$validator->validationError(
|
|
$this->name,
|
|
_t('DMYDateField.VALIDDATEFORMAT', "Please enter a valid date format (DD-MM-YYYY)."),
|
|
"validation",
|
|
false
|
|
);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function jsValidation() {
|
|
$formID = $this->form->FormName();
|
|
$error = _t('DateField.VALIDATIONJS', 'Please enter a valid date format (DD/MM/YYYY).');
|
|
$jsFunc =<<<JS
|
|
Behaviour.register({
|
|
"#$formID": {
|
|
validateDMYDate: function(fieldName) {
|
|
var value = \$F(_CURRENT_FORM.elements[fieldName+'[Day]'])
|
|
+ '/'
|
|
+ \$F(_CURRENT_FORM.elements[fieldName+'[Month]'])
|
|
+ '/'
|
|
+ \$F(_CURRENT_FORM.elements[fieldName+'[Year]'])
|
|
;
|
|
|
|
if(value && value.length > 0 && !value.match(/^[0-9]{1,2}\/[0-9]{1,2}\/[0-90-9]{2,4}\$/)) {
|
|
validationError(_CURRENT_FORM.elements[fieldName+'[Day]'],"$error","validation",false);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
});
|
|
JS;
|
|
Requirements :: customScript($jsFunc, 'func_validateDMYDate_'.$formID);
|
|
|
|
// return "\$('$formID').validateDate('$this->name');";
|
|
return <<<JS
|
|
if(\$('$formID')){
|
|
if(typeof fromAnOnBlur != 'undefined'){
|
|
if(fromAnOnBlur.name == '$this->name')
|
|
\$('$formID').validateDMYDate('$this->name');
|
|
}else{
|
|
\$('$formID').validateDMYDate('$this->name');
|
|
}
|
|
}
|
|
JS;
|
|
}
|
|
}
|
|
?>
|