/** * {@inheritDoc} */ public function createService(ServiceLocatorInterface $validators) { $validatorChain = new ValidatorChain(); $validatorChain->attachByName('Callback', ['messages' => [Callback::INVALID_VALUE => 'Termination date must be greater' . ' than the date of employment'], 'callback' => function ($value, $context = []) { if ($value && isset($context['since'])) { $filter = new DateSelectFilter(); return new \DateTime($value) > new \DateTime($filter->filter($context['since'])); } return true; }], true); return $validatorChain; }
/** * {@inheritDoc} */ public function getInputFilterSpecification() { $inputFilterSpec = parent::getInputFilterSpecification(); $spec = ['name' => 'startDate', 'allow_empty' => !!(null === $this->getOption('create_empty_option') ? $this->elements['startDate']->getOption('create_empty_option') : $this->getOption('create_empty_option')), 'required' => true, 'validators' => []]; if (empty($inputFilterSpec['startDate'])) { $inputFilterSpec['startDate'] = $spec; } else { $inputFilterSpec['startDate'] = array_merge($spec, $inputFilterSpec['startDate']); } $spec = ['name' => 'endDate', 'allow_empty' => !!(null === $this->getOption('create_empty_option') ? $this->elements['startDate']->getOption('create_empty_option') : $this->getOption('create_empty_option')), 'required' => true, 'validators' => [['name' => 'Callback', 'options' => ['messages' => [Callback::INVALID_VALUE => 'The end date ' . 'must be later or equal to the start date'], 'callback' => function ($value, $context = []) { $filter = new DateSelectFilter(['null_on_empty' => true]); if ($startDate = $filter->filter($context['startDate'])) { return new DateTime($value) >= new DateTime($startDate); } return false; }, 'break_chain_on_failure' => true]]]]; if (empty($inputFilterSpec['endDate'])) { $inputFilterSpec['endDate'] = $spec; } else { $inputFilterSpec['endDate'] = array_merge($spec, $inputFilterSpec['endDate']); } foreach ($this as $element) { $name = $element->getName(); $minGetter = 'getMin' . ucfirst($name); $maxGetter = 'getMax' . ucfirst($name); if (!method_exists($this, $minGetter) || !method_exists($this, $maxGetter)) { continue; } if ($this->{$minGetter}()) { $inputFilterSpec[$name]['validators'][] = ['name' => 'GreaterThan', 'options' => ['messages' => [GreaterThan::NOT_GREATER_INCLUSIVE => 'The date ' . 'must be not earlier than %min% inclusive'], 'messageVariables' => ['min' => ['abstractOptions' => 'fmt']], 'min' => $this->{$minGetter}()->format('Y-m-d'), 'fmt' => $this->format($this->{$minGetter}()), 'inclusive' => true, 'break_chain_on_failure' => true]]; } if ($this->{$maxGetter}()) { $inputFilterSpec[$name]['validators'][] = ['name' => 'LessThan', 'options' => ['messages' => [LessThan::NOT_LESS_INCLUSIVE => 'The date ' . 'must be not later than %max% inclusive'], 'messageVariables' => ['max' => ['abstractOptions' => 'fmt']], 'max' => $this->{$maxGetter}()->format('Y-m-d'), 'fmt' => $this->format($this->{$maxGetter}()), 'inclusive' => true, 'break_chain_on_failure' => true]]; } } return $inputFilterSpec; }