예제 #1
0
 /**
  * Executed before validation
  *
  * @param array                            $data
  * @param object                           $entity
  * @param Phalcon\Validation\Message\Group $messages
  */
 public function beforeValidation($data, $entity, $messages)
 {
     if ($this->request->getHttpHost() != 'admin.mydomain.com') {
         $messages->appendMessage(new Message('Users only can log on in the administration domain'));
         return false;
     }
     return true;
 }
예제 #2
0
 /**
  * Sign in Action
  *
  * @package     las
  * @version     1.0
  */
 public function signinAction()
 {
     if ($this->request->hasPost('submit_signin') && $this->request->hasPost('username') && $this->request->hasPost('password')) {
         $login = Auth::instance()->login($this->request->getPost('username'), $this->request->getPost('password'), $this->request->getPost('rememberMe') ? TRUE : FALSE);
         if (!$login) {
             $errors = new \Phalcon\Validation\Message\Group();
             if ($login === NULL) {
                 $errors->appendMessage(new \Phalcon\Validation\Message(__('Field :field is incorrect', array(':field' => __('Username'))), 'username', 'Incorrect'));
             } else {
                 $errors->appendMessage(new \Phalcon\Validation\Message(__('Field :field is incorrect', array(':field' => __('Password'))), 'password', 'Incorrect'));
             }
             $this->view->setVar('errors', $errors);
             $this->flashSession->warning($this->tag->linkTo(array('#', 'class' => 'close', 'title' => __("Close"), '×')) . '<strong>' . __('Warning') . '!</strong> ' . __("Please correct the errors."));
         } else {
             $referer = $this->request->getHTTPReferer();
             $needBackRedirect = !empty($referer) && strpos(parse_url($referer, PHP_URL_PATH), '/user/signin') !== 0 && parse_url($referer, PHP_URL_HOST) == $this->request->getHttpHost();
             if ($needBackRedirect) {
                 return $this->response->setHeader("Location", $referer);
             } else {
                 return $this->dispatcher->forward(array('controller' => 'index', 'action' => 'index'));
             }
         }
     }
 }
예제 #3
0
 public function testValidationSetLabels()
 {
     $_POST = array('email' => '', 'firstname' => '');
     $validation = new Phalcon\Validation();
     $validation->add('email', new PresenceOf(array('message' => 'The :field is required')));
     $validation->add('email', new Email(array('message' => 'The :field must be email', 'label' => 'E-mail')));
     $validation->add('firstname', new PresenceOf(array('message' => 'The :field is required')));
     $validation->add('firstname', new StringLength(array('min' => 4, 'messageMinimum' => 'The :field is too short')));
     $validation->setLabels(array('firstname' => 'First name'));
     $messages = $validation->validate($_POST);
     $expectedMessages = Phalcon\Validation\Message\Group::__set_state(array('_messages' => array(0 => Phalcon\Validation\Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The email is required', '_field' => 'email', '_code' => '0')), 1 => Phalcon\Validation\Message::__set_state(array('_type' => 'Email', '_message' => 'The E-mail must be email', '_field' => 'email', '_code' => '0')), 2 => Phalcon\Validation\Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The First name is required', '_field' => 'firstname', '_code' => '0')), 3 => Phalcon\Validation\Message::__set_state(array('_type' => 'TooShort', '_message' => 'The First name is too short', '_field' => 'firstname', '_code' => '0')))));
     $this->assertEquals($expectedMessages, $messages);
 }
