public function __construct($name = null) { $validator = new Validator\Digits(array('min' => 0)); $validator->setMessages(array(Validator\Digits::INVALID => "Значение обязательное и не может быть пустым", Validator\Digits::NOT_DIGITS => "Значение обязательное и не может быть пустым", Validator\Digits::STRING_EMPTY => "Значение обязательное и не может быть пустым")); $this->getValidatorChain()->attach($validator); parent::__construct($name); }
public function indexAction() { if (isset($_POST['username'])) { $data = []; $request = $this->getRequest(); $username = $request->getPost('username'); $age = $request->getPost('age'); $emailAddress = $request->getPost('email'); $digitsValidator = new Digits(); $alphaValidator = new Alpha(); $emailValidator = new EmailAddress(); $data['age']['value'] = $age; $data['username']['value'] = $username; $data['email']['value'] = $emailAddress; if ($digitsValidator->isValid($age)) { $data['age']['message'] = 'Age = ' . $age . ' years old'; } else { $data['age']['message'] = 'Age value invalid!'; } if ($alphaValidator->isValid($username)) { $data['username']['message'] = 'Username = '******'username']['message'] = 'Username value invalid!'; } if ($emailValidator->isValid($emailAddress)) { $data['email']['message'] = 'Email Address = ' . $emailAddress; } else { $data['email']['message'] = 'Email Address value invalid!'; } $data['message'] = 'success'; } return new ViewModel($data); }
/** * Validates if input is a number. * * @param $input mixed Input for validator. * @return bool Returns true if validation passed. * @throws ValidatorException if $input is not a number. */ protected function validateDigit($input) { $validator = new Digits(); if (!$validator->isValid($input)) { throw new ValidatorException("Validation failed: {$input} is not a digit in function " . debug_backtrace()[1]['function'] . "."); } return true; }
/** * {@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); }
/** * @return bool */ public function validate() { $this->validateFields($this->getFieldsForValidation(), $this->getData()); $data = $this->getData(); $notEmpty = new NotEmpty(); $stringLength = new StringLength(); $digits = new Digits(); if (!$notEmpty->isValid($data['name'])) { $this->addError('Name', Validator::ERROR_NOT_EMPTY); } if (!$stringLength->isValid($data['name'])) { $this->addError('Name', Validator::ERROR_NOT_STRING); } if (!$notEmpty->isValid($data['amount'])) { $this->addError('Amount', Validator::ERROR_NOT_EMPTY); } if (!is_float($data['amount'])) { $this->addError('Amount', Validator::ERROR_NOT_AMOUNT); } if (!$notEmpty->isValid($data['quantity'])) { $this->addError('Quantity', Validator::ERROR_NOT_EMPTY); } if (!$digits->isValid($data['quantity'])) { $this->addError('Quantity', Validator::ERROR_NOT_INTEGER); } if (!$notEmpty->isValid($data['uniqueId'])) { $this->addError('uniqueId', Validator::ERROR_NOT_EMPTY); } if (!$stringLength->isValid($data['uniqueId'])) { $this->addError('uniqueId', Validator::ERROR_NOT_STRING); } // Optional field: Currency if (isset($data['currency'])) { if (!$stringLength->isValid($data['currency'])) { $this->addError('currency', Validator::ERROR_NOT_STRING); } } return $this->confirmValidation(); }
private function validarCampo($valorCampo, array $outrosCampos = array()) { if (isset($outrosCampos['repetir']) && $outrosCampos['repetir'] == 'S') { if (empty($valorCampo)) { $this->error(self::MSG_DESPESA_REPETIR_OCORRENCIA_REQUERIDA); return false; } } elseif (isset($outrosCampos['repetir']) && $outrosCampos['repetir'] == 'N') { if (!empty($valorCampo)) { $this->error(self::MSG_DESPESA_REPETIR_OCORRENCIA_NAO_INFORMAR); return false; } } if (!empty($valorCampo)) { $digitos = new Digits(); if (!$digitos->isValid($valorCampo)) { $this->error(self::MSG_DESPESA_REPETIR_OCORRENCIA_VALOR_INVALIDO); return false; } } return true; }
/** * @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'); }
/** * @ZF-4352 */ public function testNonStringValidation() { $this->assertFalse($this->validator->isValid(array(1 => 1))); }
/** * Check to make sure the networkUserId is valid * * @param String $networkName * @param String $networkUserId * @throws InvalidArgumentException */ protected function assertValidUserIdForNetwork($networkName, $networkUserId) { $this->assertValidNetworkName($networkName); switch ($networkName) { case self::NETWORK_GOOGLE_PLUS: case self::NETWORK_KLOUT: case self::NETWORK_TWITTER_ID: case self::NETWORK_INSTAGRAM: // These networks only allow numeric userIds $validator = new ValidatorDigits(); if (!$validator->isValid($networkUserId)) { throw new InvalidArgumentException("'{$networkUserId}'" . ' is not a valid network user ID.'); } break; default: if (!preg_match('/^[A-Za-z0-9]*$/', $networkUserId)) { throw new InvalidArgumentException("'{$networkUserId}'" . ' is not a valid network user ID.'); } break; } }