Beispiel #1
0
 public function testGetValue()
 {
     $form = new Form();
     $submitElement = $form->submit('submit', 'label');
     $form->handle(new FormData('post', [$form->getUid() => 1, 'submit' => '']));
     static::assertEquals('label', $submitElement->getValue());
 }
Beispiel #2
0
 public function testNotSubmitted()
 {
     $form = new Form();
     $button = $this->createTestButton();
     $form->addElement($button);
     $form->handle(new FormData(FormData::METHOD_POST, [$form->getUid() => '1']));
     $this->assertFalse($button->isSubmitted());
 }
Beispiel #3
0
 public function testHandleStringData()
 {
     $form = new Form();
     $form->setName('test_form');
     $checkbox = $form->checkbox('send_emails');
     $this->assertEquals(0, $checkbox->getValue());
     $this->assertFalse($checkbox->isChecked());
     // submit checked value
     $form->handle(new FormData('post', ['test_form' => '1', 'send_emails' => '1']));
     $this->assertEquals(1, $checkbox->getValue());
     $this->assertTrue($checkbox->isChecked());
     // submit unchecked value
     $form->handle(new FormData('post', ['test_form' => 1, 'send_emails' => 0]));
     $this->assertEquals(0, $checkbox->getValue());
     $this->assertFalse($checkbox->isChecked());
 }
Beispiel #4
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());
 }
Beispiel #5
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());
 }
Beispiel #6
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;
 }
Beispiel #7
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());
 }
 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());
 }
Beispiel #9
0
 public function testHandleRequest()
 {
     $form = new Form();
     $form->addElement((new Input())->setName('email'));
     $form->addElement((new TextArea())->setName('description'));
     $form->setMethod('post');
     $form->handle(new FormData('post', [$form->getUid() => 1, 'email' => '*****@*****.**', 'description' => 'some description text']));
     $this->assertTrue($form->isSubmitted());
     $this->assertEquals('*****@*****.**', $form->getElement('email')->getValue());
     $this->assertEquals('some description text', $form->getElement('description')->getValue());
     $form->handle(new FormData('post', [$form->getUid() => 1, 'email' => '*****@*****.**']));
     $this->assertTrue($form->isSubmitted());
     $this->assertEquals('*****@*****.**', $form->getElement('email')->getValue());
     $this->assertNull($form->getElement('description')->getValue());
     $form->handle(new FormData('post', [$form->getUid() => '1']));
     $this->assertTrue($form->isSubmitted());
     $this->assertNull($form->getElement('email')->getValue());
     $this->assertNull($form->getElement('description')->getValue());
     $form->handle(new FormData('post', []));
     $this->assertFalse($form->isSubmitted());
 }
Beispiel #10
0
 /**
  * @return \Fiv\Form\Element\Input
  */
 protected function getElement()
 {
     $form = new Form();
     return $form->hidden('test');
 }
Beispiel #11
0
 /**
  * @return \Fiv\Form\Element\TextArea
  */
 protected function getElement()
 {
     $form = new Form();
     return $form->textarea('test');
 }
Beispiel #12
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());
 }