Skip to content

Commit

Permalink
ID#205: corrected validation for select box tag and others.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Achatz committed Jun 13, 2014
1 parent 511e992 commit a2d655f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 72 deletions.
46 changes: 15 additions & 31 deletions tools/form/taglib/DateSelectorTag.php
Expand Up @@ -20,8 +20,6 @@
* along with the APF. If not, see http://www.gnu.org/licenses/lgpl-3.0.txt.
* -->
*/
use APF\tools\form\taglib\SelectBoxTag;
use APF\tools\form\validator\AbstractFormValidator;

/**
* @package APF\tools\form\taglib
Expand Down Expand Up @@ -69,8 +67,8 @@ public function __construct() {
$this->offsetNames = array('Day' => 'Day', 'Month' => 'Month', 'Year' => 'Year');

// initialize the year range
$this->yearRange['Start'] = (int)date('Y') - 10;
$this->yearRange['End'] = (int)date('Y') + 10;
$this->yearRange['Start'] = (int) date('Y') - 10;
$this->yearRange['End'] = (int) date('Y') + 10;
}

/**
Expand Down Expand Up @@ -155,7 +153,7 @@ public function onParseTime() {
$year->setOption2Selected('');
}

for ($i = (int)$this->yearRange['Start']; $i <= (int)$this->yearRange['End']; $i++) {
for ($i = (int) $this->yearRange['Start']; $i <= (int) $this->yearRange['End']; $i++) {
$yearNumber = sprintf('%04s', $i);
$year->addOption($yearNumber, $yearNumber);
}
Expand Down Expand Up @@ -217,6 +215,7 @@ private function getId() {
if ($id === null) {
return $this->getAttribute('name');
}

return $id;
}

Expand Down Expand Up @@ -262,26 +261,6 @@ public function transform() {
return '';
}

/**
* @public
*
* Re-implements the addValidator() method for the form control due
* to special behavior.
*
* @param AbstractFormValidator $validator The validator to add.
*
* @author Christian Achatz
* @version
* Version 0.1, 29.08.2009<br />
*/
public function addValidator(AbstractFormValidator &$validator) {
if ($validator->isActive()) {
if (!$validator->validate($this->getDate())) {
$validator->notify();
}
}
}

/**
* @public
*
Expand Down Expand Up @@ -314,6 +293,7 @@ public function getDate() {
if ($date === false) {
return null;
}

return $date->format('Y-m-d');
}

Expand Down Expand Up @@ -424,9 +404,9 @@ protected function initOffsetNames() {

if (count($offsetNames) == 3) {
$this->offsetNames = array(
'Day' => $offsetNames[0],
'Month' => $offsetNames[1],
'Year' => $offsetNames[2]
'Day' => $offsetNames[0],
'Month' => $offsetNames[1],
'Year' => $offsetNames[2]
);
}
}
Expand All @@ -438,6 +418,7 @@ protected function initOffsetNames() {
* Appends a zero for month or day numbers without leading zeros.
*
* @param int $input The month or day number.
*
* @return string Month or day number with leading zero.
*
* @author Christian Achatz
Expand Down Expand Up @@ -471,6 +452,7 @@ public function getValue() {
* Re-implements the setting of values for date controls.
*
* @param string $value The date to set (e.g. "2012-04-30").
*
* @return AbstractFormControl This control for further usage.
*
* @since 1.14
Expand All @@ -481,6 +463,7 @@ public function getValue() {
*/
public function setValue($value) {
$this->setDate($value);

return $this;
}

Expand All @@ -497,11 +480,12 @@ private function getTabIndices() {
$indexList = explode(';', $indices);
if (count($indexList) == 3) {
return array(
trim($indexList[0]),
trim($indexList[1]),
trim($indexList[2])
trim($indexList[0]),
trim($indexList[1]),
trim($indexList[2])
);
}

return null;
}

