Example #1
0
 public function testIn()
 {
     $inValidator = new \Fiv\Form\Validator\In();
     $inValidator->setValues(['a', 'b', 'c']);
     $form = new Form();
     $form->input('inputName')->addValidator($inValidator);
     $form->handle(new FormData('post', [$form->getUid() => 1, 'inputName' => 'a']));
     $this->assertTrue($form->isValid());
     $form->handle(new FormData('post', [$form->getUid() => 1, 'inputName' => 'd']));
     $this->assertFalse($form->isValid());
 }
Example #2
0
 public function testRegexp()
 {
     $regexpValidator = new \Fiv\Form\Validator\Regexp();
     $regexpValidator->setRegexp('![^\\@]+\\@[^\\@]+!');
     $form = new Form();
     $form->input('email')->addValidator($regexpValidator);
     $form->handle(new FormData('post', [$form->getUid() => 1, 'email' => 'test@test']));
     $this->assertTrue($form->isValid());
     $form->handle(new FormData('post', [$form->getUid() => 1, 'email' => 'test']));
     $this->assertFalse($form->isValid());
 }
Example #3
0
 public function testSimpleValidation()
 {
     $validator = new Required();
     $validator->setError('Test error message');
     $form = new Form();
     $form->input('login')->addValidator($validator);
     $form->handle(new FormData('post', [$form->getUid() => 1, 'login' => 'testLogin']));
     $this->assertTrue($form->isValid());
     $this->assertEmpty($validator->getErrors());
     $form->handle(new FormData('post', [$form->getUid() => 1, 'login' => '']));
     $this->assertFalse($form->isValid());
     $this->assertEquals('Test error message', $validator->getFirstError());
     $form->handle(new FormData('post', [$form->getUid() => 1, 'login' => '0']));
     $this->assertTrue($form->isValid());
 }
Example #4
0
 public function testFilterWithForm()
 {
     $form = new Form();
     $form->setName('test_form');
     $form->input('text')->addFilter(new RegexReplace('!\\s{2,}!', ' '))->addFilter(new CallbackFilter('trim'));
     $form->handle(new FormData('post', ['test_form' => 1, 'text' => 'hello    world         ']));
     $this->assertTrue($form->isValid());
     $this->assertEquals('hello world', $form->getElements()['text']->getValue());
 }
Example #5
0
 /**
  * @return bool
  */
 public function isValid()
 {
     if (!parent::isValid()) {
         return false;
     }
     if ($this->getElements()['emailFrom']->getValue() == '*****@*****.**' and $this->getElements()['emailTo']->getValue() == '*****@*****.**' and $this->getElements()['message']->getValue() == 'copy message text') {
         $this->validationResult = false;
         $this->addError('message duplicate error');
     }
     return $this->validationResult;
 }
Example #6
0
 public function testEmailVirtualDbValidation()
 {
     $form = new Form();
     $existEmailList = ['*****@*****.**', '*****@*****.**'];
     $callBackValidator = (new CallBackValidator(function ($value) use($existEmailList) {
         if (empty($value)) {
             return true;
         }
         if (in_array($value, $existEmailList)) {
             return false;
         }
         return true;
     }))->setErrorMessage('Email already exist!');
     $input = $form->input('email');
     $input->addValidator($callBackValidator);
     $form->handle(new FormData('post', [$form->getUid() => 1, 'email' => '*****@*****.**']));
     $this->assertFalse($form->isValid());
     $this->assertEquals('Email already exist!', $callBackValidator->getFirstError());
     $form->handle(new FormData('post', [$form->getUid() => 1, 'email' => '*****@*****.**']));
     $this->assertTrue($form->isValid());
 }
Example #7
0
 public function testFormValidationErrors()
 {
     $form = new Form();
     $form->input('name')->addValidator((new Required())->setError('name input error'));
     $form->input('email')->addValidator((new Required())->setError('email input error'));
     $form->handle(new FormData('post', [$form->getUid() => 1]));
     $this->assertFalse($form->isValid());
     $this->assertEquals(['name input error', 'email input error'], $form->getErrors());
     $form->handle(new FormData('post', [$form->getUid() => 1, 'email' => '*****@*****.**']));
     $this->assertFalse($form->isValid());
     $this->assertEquals(['name input error'], $form->getErrors());
     $form->handle(new FormData('post', [$form->getUid() => 1, 'name' => 'testName', 'email' => '*****@*****.**']));
     $this->assertTrue($form->isValid());
     $this->assertEquals([], $form->getErrors());
 }