/** * Checks that if both fields are present and value, time period meets spec. * * @param T_Form_Group $value collection to examine * @return void * @throws T_Exception_Filter if time period is not value */ protected function doTransform($value) { $start = $value->search($this->start); $end = $value->search($this->end); if (!$start || !$end) { $msg = "does not contain {$this->start} or {$this->end}"; throw new InvalidArgumentException($msg); } // if either field already has errors, or if not present, // no validation is required. if (!$start->isValid() || !$start->isPresent() || !$end->isValid() || !$end->isPresent()) { return; } $period = $end->getValue() - $start->getValue(); if ($period < 0) { $msg = "{$end->getLabel()} must be after {$start->getLabel()}"; throw new T_Exception_Filter($msg); } if (!is_null($this->min) && $period < $this->min) { $f = new T_Filter_HumanTimePeriod(); $msg = "The period between {$start->getLabel()} and {$end->getLabel()} must be at least " . _transform($this->min, $f); } if (!is_null($this->max) && $period > $this->max) { $f = new T_Filter_HumanTimePeriod(); $msg = "The period between {$start->getLabel()} and {$end->getLabel()} may be a maximum of " . _transform($this->max, $f); } }
/** * Checks that if the master is present, so too is the confirmation. * * @param T_Form_Group $value collection to examine * @return void * @throws T_Exception_Filter when data > max length */ protected function doTransform($value) { $master = $value->search($this->master); $confirm = $value->search($this->confirm); if (!$master || !$confirm) { $msg = "does not contain {$this->master} or {$this->confirm}"; throw new InvalidArgumentException($msg); } // if either field already has errors, let these be corrected first ... if (!$master->isValid() || !$confirm->isValid()) { return; } // ... otherwise check the values are equal if ($confirm->getValue() != $master->getValue()) { $msg = 'does not match the value in ' . $master->getLabel(); $confirm->setError(new T_Form_Error($msg)); } }
/** * Checks that if both are present, end int is > start int. * * @param T_Form_Group $value collection to examine * @return void * @throws T_Exception_Filter when end date not correct */ protected function doTransform($value) { $start = $value->search($this->start); $end = $value->search($this->end); if ($start === false || false === $end) { $msg = "does not contain {$this->start} and {$this->end}"; throw new InvalidArgumentException($msg); } if (!$start->isValid() || !$end->isValid()) { return; } if ($start->isPresent() && $end->isPresent()) { if ($start->getValue() > $end->getValue()) { $msg = 'must be larger than ' . $start->getLabel(); $end->setError(new T_Form_Error($msg)); } } }
/** * Create repeatable group. * * @param string $alias * @param string $label * @param int $min minimum number that must be submitted * @param int $max maximum */ function __construct($alias, $label, $min, $max) { parent::__construct($alias, $label); $this->min = (int) $min; for ($i = 1; $i <= $max; $i++) { $this->children[] = new T_Form_Group($alias . "_{$i}", $label); } if (count($this->children) == 0) { throw new RuntimeException("maximum repetion {$max} must be greater than 0"); } }
/** * Checks that if any fields are present, all are present. * * @param T_Form_Group $value collection to process * @return void */ protected function doTransform($value) { $fields = array(); $any_present = false; foreach ($this->fields as $name) { $f = $value->search($name); if (!$f) { $msg = "does not contain {$name} field"; throw new InvalidArgumentException($msg); } if (!$any_present) { $any_present = $f->isPresent(); } $fields[] = $f; } if ($any_present) { foreach ($fields as $f) { if ($f->isValid() && !$f->isPresent()) { $f->setError(new T_Form_Error('is missing')); } } } }
/** * Whether any elements in form are present (inc actions). * * @return bool whether any elements are present */ function isPresent() { foreach ($this->action as $button) { if ($button->isPresent()) { return true; } } return parent::isPresent(); }