Removed PastDateValidator and use DbDateValidator instead

This commit is contained in:
Lucas Bartholemy 2016-10-27 18:33:44 +02:00
parent 75c7c2c984
commit f739c4a30d
4 changed files with 54 additions and 28 deletions

View File

@ -13,25 +13,28 @@ use yii\validators\Validator;
/**
* Description of AbstractDateValidator
*
* @deprecated since version 1.1.2
* @author buddha
*/
abstract class AbstractDateValidator extends Validator
{
public $message;
abstract public function dateValidation($timestamp);
public function validateAttribute($model, $attribute)
{
$date = $model->$attribute;
if(is_string($model->$attribute)) {
if (is_string($model->$attribute)) {
$date = strtotime($model->$attribute);
} else if($model->$attribute instanceof DateTime) {
} else if ($model->$attribute instanceof DateTime) {
$date = $model->$attribute->getTimestamp();
}
if ($this->dateValidation($date)) {
$this->addError($model, $attribute, $this->message);
}
}
}

View File

@ -9,21 +9,35 @@
namespace humhub\components\validators;
use Yii;
use yii\validators\DateValidator;
/**
* Description of PastDateValidator
*
* PastDateValidator ensurs the date is in the past
*
* @deprecated since version 1.1.2
* @author buddha
*/
class PastDateValidator extends AbstractDateValidator
{
class PastDateValidator extends \yii\validators\DbDateValidator
{
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->message = Yii::t('base', 'The date has to be in the past.');
}
public function dateValidation($dateTS)
/**
* @inheritdoc
*/
public function validateAttribute($model, $attribute)
{
return $dateTS > time();
$timestamp = $this->parseDateValue($model->$attribute);
if ($timestamp !== false && $timestamp > time()) {
$this->addError($model, $attribute, $this->message);
}
}
}

View File

@ -58,7 +58,10 @@ class DbDateValidator extends \yii\validators\DateValidator
// To ensure we're saving 00:00:00 time infos.
$date->setTimezone(new \DateTimeZone('UTC'));
}
$model->$attribute = $date->format($this->convertToFormat);
if ($this->convertToFormat !== null) {
$model->$attribute = $date->format($this->convertToFormat);
}
}
}
@ -83,10 +86,10 @@ class DbDateValidator extends \yii\validators\DateValidator
$attributeName = $this->timeAttribute;
return $model->$attributeName;
}
return '';
}
/**
* Parses a date and optionally a time if timeAttribute is specified.
*

View File

@ -40,17 +40,17 @@ class Birthday extends Date
public function getFormDefinition($definition = array())
{
return parent::getFormDefinition([
get_class($this) => [
'type' => 'form',
'title' => Yii::t('UserModule.models_ProfileFieldTypeBirthday', 'Birthday field options'),
'elements' => [
'defaultHideAge' => [
'type' => 'checkbox',
'label' => Yii::t('UserModule.models_ProfileFieldTypeBirthday', 'Hide age per default'),
'class' => 'form-control',
],
]
]
get_class($this) => [
'type' => 'form',
'title' => Yii::t('UserModule.models_ProfileFieldTypeBirthday', 'Birthday field options'),
'elements' => [
'defaultHideAge' => [
'type' => 'checkbox',
'label' => Yii::t('UserModule.models_ProfileFieldTypeBirthday', 'Hide age per default'),
'class' => 'form-control',
],
]
]
]);
}
@ -89,7 +89,13 @@ class Birthday extends Date
{
$rules[] = [$this->profileField->internal_name . "_hide_year", 'in', 'range' => [0, 1]];
$rules[] = [$this->profileField->internal_name, \humhub\components\validators\PastDateValidator::className()];
$rules[] = [$this->profileField->internal_name,
\humhub\libs\DbDateValidator::className(),
'format' => Yii::$app->formatter->dateInputFormat,
'convertToFormat' => null,
'max' => time(),
'tooBig' => Yii::t('base', 'The date has to be in the past.')
];
return parent::getFieldRules($rules);
}
@ -103,7 +109,7 @@ class Birthday extends Date
'format' => Yii::$app->formatter->dateInputFormat,
'class' => 'form-control',
'readonly' => (!$this->profileField->editable),
'yearRange' => (date('Y') - 100) . ":". date('Y')
'yearRange' => (date('Y') - 100) . ":" . date('Y')
],
$this->profileField->internal_name . "_hide_year" => [
'type' => 'checkbox',