/** * Handles requested "/" route */ public function indexAction() { // handle contact form if (!empty($_POST['form_submit'])) { $formValidator = new FormValidator($_POST['form'], $this->view); // if there were no errors if ($formValidator->isValid()) { // format user submitted data $data = array(); $data[] = '<table>'; foreach ($_POST['form'] as $field => $value) { $data[] = '<tr><th>' . ucwords(str_replace(array('-', '_'), ' ', $field)) . '</th><td>' . nl2br($value) . '</td></tr>'; } $data[] = '</table>'; // send message $mailer = new Mailer(); $result = $mailer->sendSystemMessage(implode("\n", $data), $_POST['form_submit']); if ($result) { $this->router->redirect('/thanks'); die; } } else { $this->errorMessages = $formValidator->getFormattedErrors(); } } }
/** * @see Validator::isValid() * @param $value mixed * @return boolean */ function isValid() { // check if generally formatted as a date and if required if (!parent::isValid()) { return false; } // if parent::isValid is true and $value is empty, this value is optional $fieldValue = $this->getFieldValue(); if (!$fieldValue) { return true; } $validator = parent::getValidator(); return $validator->isValid($fieldValue, $this->_scopeMin, $this->_scopeMax); }
/** * @covers FormValidator::isValid */ public function testIsValid() { // We don't need to test the case where a validator is set, this // is sufficiently tested by the other FormValidator* tests. // Test default validation (without internal validator set and optional values) $formValidator = new FormValidator($this->form, 'testData', FORM_VALIDATOR_OPTIONAL_VALUE, 'some.message.key'); self::assertTrue($formValidator->isValid()); // Test default validation (without internal validator set and required values) $formValidator = new FormValidator($this->form, 'testData', FORM_VALIDATOR_REQUIRED_VALUE, 'some.message.key'); self::assertFalse($formValidator->isValid()); $this->form->setData('testData', array()); $formValidator = new FormValidator($this->form, 'testData', FORM_VALIDATOR_REQUIRED_VALUE, 'some.message.key'); self::assertFalse($formValidator->isValid()); $this->form->setData('testData', 'some value'); $formValidator = new FormValidator($this->form, 'testData', FORM_VALIDATOR_REQUIRED_VALUE, 'some.message.key'); self::assertTrue($formValidator->isValid()); $this->form->setData('testData', array('some value')); $formValidator = new FormValidator($this->form, 'testData', FORM_VALIDATOR_REQUIRED_VALUE, 'some.message.key'); self::assertTrue($formValidator->isValid()); }