예제 #1
0
 /**
  * 取错误信息
  * @param string $filter
  * @return array
  */
 public function getMessages($filter = '')
 {
     if (is_string($filter) && !empty($filter)) {
         $filtered = new MessageGroup();
         foreach ($this->errorMessages as $message) {
             if (is_object($message) && method_exists($message, 'getField')) {
                 if ($message->getField() == $filter) {
                     $filtered->appendMessage($message);
                 }
             }
         }
         return $filtered;
     }
     return $this->errorMessages;
 }
예제 #2
0
파일: Herald.php 프로젝트: rj28/test
 /** @return bool */
 public static function queue($data)
 {
     $payload = json_encode($data);
     $length = strlen($payload);
     $context = stream_context_create(['http' => ['header' => implode("\r\n", ["Host: " . static::_config()['host'], "Project-Key: " . sha1(static::_config()['project'] . sha1(static::_config()['api_key'])), "Content-Type: multipart/form-data", "Content-Length: " . $length, ""]), 'method' => 'POST', 'content' => $payload, 'timeout' => 2]]);
     $url = 'http://' . static::_host() . '/api01/' . static::_config()['project'] . '/mailing/queue';
     $content = json_decode($raw = file_get_contents($url, null, $context), true);
     if (!empty($content['messages'])) {
         $group = new Group();
         foreach ($content['messages'] as $row) {
             $group->appendMessage(new Message($row['text'], empty($row['field']) ? '' : $row['field'], empty($row['type']) ? '' : $row['type']));
         }
         return $group;
     }
     return !empty($content['result']) && 'ok' === $content['result'];
 }
예제 #3
0
 /**
  * Appends a message to the messages list
  *
  * @param \Phalcon\Validation\MessageInterface $message
  * @return \Phalcon\Validation
  */
 public function appendMessage($message)
 {
     //@note the type check is not implemented in the original source code
     if (is_null($this->_messages) === false) {
         $this->_messages->appendMessage($message);
     }
     return $this;
 }
예제 #4
0
 /**
  * Tests the get
  *
  * @issue  10405
  * @author Serghei Iakovlev <*****@*****.**>
  * @since  2016-06-27
  * @param IntegrationTester $I
  */
 public function appendValidationMessageToTheNonObject(IntegrationTester $I)
 {
     $myValidator = new PresenceOf();
     $validation = new Validation();
     $validation->bind(new \stdClass(), ['day' => date('d'), 'month' => date('m'), 'year' => date('Y') + 1]);
     $myValidator->validate($validation, 'foo');
     $expectedMessages = Group::__set_state(['_position' => 0, '_messages' => [new Message('Field foo is required', 'foo', 'PresenceOf', 0)]]);
     $I->assertEquals($expectedMessages, $validation->getMessages());
 }
예제 #5
0
 /**
  * Tests Non Alphabetic Characters
  *
  * @author Serghei Iakovlev <*****@*****.**>
  * @since  2016-06-10
  */
 public function testShouldDetectNonAlphabeticCharacters()
 {
     $this->specify("The Alpha Validator does not detect non alphabetic characters", function ($input) {
         $validation = new Validation();
         $validation->add('name', new Alpha(['message' => ':field must contain only letters']));
         $messages = $validation->validate(['name' => $input]);
         $expectedMessages = Group::__set_state(['_messages' => [Message::__set_state(['_type' => 'Alpha', '_message' => 'name must contain only letters', '_field' => 'name', '_code' => '0'])]]);
         expect($messages)->equals($expectedMessages);
     }, ['examples' => [['1'], [123], ['a-b-c-d'], ['a-1-c-2'], ['a1c2'], ['o0o0o0o0']]]);
 }
