/** * @test */ public function requiredIsNotValid() { $form = \Ameos\AmeosForm\Form\Factory::make('tx_ameosform-unittest'); $form->disableCsrftoken()->add('input-text', 'text')->addConstraint('input-text', 'required', 'field mandatory'); $_POST['tx_ameosform-unittest']['issubmitted'] = 1; // simulate post form $form->bindRequest(array('input-text' => '')); $result = $form->get('input-text')->isValid(); $this->assertFalse($result); }
/** * @test */ public function customIsNotValid() { $form = \Ameos\AmeosForm\Form\Factory::make('tx_ameosform-unittest'); $form->disableCsrftoken()->add('input-text', 'text'); $form->addConstraint('input-text', 'custom', 'custom error', ['method' => function ($value, $form) { return $value == 'test'; }]); $_POST['tx_ameosform-unittest']['issubmitted'] = 1; // simulate post form $form->bindRequest(array('input-text' => 'othervalue')); $result = $form->get('input-text')->isValid(); $this->assertFalse($result); }
/** * @test */ public function sameasIsNotValid() { $form = \Ameos\AmeosForm\Form\Factory::make('tx_ameosform-unittest'); $form->disableCsrftoken(); $form->add('input-text-1', 'text'); $form->add('input-text-2', 'text'); $form->addConstraint('input-text-2', 'sameas', 'must be the same', ['sameas' => 'input-text-1']); $_POST['tx_ameosform-unittest']['issubmitted'] = 1; // simulate post form $form->bindRequest(array('input-text-1' => 'test', 'input-text-2' => 'othervalue')); $result = $form->get('input-text-2')->isValid(); $this->assertFalse($result); }
/** * @test */ public function addErrors() { $form = \Ameos\AmeosForm\Form\Factory::make('tx_ameosform-unittest'); $form->disableCsrftoken()->add('input-text-required', 'text')->addConstraint('input-text-required', 'required', 'field mandatory'); $form->disableCsrftoken()->add('input-text-email', 'text')->addConstraint('input-text-email', 'email', 'email not valid'); $form->disableCsrftoken()->add('input-text-required-2', 'text')->addConstraint('input-text-required-2', 'required', 'field mandatory'); $_POST['tx_ameosform-unittest']['issubmitted'] = 1; // simulate post form $form->bindRequest(['input-text-required' => '', 'input-text-email' => 'mail-not-valid', 'input-text-required-2' => 'test']); $expectedResult = ['is_valid' => FALSE, 'errors_merged' => ['field mandatory', 'email not valid'], 'errors_by_element' => ['input-text-required' => ['field mandatory'], 'input-text-email' => ['email not valid']], 'errors_for_text' => ['field mandatory'], 'errors_for_mail' => ['email not valid']]; $result = ['is_valid' => $form->isValid(), 'errors_merged' => $form->getErrors(), 'errors_by_element' => $form->getErrorsByElement(), 'errors_for_text' => $form->getErrorsFormElement('input-text-required'), 'errors_for_mail' => $form->getErrorsFormElement('input-text-email')]; $this->assertEquals($result, $expectedResult); }
/** * @test */ public function elementRendereringInformation() { $expectedHtmlResult = '<input type="text" id="tx_ameosform-unittest_input-text" ' . 'name="tx_ameosform-unittest[input-text]" ' . 'value="my-value" ' . 'placeholder="Here your value" ' . 'style="display: block;" ' . 'title="My text field" ' . 'customattr="custom-value" ' . 'class="my-css-class" />'; $expectedResult = ['defaultValue' => 'my-value', 'class' => 'my-css-class', 'style' => 'display: block;', 'title' => 'My text field', 'custom' => 'customattr="custom-value"', 'placeholder' => 'Here your value', 'otherdata' => 'test', '__compiled' => $expectedHtmlResult, 'name' => 'input-text', 'value' => 'my-value', 'absolutename' => 'tx_ameosform-unittest[input-text]', 'htmlid' => 'tx_ameosform-unittest_input-text', 'errors' => [], 'isvalid' => TRUE, 'hasError' => FALSE]; $elementConfiguration = []; $elementConfiguration['defaultValue'] = 'my-value'; $elementConfiguration['class'] = 'my-css-class'; $elementConfiguration['title'] = 'My text field'; $elementConfiguration['custom'] = 'customattr="custom-value"'; $elementConfiguration['placeholder'] = 'Here your value'; $elementConfiguration['otherdata'] = 'test'; $form = \Ameos\AmeosForm\Form\Factory::make('tx_ameosform-unittest'); $form->add('input-text', 'text', $elementConfiguration); $form->get('input-text')->with('style', 'display: block;'); $result = $form->get('input-text')->getRenderingInformation(); $this->assertEquals($result, $expectedResult); }
/** * @test */ public function elementRendereringInformation() { $expectedHtmlResult = '<select id="tx_ameosform-unittest_my-select" name="tx_ameosform-unittest[my-select]" ' . 'placeholder="Here your value" ' . 'style="display: block;" ' . 'title="My text field" ' . 'customattr="custom-value" ' . 'class="my-css-class">' . '<option value="">Here your value</option>' . '<option value="value-1">Value 1</option>' . '<option value="value-2" selected="selected">Value 2</option>' . '<option value="value-3">Value 3</option>' . '</select>'; $expectedResult = ['defaultValue' => 'value-2', 'class' => 'my-css-class', 'style' => 'display: block;', 'title' => 'My text field', 'custom' => 'customattr="custom-value"', 'placeholder' => 'Here your value', 'otherdata' => 'test', '__compiled' => $expectedHtmlResult, 'name' => 'my-select', 'value' => 'value-2', 'absolutename' => 'tx_ameosform-unittest[my-select]', 'htmlid' => 'tx_ameosform-unittest_my-select', 'items' => ['value-1' => 'Value 1', 'value-2' => 'Value 2', 'value-3' => 'Value 3'], 'errors' => [], 'isvalid' => TRUE, 'hasError' => FALSE, 'optionValueField' => 'uid']; $elementConfiguration = []; $elementConfiguration['defaultValue'] = 'value-2'; $elementConfiguration['class'] = 'my-css-class'; $elementConfiguration['title'] = 'My text field'; $elementConfiguration['custom'] = 'customattr="custom-value"'; $elementConfiguration['placeholder'] = 'Here your value'; $elementConfiguration['otherdata'] = 'test'; $elementConfiguration['items'] = ['value-1' => 'Value 1', 'value-2' => 'Value 2', 'value-3' => 'Value 3']; $form = \Ameos\AmeosForm\Form\Factory::make('tx_ameosform-unittest'); $form->add('my-select', 'dropdown', $elementConfiguration); $form->get('my-select')->with('style', 'display: block;'); $result = $form->get('my-select')->getRenderingInformation(); $this->assertEquals($result, $expectedResult); }