/** * Test the errors methods. * * @return void */ public function testErrors() { $form = new Form(); $form->validator()->add('email', 'format', ['message' => 'Must be a valid email', 'rule' => 'email'])->add('body', 'length', ['message' => 'Must be so long', 'rule' => ['minLength', 12]]); $data = ['email' => 'rong', 'body' => 'too short']; $form->validate($data); $errors = $form->errors(); $this->assertCount(2, $errors); $this->assertEquals('Must be a valid email', $errors['email']['format']); $this->assertEquals('Must be so long', $errors['body']['length']); }
public static function getForm(string $tabId = null) : Form { $schema = new Schema(); $settings = self::getSettings($tabId); /** @var $v \ThreeCMS\Core\Settings\Setting **/ foreach ($settings as $v) { $schema->addField($v->getKey(), ['type' => 'text']); } $form = new Form(); $form->validator(self::getValidator($tabId)); $form->schema($schema); return $form; }
/** * test __debugInfo * * @return void */ public function testDebugInfo() { $form = new Form(); $result = $form->__debugInfo(); $this->assertArrayHasKey('_schema', $result); $this->assertArrayHasKey('_errors', $result); $this->assertArrayHasKey('_validator', $result); }
/** * Test checking errors. * * @return void */ public function testHasError() { $form = new Form(); $form->validator()->add('email', 'format', ['rule' => 'email'])->add('name', 'length', ['rule' => ['minLength', 10]]); $form->validate(['email' => 'derp', 'name' => 'derp']); $context = new FormContext($this->request, ['entity' => $form]); $this->assertTrue($context->hasError('email')); $this->assertTrue($context->hasError('name')); $this->assertFalse($context->hasError('nope')); $this->assertFalse($context->hasError('nope.nope')); }
/** * Test checking errors. * * @return void */ public function testHasError() { $nestedValidator = new Validator(); $nestedValidator->add('password', 'length', ['rule' => ['minLength', 8]])->add('confirm', 'length', ['rule' => ['minLength', 8]]); $form = new Form(); $form->validator()->add('email', 'format', ['rule' => 'email'])->add('name', 'length', ['rule' => ['minLength', 10]])->addNested('pass', $nestedValidator); $form->validate(['email' => 'derp', 'name' => 'derp', 'pass' => ['password' => 'short', 'confirm' => 'long enough']]); $context = new FormContext($this->request, ['entity' => $form]); $this->assertTrue($context->hasError('email')); $this->assertTrue($context->hasError('name')); $this->assertFalse($context->hasError('nope')); $this->assertFalse($context->hasError('nope.nope')); $this->assertTrue($context->hasError('pass.password')); }