예제 #6
0
 /**
  * Get form notices.
  *
  * @param string|null $field Field name
  *
  * @return array
  */
 public function getNotices($field = null)
 {
     if ($field) {
         $notices = $this->_notices->filter($field);
     } else {
         $notices = iterator_to_array($this->_notices);
     }
     foreach ($this->getFieldSets() as $fieldSet) {
         $notices += $fieldSet->getNotices($field);
     }
     return $notices;
 }
예제 #7
0
 public function testCustomMessage()
 {
     $this->specify('Test Between validator works with a custom message.', function () {
         $validation = new Validation();
         $validation->add('price', new Between(['minimum' => 1, 'maximum' => 3, 'message' => 'The price must be between 1 and 3']));
         $messages = $validation->validate(['price' => 5]);
         $expectedMessages = Validation\Message\Group::__set_state(['_messages' => [0 => Validation\Message::__set_state(['_type' => 'Between', '_message' => 'The price must be between 1 and 3', '_field' => 'price', '_code' => '0'])]]);
         $this->assertEquals($expectedMessages, $messages);
         $messages = $validation->validate([]);
         expect($expectedMessages)->equals($messages);
         $messages = $validation->validate(['price' => 2]);
         expect($messages)->count(0);
     });
 }
예제 #8
0
 public function testCustomMessage()
 {
     $this->specify('Test Email validator works with a custom message.', function () {
         $validation = new Validation();
         $validation->add('email', new Email(['message' => 'The email is not valid']));
         $messages = $validation->validate([]);
         $expectedMessages = Validation\Message\Group::__set_state(['_messages' => [0 => Validation\Message::__set_state(['_type' => 'Email', '_message' => 'The email is not valid', '_field' => 'email', '_code' => '0'])]]);
         $this->assertEquals($expectedMessages, $messages);
         $messages = $validation->validate(['email' => 'x=1']);
         expect($expectedMessages)->equals($messages);
         $messages = $validation->validate(['email' => '*****@*****.**']);
         expect($messages)->count(0);
     });
 }
예제 #9
0
 public function testCustomMessage()
 {
     $this->specify('Test URL validator works with a custom message.', function () {
         $validation = new Validation();
         $validation->add('url', new Validation\Validator\Url(['message' => 'The url is not valid']));
         $messages = $validation->validate([]);
         $expectedMessages = Validation\Message\Group::__set_state(['_messages' => [0 => Validation\Message::__set_state(['_type' => 'Url', '_message' => 'The url is not valid', '_field' => 'url', '_code' => '0'])]]);
         expect($expectedMessages)->equals($messages);
         $messages = $validation->validate(['url' => 'x=1']);
         expect($expectedMessages)->equals($messages);
         $messages = $validation->validate(['url' => 'http://phalconphp.com']);
         expect($messages)->count(0);
     });
 }
예제 #10
0
 /**
  * Tests presence of validator with multiple field
  *
  * @author Wojciech Ślawski <*****@*****.**>
  * @since  2016-06-05
  */
 public function testMultipleField()
 {
     $this->specify('Test presence of validator with multiple field.', function () {
         $validation = new Validation();
         $validationMessages = ['name' => 'Name cant be empty.', 'type' => 'Type cant be empty.'];
         $validation->add(['name', 'type'], new PresenceOf(['message' => $validationMessages]));
         $messages = $validation->validate(['name' => 'SomeValue', 'type' => 'SomeValue']);
         expect($messages->count())->equals(0);
         $messages = $validation->validate(['name' => '', 'type' => 'SomeValue']);
         expect($messages->count())->equals(1);
         expect($messages->offsetGet(0)->getMessage())->equals($validationMessages['name']);
         $expectedMessages = Group::__set_state(['_messages' => [Message::__set_state(['_type' => 'PresenceOf', '_message' => 'Name cant be empty.', '_field' => 'name', '_code' => '0'])]]);
         expect($messages)->equals($expectedMessages);
         $messages = $validation->validate(['name' => '', 'type' => '']);
         expect($messages->count())->equals(2);
         expect($messages->offsetGet(0)->getMessage())->equals($validationMessages['name']);
         expect($messages->offsetGet(1)->getMessage())->equals($validationMessages['type']);
         $expectedMessages = Group::__set_state(['_messages' => [Message::__set_state(['_type' => 'PresenceOf', '_message' => 'Name cant be empty.', '_field' => 'name', '_code' => '0']), Message::__set_state(['_type' => 'PresenceOf', '_message' => 'Type cant be empty.', '_field' => 'type', '_code' => '0'])]]);
         expect($messages)->equals($expectedMessages);
     });
 }
