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 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 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 testReSetData() { $form = new Form(); $form->input('name'); $form->input('email'); $form->input('age'); $form->handle(new FormData('post', [$form->getUid() => '1', 'name' => 'petro', 'email' => '*****@*****.**'])); $this->assertEquals('*****@*****.**', $form->getElements()['email']->getValue()); $this->assertEquals(['name' => 'petro', 'email' => '*****@*****.**', 'age' => ''], $form->getData()); $form->handle(new FormData('post', [$form->getUid() => '1', 'name' => 'stepan'])); $this->assertEquals(null, $form->getElement('email')->getValue()); $this->assertEquals('stepan', $form->getElement('name')->getValue()); }