예제 #4
0
 public function testOptionAllowEmpty()
 {
     $expectedMessages = Phalcon\Validation\Message\Group::__set_state(array('_messages' => array(0 => Phalcon\Validation\Message::__set_state(array('_type' => 'Confirmation', '_message' => 'Field password must be the same as password2', '_field' => 'password', '_code' => '0')))));
     // allowEmpty: true
     $validation = new Phalcon\Validation();
     $validation->add('password', new Confirmation(array('allowEmpty' => true, 'with' => 'password2')));
     $this->assertEquals(count($validation->validate(array('password' => 'test123', 'password2' => 'test123'))), 0);
     $this->assertEquals(count($validation->validate(array('password' => null, 'password2' => 'test123'))), 0);
     // END
     // allowEmpty: false
     $validation = new Phalcon\Validation();
     $validation->add('password', new Confirmation(array('allowEmpty' => false, 'with' => 'password2')));
     $this->assertEquals(count($validation->validate(array('password' => 'test123', 'password2' => 'test123'))), 0);
     $messages = $validation->validate(array('password' => null, 'password2' => 'test123'));
     $this->assertEquals(count($messages), 1);
     $this->assertEquals($messages, $expectedMessages);
     // END
     // allowEmpty: DEFAULT
     $validation = new Phalcon\Validation();
     $validation->add('password', new Confirmation(array('with' => 'password2')));
     $this->assertEquals(count($validation->validate(array('password' => 'test123', 'password2' => 'test123'))), 0);
     $messages = $validation->validate(array('password' => null, 'password2' => 'test123'));
     $this->assertEquals(count($messages), 1);
     $this->assertEquals($messages, $expectedMessages);
     // END
 }
예제 #5
0
 public function testValidationCancelOnFail()
 {
     $validation = new Phalcon\Validation();
     $validation->add('name', new PresenceOf(array('message' => 'The name is required')))->add('email', new PresenceOf(array('message' => 'The email is required', 'cancelOnFail' => true)))->add('login', new PresenceOf(array('message' => 'The login is required')));
     $messages = $validation->validate($_POST);
     $expectedMessages = Phalcon\Validation\Message\Group::__set_state(array('_messages' => array(0 => Phalcon\Validation\Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The name is required', '_field' => 'name')), 1 => Phalcon\Validation\Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The email is required', '_field' => 'email')))));
     $this->assertEquals($messages, $expectedMessages);
 }
예제 #6
0
 public function testFormValidator()
 {
     //First element
     $telephone = new Text("telephone");
     $telephone->addValidator(new PresenceOf(array('message' => 'The telephone is required')));
     $this->assertEquals(count($telephone->getValidators()), 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'))));
     $this->assertEquals(count($telephone->getValidators()), 3);
     //Second element
     $address = new Text('address');
     $address->addValidator(new PresenceOf(array('message' => 'The address is required')));
     $this->assertEquals(count($address->getValidators()), 1);
     $form = new Form();
     $form->add($telephone);
     $form->add($address);
     $this->assertFalse($form->isValid(array()));
     $expectedMessages = Phalcon\Validation\Message\Group::__set_state(array('_messages' => array(0 => Phalcon\Validation\Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The telephone is required', '_field' => 'telephone', '_code' => 0)), 1 => Phalcon\Validation\Message::__set_state(array('_type' => 'TooShort', '_message' => 'The telephone is too short', '_field' => 'telephone', '_code' => 0)), 2 => Phalcon\Validation\Message::__set_state(array('_type' => 'Regex', '_message' => 'The telephone has an invalid format', '_field' => 'telephone', '_code' => 0)), 3 => Phalcon\Validation\Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The address is required', '_field' => 'address', '_code' => 0)))));
     $this->assertEquals($form->getMessages(), $expectedMessages);
     $this->assertFalse($form->isValid(array('telephone' => '12345', 'address' => 'hello')));
     $expectedMessages = Phalcon\Validation\Message\Group::__set_state(array('_messages' => array(0 => Phalcon\Validation\Message::__set_state(array('_type' => 'Regex', '_message' => 'The telephone has an invalid format', '_field' => 'telephone', '_code' => 0)))));
     $this->assertEquals($form->getMessages(), $expectedMessages);
     $this->assertTrue($form->isValid(array('telephone' => '+44 124 82122', 'address' => 'hello')));
 }