예제 #11
0
 /**
  * Appends a message to the internal message list
  *
  * @param \Phalcon\Validation\Message $message
  * @return \Phalcon\Forms\ElementInterface
  * @throws Exception
  */
 public function appendMessage($message)
 {
     if (is_object($message) === false || $message instanceof Message === false) {
         throw new Exception('Invalid parameter type.');
     }
     if (is_object($this->_messages) === false) {
         $this->_messages = new Group();
     }
     $this->_messages->appendMessage($message);
     return $this;
 }
예제 #12
0
 public function testCustomMessage()
 {
     $this->specify('Test Identical validator works with a custom message.', function () {
         $validation = new Validation();
         $validation->add('name', new Validation\Validator\Identical(['accepted' => 'Peter', 'message' => 'The name must be peter']));
         $messages = $validation->validate([]);
         $expectedMessages = Validation\Message\Group::__set_state(['_messages' => [0 => Validation\Message::__set_state(['_type' => 'Identical', '_message' => 'The name must be peter', '_field' => 'name', '_code' => '0'])]]);
         expect($expectedMessages)->equals($messages);
         $messages = $validation->validate(['name' => 'Peter']);
         expect($messages)->count(0);
     });
 }
예제 #13
0
 /**
  * Returns the messages generated in the validation
  *
  * @param boolean|null $byItemName
  * @return array
  * @throws Exception
  */
 public function getMessages($byItemName = null)
 {
     if (is_null($byItemName) === true) {
         $byItemName = false;
     } elseif (is_bool($byItemName) === false) {
         throw new Exception('Invalid parameter type.');
     }
     $messages = $this->_messages;
     if ($byItemName === true) {
         if (is_array($messages) === false) {
             return new Group();
         }
         return $messages;
     }
     $g = new Group();
     if (is_array($messages) === true) {
         foreach ($messages as $message) {
             $g->appendMessages($message);
         }
     }
     return $g;
 }
예제 #14
0
 public function testValidationSetLabels()
 {
     $this->specify("setLabels() doesn't work as expected", function () {
         $validation = new Validation();
         $validation->add('email', new Validation\Validator\PresenceOf(array('message' => 'The :field is required')));
         $validation->add('email', new Validation\Validator\Email(array('message' => 'The :field must be email', 'label' => 'E-mail')));
         $validation->add('firstname', new Validation\Validator\PresenceOf(array('message' => 'The :field is required')));
         $validation->add('firstname', new Validation\Validator\StringLength(array('min' => 4, 'messageMinimum' => 'The :field is too short')));
         $validation->setLabels(array('firstname' => 'First name'));
         $messages = $validation->validate(['email' => '', 'firstname' => '']);
         $expectedMessages = Group::__set_state(array('_messages' => array(0 => Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The email is required', '_field' => 'email', '_code' => '0')), 1 => Message::__set_state(array('_type' => 'Email', '_message' => 'The E-mail must be email', '_field' => 'email', '_code' => '0')), 2 => Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The First name is required', '_field' => 'firstname', '_code' => '0')), 3 => Message::__set_state(array('_type' => 'TooShort', '_message' => 'The First name is too short', '_field' => 'firstname', '_code' => '0')))));
         expect($messages)->equals($expectedMessages);
     });
 }
 /**
  * Returns formatted message validation
  * In this validation we put messages on exception
  * 
  * @param array $data
  * @param stdClass $entity
  * @param \Phalcon\Validation\Message\Group $messages
  *
  * @throws \Cronmonitor\Core\Entity\ValidationException
  */
 public function afterValidation(array $data, $entity = null, $messages)
 {
     if ($messages->count() > 0) {
         throw new ValidationException($messages);
     }
 }
