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()); }
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()); }
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()); }
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()); }
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()); }
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()); }
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()); }