Expand Down
22 changes: 18 additions & 4 deletions tools/form/taglib/SelectBoxTag.php
Expand Up @@ -189,6 +189,7 @@ public function addGroupOptionTag($groupLabel, SelectBoxOptionTag $option) {
* Returns - or lazily creates - a desired option group.
*
* @param string $groupLabel The name of the group.
*
* @return SelectBoxGroupTag The option group.
*
* @author Ralf Schubert
Expand Down Expand Up @@ -218,6 +219,7 @@ public function &getOrCreateGroup($groupLabel) {
// make group available for the subsequent call
$group = & $this->children[$objectId];
}

return $group;
}

Expand Down Expand Up @@ -314,8 +316,8 @@ public function setOption2Selected($displayNameOrValue) {
$this->children[$objectId]->setOption2Selected($displayNameOrValue);
} else {
// bug 981: introduced string-based comparison to avoid pre-select issues with "0".
if ($this->children[$objectId]->getAttribute('value') == (string)$displayNameOrValue
|| $this->children[$objectId]->getContent() == (string)$displayNameOrValue
if ($this->children[$objectId]->getAttribute('value') == (string) $displayNameOrValue
|| $this->children[$objectId]->getContent() == (string) $displayNameOrValue
) {
$this->children[$objectId]->setAttribute('selected', 'selected');
$selectedObjectId = $objectId;
Expand Down Expand Up @@ -375,13 +377,14 @@ public function transform() {

// create html code
if ($this->isVisible) {
$select = (string)'';
$select = (string) '';
$select .= '<select ' . $this->getSanitizedAttributesAsString($this->attributes) . '>';

$this->transformChildren();

return $select . $this->content . '</select>';
}

return '';
}

Expand All @@ -399,7 +402,16 @@ public function transform() {
*/
public function addValidator(AbstractFormValidator &$validator) {

if ($validator->isActive()) {
// ID#166: register validator for further usage.
$this->validators[] = $validator;

// Directly execute validator to allow adding validators within tags and
// document controllers for both static and dynamic form controls.
$value = $this->getValue();

// Check both for validator being active and for mandatory fields to allow optional
// validation (means: field has a registered validator but is sent with empty value).
if ($validator->isActive() && $this->isMandatory($value)) {
$option = & $this->getSelectedOption();
if ($option === null) {
$value = null;
Expand Down Expand Up @@ -476,6 +488,7 @@ public function getValue() {
* Re-implements the setting of values for select controls
*
* @param string $value The display name or the value of the option to pre-select.
*
* @return SelectBoxTag
*
* @since 1.14
Expand All @@ -486,6 +499,7 @@ public function getValue() {
*/
public function setValue($value) {
$this->setOption2Selected($value);

return $this;
}

Expand Down
73 changes: 36 additions & 37 deletions tools/form/taglib/TimeSelectorTag.php
Expand Up @@ -21,9 +21,6 @@
* -->
*/
use APF\tools\form\FormException;
use APF\tools\form\taglib\SelectBoxTag;
use APF\tools\form\taglib\AbstractFormControl;
use APF\tools\form\validator\AbstractFormValidator;

/**
* @package APF\tools\form\taglib
Expand Down Expand Up @@ -78,7 +75,7 @@ public function onParseTime() {
$this->initOffsetNames();

if (!empty($this->attributes['minutesinterval'])) {
$this->minutesInterval = (int)$this->attributes['minutesinterval'];
$this->minutesInterval = (int) $this->attributes['minutesinterval'];
}
if (!empty($this->attributes['showseconds']) && $this->attributes['showseconds'] == "false") {
$this->showSeconds = false;
Expand Down Expand Up @@ -120,7 +117,7 @@ public function onParseTime() {


// set the values for the hours select box
for ($i = (int)$this->hoursRange['Start']; $i <= (int)$this->hoursRange['End']; $i++) {
for ($i = (int) $this->hoursRange['Start']; $i <= (int) $this->hoursRange['End']; $i++) {
$i = $this->appendZero($i);
$hours->addOption($i, $i);
}
Expand Down Expand Up @@ -196,6 +193,7 @@ private function getId() {
if ($id === null) {
return $this->getAttribute('name');
}

return $id;
}

Expand All @@ -217,7 +215,7 @@ public function transform() {
// as of 1.12, the time control should be rendered using a
// surrounding span do enable the client validator extension
// to address the control more easily.
$buffer = (string)'<span id="' . $this->getId() . '"';
$buffer = (string) '<span id="' . $this->getId() . '"';

$style = $this->getAttribute('style');
if ($style != null) {
Expand All @@ -239,26 +237,6 @@ public function transform() {
return '';
}

/**
* @public
*
* Re-implements the addValidator() method for the form control due
* to special behavior.
*
* @param AbstractFormValidator $validator The validator to add.
*
* @author Werner Liemberger
* @version
* Version 0.1, 21.2.2011<br />
*/
public function addValidator(AbstractFormValidator &$validator) {
if ($validator->isActive()) {
if (!$validator->validate($this->getTime())) {
$validator->notify();
}
}
}

/**
* @public
*
Expand All @@ -271,13 +249,30 @@ public function addValidator(AbstractFormValidator &$validator) {
* Version 0.1, 21.2.2011<br />
*/
public function getTime() {
$hours = $this->getHoursControl()->getSelectedOption()->getAttribute('value');
$minutes = $this->getMinutesControl()->getSelectedOption()->getAttribute('value');
if ($this->showSeconds != false) {
$seconds = $this->getSecondsControl()->getSelectedOption()->getAttribute('value');
return $hours . ':' . $minutes . ':' . $seconds;

$hours = $this->getHoursControl()->getSelectedOption();
$minutes = $this->getMinutesControl()->getSelectedOption();

if ($this->showSeconds === false) {

// in case any of the select boxes are missing a none-empty selection, the time is null
if ($hours === null || $minutes === null) {
return null;
}

return $hours->getValue() . ':' . $minutes->getValue();

} else {
$seconds = $this->getSecondsControl()->getSelectedOption();

// in case any of the select boxes are missing a none-empty selection, the time is null
if ($hours === null || $minutes === null || $seconds === null) {
return null;
}

return $hours->getValue() . ':' . $minutes->getValue() . ':' . $seconds->getValue();
}
return $hours . ':' . $minutes;

}

/**
Expand All @@ -286,6 +281,7 @@ public function getTime() {
* Allows you to initialize the time control with a given time (e.g. "08:31" or "08:31:20" or "2011-02-21 08:31:00").
*
* @param string $time The time to initialize the control with.
*
* @throws FormException In case of date parsing errors.
*
* @author Christian Achatz
Expand Down Expand Up @@ -393,15 +389,15 @@ protected function initOffsetNames() {

if (count($offsetNames) == 3) {
$this->offsetNames = array(
'Hours' => $offsetNames[0],
'Minutes' => $offsetNames[1],
'Seconds' => $offsetNames[2]
'Hours' => $offsetNames[0],
'Minutes' => $offsetNames[1],
'Seconds' => $offsetNames[2]
);
}
if (count($offsetNames) == 2) {
$this->offsetNames = array(
'Hours' => $offsetNames[0],
'Minutes' => $offsetNames[1]
'Hours' => $offsetNames[0],
'Minutes' => $offsetNames[1]
);
}
}
Expand All @@ -413,6 +409,7 @@ protected function initOffsetNames() {
* Appends a zero for hours, minutes or seconds numbers without leading zeros.
*
* @param int $input The hour, minute or second number.
*
* @return string Hour, minute or second number with leading zero.
*
* @author Werner Liemberger
Expand Down Expand Up @@ -446,6 +443,7 @@ public function getValue() {
* Re-implements the setting of values for time controls
*
* @param string $value
*
* @return AbstractFormControl
*
* @since 1.14
Expand All @@ -456,6 +454,7 @@ public function getValue() {
*/
public function setValue($value) {
$this->setTime($value);

return $this;
}

Expand Down

0 comments on commit a2d655f

Please sign in to comment.