예제 #16
0
 /**
  * Tests clearing the Form Elements and using Form::isValid
  *
  * @issue  11978
  * @author Serghei Iakovlev <*****@*****.**>
  * @since  2016-10-01
  * @param  IntegrationTester $I
  */
 public function clearFormElementsAndUsingValidation(IntegrationTester $I)
 {
     $password = new Password('password', ['placeholder' => 'Insert your Password']);
     $password->addValidators([new PresenceOf(['message' => 'The field is required', 'cancelOnFail' => true]), new StringLength(['min' => 7, 'messageMinimum' => 'The text is too short'])]);
     $form = new Form();
     $form->add($password);
     $I->assertNull($form->get('password')->getValue());
     $input = '<input type="password" id="password" name="password" placeholder="Insert your Password">';
     $I->assertEquals($input, $form->render('password'));
     $_POST = ['password' => 'secret'];
     $I->assertEquals('secret', $form->get('password')->getValue());
     $input = '<input type="password" id="password" name="password" value="secret" placeholder="Insert your Password">';
     $I->assertEquals($input, $form->render('password'));
     $I->assertFalse($form->isValid($_POST));
     $actual = $form->getMessages();
     $expected = Group::__set_state(['_position' => 0, '_messages' => [Message::__set_state(['_type' => 'TooShort', '_message' => 'The text is too short', '_field' => 'password', '_code' => '0'])]]);
     $I->assertEquals($actual, $expected);
     $form->clear(['password']);
     $I->assertNull($form->get('password')->getValue());
     $input = '<input type="password" id="password" name="password" placeholder="Insert your Password">';
     $I->assertEquals($input, $form->render('password'));
     $I->assertEquals(['password' => 'secret'], $_POST);
 }
예제 #17
0
 public function testFormValidator()
 {
     $this->specify("Form validators don't work", function () {
         //First element
         $telephone = new Text("telephone");
         $telephone->addValidator(new PresenceOf(array('message' => 'The telephone is required')));
         expect($telephone->getValidators())->count(1);
         $telephone->addValidators(array(new StringLength(array('min' => 5, 'messageMinimum' => 'The telephone is too short')), new Regex(array('pattern' => '/\\+44 [0-9]+ [0-9]+/', 'message' => 'The telephone has an invalid format'))));
         expect($telephone->getValidators())->count(3);
         //Second element
         $address = new Text('address');
         $address->addValidator(new PresenceOf(array('message' => 'The address is required')));
         expect($address->getValidators())->count(1);
         $form = new Form();
         $form->add($telephone);
         $form->add($address);
         expect($form->isValid([]))->false();
         $expectedMessages = Group::__set_state(array('_messages' => array(0 => Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The telephone is required', '_field' => 'telephone', '_code' => 0)), 1 => Message::__set_state(array('_type' => 'TooShort', '_message' => 'The telephone is too short', '_field' => 'telephone', '_code' => 0)), 2 => Message::__set_state(array('_type' => 'Regex', '_message' => 'The telephone has an invalid format', '_field' => 'telephone', '_code' => 0)), 3 => Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The address is required', '_field' => 'address', '_code' => 0)))));
         expect($form->getMessages())->equals($expectedMessages);
         expect($form->isValid(array('telephone' => '12345', 'address' => 'hello')))->false();
         $expectedMessages = Group::__set_state(array('_messages' => array(0 => Message::__set_state(array('_type' => 'Regex', '_message' => 'The telephone has an invalid format', '_field' => 'telephone', '_code' => 0)))));
         expect($form->getMessages())->equals($expectedMessages);
         expect($form->isValid(array('telephone' => '+44 124 82122', 'address' => 'hello')))->true();
     });
 }
