From fff7a9ebaa1b68dd4f4e6560895c4d3078ac1c12 Mon Sep 17 00:00:00 2001 From: Megan Watson Date: Fri, 2 Oct 2009 17:34:59 +0200 Subject: [PATCH] Added date validation for the date field. PT: 1347284 Jira: KTC-793 --- plugins/ktcore/KTCorePlugin.php | 1 + plugins/ktcore/KTValidators.php | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- plugins/ktcore/KTWidgets.php | 18 ++++++++++++++++++ plugins/ktcore/document/edit.php | 4 ++-- 4 files changed, 87 insertions(+), 11 deletions(-) diff --git a/plugins/ktcore/KTCorePlugin.php b/plugins/ktcore/KTCorePlugin.php index b29a154..93ea24d 100755 --- a/plugins/ktcore/KTCorePlugin.php +++ b/plugins/ktcore/KTCorePlugin.php @@ -216,6 +216,7 @@ class KTCorePlugin extends KTPlugin { $this->registerValidator('KTRequiredFileValidator', 'ktcore.validators.requiredfile', 'KTValidators.php'); $this->registerValidator('KTFileIllegalCharValidator', 'ktcore.validators.fileillegalchar', 'KTValidators.php'); $this->registerValidator('KTArrayValidator', 'ktcore.validators.array', 'KTValidators.php'); + $this->registerValidator('KTDateValidator', 'ktcore.validators.date', 'KTValidators.php'); // criterion $this->registerCriterion('NameCriterion', 'ktcore.criteria.name', KT_LIB_DIR . '/browse/Criteria.inc'); diff --git a/plugins/ktcore/KTValidators.php b/plugins/ktcore/KTValidators.php index 70e2323..6306f7d 100644 --- a/plugins/ktcore/KTValidators.php +++ b/plugins/ktcore/KTValidators.php @@ -6,31 +6,31 @@ * Document Management Made Simple * Copyright (C) 2008, 2009 KnowledgeTree Inc. * Portions copyright The Jam Warehouse Software (Pty) Limited - * + * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 3 as published by the * Free Software Foundation. - * + * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * - * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, + * + * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, * California 94120-7775, or email info@knowledgetree.com. - * + * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU General Public License version 3. - * + * * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "Powered by - * KnowledgeTree" logo and retain the original copyright notice. If the display of the + * KnowledgeTree" logo and retain the original copyright notice. If the display of the * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices - * must display the words "Powered by KnowledgeTree" and retain the original + * must display the words "Powered by KnowledgeTree" and retain the original * copyright notice. * Contributor( s): ______________________________________ */ @@ -434,6 +434,9 @@ class KTFieldsetValidator extends KTValidator { $d = (array) KTUtil::arrayGet($data, $this->sInputVariable); //var_dump($this); exit(0); foreach ($this->_validators as $v) { + if(is_null($v)){ + continue; + } $res = $v->validate($d); // results comes out with a set of names and values. @@ -562,4 +565,58 @@ class KTArrayValidator extends KTValidator { } } +class KTDateValidator extends KTValidator { + var $sNamespace = 'ktcore.validators.date'; + + var $sFormat; + var $sFormatWarning; + + function configure($aOptions) { + $res = parent::configure($aOptions); + if (PEAR::isError($res)) { + return $res; + } + + $this->sFormat = KTUtil::arrayGet($aOptions, 'format', 'YYYY-MM-DD'); + + $this->sFormatWarning = KTUtil::arrayGet($aOptions, 'format_warning', + sprintf(_kt('The date entered must be in the format "%s".'), $this->sFormat)); + + $this->bTrim = KTUtil::arrayGet($aOptions, 'trim', true, false); + } + + function validate($data) { + $results = array(); + $errors = array(); + + // very simple if we're required and not present, fail + // otherwise, its ok. + $val = KTUtil::arrayGet($data, $this->sInputVariable); + + if ($this->bTrim) { + $val = trim($val); + } + + if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $val, $parts)) + { + //check weather the date is valid of not + if(checkdate($parts[2],$parts[3],$parts[1])){ + }else{ + $errors[$this->sBasename] = $this->sFormatWarning; + } + }else { + $errors[$this->sBasename] = $this->sFormatWarning; + } + + if ($this->bProduceOutput) { + $results[$this->sOutputVariable] = $val; + } + + return array( + 'errors' => $errors, + 'results' => $results, + ); + } +} + ?> diff --git a/plugins/ktcore/KTWidgets.php b/plugins/ktcore/KTWidgets.php index e1cb41b..156501a 100755 --- a/plugins/ktcore/KTWidgets.php +++ b/plugins/ktcore/KTWidgets.php @@ -953,4 +953,22 @@ class KTCoreTextAreaWidget extends KTWidget { class KTCoreDateWidget extends KTWidget { var $sNamespace = 'ktcore.widgets.date'; var $sTemplate = 'ktcore/forms/widgets/date'; + + function getValidators() { + if (!$this->bAutoValidate) { + return null; + } + $validators = parent::getValidators(); // required, etc. + + $oVF =& KTValidatorFactory::getSingleton(); + + $val = array(); + if(!empty($validators) && !PEAR::isError($validators)) $val[] = $validators; + $val[] = $oVF->get('ktcore.validators.date', array( + 'test' => $this->sOrigname, + 'basename' => $this->sBasename + )); + + return $val; + } } \ No newline at end of file diff --git a/plugins/ktcore/document/edit.php b/plugins/ktcore/document/edit.php index 690d595..06567e8 100755 --- a/plugins/ktcore/document/edit.php +++ b/plugins/ktcore/document/edit.php @@ -217,14 +217,14 @@ class KTDocumentEditAction extends KTDocumentAction { foreach ($fields as $oField) { $val = KTUtil::arrayGet($values, 'metadata_' . $oField->getId()); - if($oField->getDataType() == "LARGE TEXT") + if($oField->getDataType() == "LARGE TEXT" && !is_null($oField->getMaxLength())) { if(strlen(strip_tags($val)) > $oField->getMaxLength()) { $oForm->handleError(sprintf(_kt("Value exceeds max allowed length of %d characters for %s. Current value is %d characters."), $oField->getMaxLength(), $oField->getName(), strlen(strip_tags($val)))); } } - + // FIXME "null" has strange meanings here. if (!is_null($val)) { if(KTPluginUtil::pluginIsActive('inet.multiselect.lookupvalue.plugin') && is_array($val) && $oField->getHasInetLookup()) { -- libgit2 0.21.4