Commit fff7a9ebaa1b68dd4f4e6560895c4d3078ac1c12
1 parent
a20fe6eb
Added date validation for the date field.
PT: 1347284 Jira: KTC-793 Committed by: Megan Watson
Showing
4 changed files
with
87 additions
and
11 deletions
plugins/ktcore/KTCorePlugin.php
| ... | ... | @@ -216,6 +216,7 @@ class KTCorePlugin extends KTPlugin { |
| 216 | 216 | $this->registerValidator('KTRequiredFileValidator', 'ktcore.validators.requiredfile', 'KTValidators.php'); |
| 217 | 217 | $this->registerValidator('KTFileIllegalCharValidator', 'ktcore.validators.fileillegalchar', 'KTValidators.php'); |
| 218 | 218 | $this->registerValidator('KTArrayValidator', 'ktcore.validators.array', 'KTValidators.php'); |
| 219 | + $this->registerValidator('KTDateValidator', 'ktcore.validators.date', 'KTValidators.php'); | |
| 219 | 220 | |
| 220 | 221 | // criterion |
| 221 | 222 | $this->registerCriterion('NameCriterion', 'ktcore.criteria.name', KT_LIB_DIR . '/browse/Criteria.inc'); | ... | ... |
plugins/ktcore/KTValidators.php
| ... | ... | @@ -6,31 +6,31 @@ |
| 6 | 6 | * Document Management Made Simple |
| 7 | 7 | * Copyright (C) 2008, 2009 KnowledgeTree Inc. |
| 8 | 8 | * Portions copyright The Jam Warehouse Software (Pty) Limited |
| 9 | - * | |
| 9 | + * | |
| 10 | 10 | * This program is free software; you can redistribute it and/or modify it under |
| 11 | 11 | * the terms of the GNU General Public License version 3 as published by the |
| 12 | 12 | * Free Software Foundation. |
| 13 | - * | |
| 13 | + * | |
| 14 | 14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | 16 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 17 | 17 | * details. |
| 18 | - * | |
| 18 | + * | |
| 19 | 19 | * You should have received a copy of the GNU General Public License |
| 20 | 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | - * | |
| 22 | - * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, | |
| 21 | + * | |
| 22 | + * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, | |
| 23 | 23 | * California 94120-7775, or email info@knowledgetree.com. |
| 24 | - * | |
| 24 | + * | |
| 25 | 25 | * The interactive user interfaces in modified source and object code versions |
| 26 | 26 | * of this program must display Appropriate Legal Notices, as required under |
| 27 | 27 | * Section 5 of the GNU General Public License version 3. |
| 28 | - * | |
| 28 | + * | |
| 29 | 29 | * In accordance with Section 7(b) of the GNU General Public License version 3, |
| 30 | 30 | * these Appropriate Legal Notices must retain the display of the "Powered by |
| 31 | - * KnowledgeTree" logo and retain the original copyright notice. If the display of the | |
| 31 | + * KnowledgeTree" logo and retain the original copyright notice. If the display of the | |
| 32 | 32 | * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices |
| 33 | - * must display the words "Powered by KnowledgeTree" and retain the original | |
| 33 | + * must display the words "Powered by KnowledgeTree" and retain the original | |
| 34 | 34 | * copyright notice. |
| 35 | 35 | * Contributor( s): ______________________________________ |
| 36 | 36 | */ |
| ... | ... | @@ -434,6 +434,9 @@ class KTFieldsetValidator extends KTValidator { |
| 434 | 434 | $d = (array) KTUtil::arrayGet($data, $this->sInputVariable); |
| 435 | 435 | //var_dump($this); exit(0); |
| 436 | 436 | foreach ($this->_validators as $v) { |
| 437 | + if(is_null($v)){ | |
| 438 | + continue; | |
| 439 | + } | |
| 437 | 440 | $res = $v->validate($d); |
| 438 | 441 | |
| 439 | 442 | // results comes out with a set of names and values. |
| ... | ... | @@ -562,4 +565,58 @@ class KTArrayValidator extends KTValidator { |
| 562 | 565 | } |
| 563 | 566 | } |
| 564 | 567 | |
| 568 | +class KTDateValidator extends KTValidator { | |
| 569 | + var $sNamespace = 'ktcore.validators.date'; | |
| 570 | + | |
| 571 | + var $sFormat; | |
| 572 | + var $sFormatWarning; | |
| 573 | + | |
| 574 | + function configure($aOptions) { | |
| 575 | + $res = parent::configure($aOptions); | |
| 576 | + if (PEAR::isError($res)) { | |
| 577 | + return $res; | |
| 578 | + } | |
| 579 | + | |
| 580 | + $this->sFormat = KTUtil::arrayGet($aOptions, 'format', 'YYYY-MM-DD'); | |
| 581 | + | |
| 582 | + $this->sFormatWarning = KTUtil::arrayGet($aOptions, 'format_warning', | |
| 583 | + sprintf(_kt('The date entered must be in the format "%s".'), $this->sFormat)); | |
| 584 | + | |
| 585 | + $this->bTrim = KTUtil::arrayGet($aOptions, 'trim', true, false); | |
| 586 | + } | |
| 587 | + | |
| 588 | + function validate($data) { | |
| 589 | + $results = array(); | |
| 590 | + $errors = array(); | |
| 591 | + | |
| 592 | + // very simple if we're required and not present, fail | |
| 593 | + // otherwise, its ok. | |
| 594 | + $val = KTUtil::arrayGet($data, $this->sInputVariable); | |
| 595 | + | |
| 596 | + if ($this->bTrim) { | |
| 597 | + $val = trim($val); | |
| 598 | + } | |
| 599 | + | |
| 600 | + if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $val, $parts)) | |
| 601 | + { | |
| 602 | + //check weather the date is valid of not | |
| 603 | + if(checkdate($parts[2],$parts[3],$parts[1])){ | |
| 604 | + }else{ | |
| 605 | + $errors[$this->sBasename] = $this->sFormatWarning; | |
| 606 | + } | |
| 607 | + }else { | |
| 608 | + $errors[$this->sBasename] = $this->sFormatWarning; | |
| 609 | + } | |
| 610 | + | |
| 611 | + if ($this->bProduceOutput) { | |
| 612 | + $results[$this->sOutputVariable] = $val; | |
| 613 | + } | |
| 614 | + | |
| 615 | + return array( | |
| 616 | + 'errors' => $errors, | |
| 617 | + 'results' => $results, | |
| 618 | + ); | |
| 619 | + } | |
| 620 | +} | |
| 621 | + | |
| 565 | 622 | ?> | ... | ... |
plugins/ktcore/KTWidgets.php
| ... | ... | @@ -953,4 +953,22 @@ class KTCoreTextAreaWidget extends KTWidget { |
| 953 | 953 | class KTCoreDateWidget extends KTWidget { |
| 954 | 954 | var $sNamespace = 'ktcore.widgets.date'; |
| 955 | 955 | var $sTemplate = 'ktcore/forms/widgets/date'; |
| 956 | + | |
| 957 | + function getValidators() { | |
| 958 | + if (!$this->bAutoValidate) { | |
| 959 | + return null; | |
| 960 | + } | |
| 961 | + $validators = parent::getValidators(); // required, etc. | |
| 962 | + | |
| 963 | + $oVF =& KTValidatorFactory::getSingleton(); | |
| 964 | + | |
| 965 | + $val = array(); | |
| 966 | + if(!empty($validators) && !PEAR::isError($validators)) $val[] = $validators; | |
| 967 | + $val[] = $oVF->get('ktcore.validators.date', array( | |
| 968 | + 'test' => $this->sOrigname, | |
| 969 | + 'basename' => $this->sBasename | |
| 970 | + )); | |
| 971 | + | |
| 972 | + return $val; | |
| 973 | + } | |
| 956 | 974 | } |
| 957 | 975 | \ No newline at end of file | ... | ... |
plugins/ktcore/document/edit.php
| ... | ... | @@ -217,14 +217,14 @@ class KTDocumentEditAction extends KTDocumentAction { |
| 217 | 217 | foreach ($fields as $oField) { |
| 218 | 218 | $val = KTUtil::arrayGet($values, 'metadata_' . $oField->getId()); |
| 219 | 219 | |
| 220 | - if($oField->getDataType() == "LARGE TEXT") | |
| 220 | + if($oField->getDataType() == "LARGE TEXT" && !is_null($oField->getMaxLength())) | |
| 221 | 221 | { |
| 222 | 222 | if(strlen(strip_tags($val)) > $oField->getMaxLength()) |
| 223 | 223 | { |
| 224 | 224 | $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)))); |
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | - | |
| 227 | + | |
| 228 | 228 | // FIXME "null" has strange meanings here. |
| 229 | 229 | if (!is_null($val)) { |
| 230 | 230 | if(KTPluginUtil::pluginIsActive('inet.multiselect.lookupvalue.plugin') && is_array($val) && $oField->getHasInetLookup()) { | ... | ... |