예제 #18
0
 public function testCustomMessage()
 {
     $this->specify('Test Exclusion In validator works with a custom message.', function () {
         $validation = new Validation();
         $validation->add('status', new ExclusionIn(['message' => 'The status must not be A=Active or I=Inactive', 'domain' => ['A', 'I']]));
         $messages = $validation->validate(['status' => 'A']);
         $expectedMessages = Validation\Message\Group::__set_state(['_messages' => [0 => Validation\Message::__set_state(['_type' => 'ExclusionIn', '_message' => 'The status must not be A=Active or I=Inactive', '_field' => 'status', '_code' => '0'])]]);
         expect($expectedMessages)->equals($messages);
         $messages = $validation->validate(['status' => 'A']);
         expect($expectedMessages)->equals($messages);
         $messages = $validation->validate(['status' => 'X']);
         expect($messages)->count(0);
     });
 }
예제 #19
0
파일: Group.php 프로젝트: mattvb91/cphalcon
 public static function __set_state($group)
 {
     return parent::__set_state($group);
 }
예제 #20
0
 /**
  * Tests file validator with multiple field
  *
  * @author Wojciech Ślawski <*****@*****.**>
  * @since  2016-10-23
  */
 public function testMultipleField()
 {
     $this->specify('The file validator does not validates correctly with multiple field', function () {
         $validation = new Validation();
         $validation->add(['file', 'anotherFile'], new File(['maxSize' => ['file' => '500K', 'anotherFile' => '600K'], 'allowedTypes' => ['image/jpeg', 'image/png'], 'maxResolution' => ['file' => '800x800', 'anotherFile' => '900x900'], 'minResolution' => '1x1', 'message' => ['file' => 'Image should have max 800x800 resolution', 'anotherFile' => 'Image should have max 900x900 resolution']]));
         $messages = $validation->validate(['file' => $this->files[2], 'anotherFile' => $this->files[2]]);
         expect($messages->count())->equals(0);
         $messages = $validation->validate(['file' => $this->files[2], 'anotherFile' => $this->files[3]]);
         expect($messages->count())->equals(1);
         $expectedMessages = Validation\Message\Group::__set_state(['_messages' => [0 => Validation\Message::__set_state(['_type' => 'File', '_message' => 'Image should have max 900x900 resolution', '_field' => 'anotherFile', '_code' => '0'])]]);
         expect($expectedMessages)->equals($messages);
         $messages = $validation->validate(['file' => $this->files[3], 'anotherFile' => $this->files[3]]);
         expect($messages->count())->equals(2);
         $expectedMessages = Validation\Message\Group::__set_state(['_messages' => [0 => Validation\Message::__set_state(['_type' => 'File', '_message' => 'Image should have max 800x800 resolution', '_field' => 'file', '_code' => '0']), 1 => Validation\Message::__set_state(['_type' => 'File', '_message' => 'Image should have max 900x900 resolution', '_field' => 'anotherFile', '_code' => '0'])]]);
         expect($expectedMessages)->equals($messages);
     });
 }
예제 #21
0
 public function testCustomMessage()
 {
     $this->specify('Test Regex validator works with a custom message.', function () {
         $validation = new Validation();
         $validation->add('car_plate', new Validation\Validator\Regex(['pattern' => '/[A-Z]{3}\\-[0-9]{3}/', 'message' => 'The car plate is not valid']));
         $messages = $validation->validate([]);
         $expectedMessages = Validation\Message\Group::__set_state(['_messages' => [0 => Validation\Message::__set_state(['_type' => 'Regex', '_message' => 'The car plate is not valid', '_field' => 'car_plate', '_code' => '0'])]]);
         expect($expectedMessages)->equals($messages);
         $messages = $validation->validate(['car_plate' => 'XYZ-123']);
         expect($messages)->count(0);
     });
 }