From f739c4a30d9874b3eee7bc4e01abe7c1fc40163c Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Thu, 27 Oct 2016 18:33:44 +0200 Subject: [PATCH] Removed PastDateValidator and use DbDateValidator instead --- .../validators/AbstractDateValidator.php | 13 +++++--- .../validators/PastDateValidator.php | 28 ++++++++++++---- protected/humhub/libs/DbDateValidator.php | 9 ++++-- .../user/models/fieldtype/Birthday.php | 32 +++++++++++-------- 4 files changed, 54 insertions(+), 28 deletions(-) diff --git a/protected/humhub/components/validators/AbstractDateValidator.php b/protected/humhub/components/validators/AbstractDateValidator.php index 64690cec2d..798ab14b47 100644 --- a/protected/humhub/components/validators/AbstractDateValidator.php +++ b/protected/humhub/components/validators/AbstractDateValidator.php @@ -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); } } + } diff --git a/protected/humhub/components/validators/PastDateValidator.php b/protected/humhub/components/validators/PastDateValidator.php index b80c0076ed..aff1c1ec3f 100644 --- a/protected/humhub/components/validators/PastDateValidator.php +++ b/protected/humhub/components/validators/PastDateValidator.php @@ -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); + } } + } diff --git a/protected/humhub/libs/DbDateValidator.php b/protected/humhub/libs/DbDateValidator.php index ce57110fa0..c71f9d4d6d 100644 --- a/protected/humhub/libs/DbDateValidator.php +++ b/protected/humhub/libs/DbDateValidator.php @@ -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. * diff --git a/protected/humhub/modules/user/models/fieldtype/Birthday.php b/protected/humhub/modules/user/models/fieldtype/Birthday.php index b2b2e8281a..ad6af9d617 100644 --- a/protected/humhub/modules/user/models/fieldtype/Birthday.php +++ b/protected/humhub/modules/user/models/fieldtype/Birthday.php @@ -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',