/** * @return void */ public function testInvalidValueResultsInProperValidationFailureMessages() { $this->assertFalse($this->validator->isValid('#')); $messages = $this->validator->getMessages(); $arrayExpected = array(Digits::NOT_DIGITS => 'The input must contain only digits'); $this->assertThat($messages, $this->identicalTo($arrayExpected)); }
/** * {@inheritDoc} * @param mixed $value * @return int|mixed */ public function filter($value) { // Value is already an integer ; nothing to do if (is_int($value)) { return $value; } if (null === static::$validatorDigits) { static::$validatorDigits = new ZendValidator\Digits(); } if (!static::$validatorDigits->isValid($value)) { throw new Exception\InvalidArgumentException('Cannot filter Octal value', self::NOT_DIGITS, new ZendValidator\Exception\InvalidArgumentException(implode(' ; ', static::$validatorDigits->getMessages()))); } if (null === static::$validatorOctal) { static::$validatorOctal = new FileManagerValidator\Octal(); } if (!static::$validatorOctal->isValid($value)) { throw new Exception\InvalidArgumentException('Cannot filter Octal value', self::NOT_OCTAL, new Exception\InvalidArgumentException(implode(' ; ', static::$validatorOctal->getMessages()))); } return intval($value, 8); }
/** * @group ZF-11267 * If we pass in a validator instance that has a preset custom message, this * message should be used. */ function testIfCustomMessagesOnValidatorInstancesCanBeUsed() { // test with a Digits validator $data = array('field1' => 'invalid data'); $customMessage = 'Hey, that\'s not a Digit!!!'; $validator = new Validator\Digits(); $validator->setMessage($customMessage, 'notDigits'); $this->assertFalse($validator->isValid('foo'), 'standalone validator thinks \'foo\' is a valid digit'); $messages = $validator->getMessages(); $this->assertSame($messages['notDigits'], $customMessage, 'stanalone validator does not have custom message'); $validators = array('field1' => $validator); $input = new InputFilter(null, $validators, $data); $this->assertFalse($input->isValid(), 'invalid input is valid'); $messages = $input->getMessages(); $this->assertSame($messages['field1']['notDigits'], $customMessage, 'The custom message is not used'); // test with a NotEmpty validator $data = array('field1' => ''); $customMessage = 'You should really supply a value...'; $validator = new Validator\NotEmpty(); $validator->setMessage($customMessage, 'isEmpty'); $this->assertFalse($validator->isValid(''), 'standalone validator thinks \'\' is not empty'); $messages = $validator->getMessages(); $this->assertSame($messages['isEmpty'], $customMessage, 'stanalone NotEmpty validator does not have custom message'); $validators = array('field1' => $validator); $input = new InputFilter(null, $validators, $data); $this->assertFalse($input->isValid(), 'invalid input is valid'); $messages = $input->getMessages(); $this->assertSame($messages['field1']['isEmpty'], $customMessage, 'For the NotEmpty validator the custom message is not used'); }