summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2022-01-05 23:12:23 +0100
committerDaniel Weipert <code@drogueronin.de>2022-01-10 11:29:13 +0100
commitd6c5c7735e3207ead7759375e444bc6b72045e49 (patch)
treebcaded65f8279dde16750699cd7bbf4886a94e20
parent43ec8517cbc42093ad1f17063495f79e6eddf1cf (diff)
Validate fields by pattern and add more example fields
-rw-r--r--src/App.php7
-rw-r--r--templates/form-paged/fields/second.toml14
-rw-r--r--tests/Test.php62
3 files changed, 78 insertions, 5 deletions
diff --git a/src/App.php b/src/App.php
index a155017..1a12f6a 100644
--- a/src/App.php
+++ b/src/App.php
@@ -257,9 +257,14 @@ class App
*/
public function validateSingleField($field)
{
+ $value = $_POST[$field['name']] ?? '';
$field['is_valid'] = true;
- if (($field['required'] ?? false) && empty($_POST[$field['name']])) {
+ if (isset($field['required']) && empty($value)) {
+ $field['is_valid'] = false;
+ }
+
+ if (isset($field['validation']['pattern']) && preg_match_all('/' . $field['validation']['pattern'] . '/', $value) === 0) {
$field['is_valid'] = false;
}
diff --git a/templates/form-paged/fields/second.toml b/templates/form-paged/fields/second.toml
index bbc6ea7..cdeb9f0 100644
--- a/templates/form-paged/fields/second.toml
+++ b/templates/form-paged/fields/second.toml
@@ -1,3 +1,17 @@
[field.text]
placeholder = "Text placeholder"
+[field.text.validation]
+pattern = "\\w+"
+
+[field.manythings]
+type = "select"
+
+[field.manythings.options]
+first = "First level"
+second = "Second level"
+
+[field.multiplethings]
+type = "checkbox"
+options = ["thing-1", "thing-2", "thing-3"]
+
diff --git a/tests/Test.php b/tests/Test.php
index 798a74d..abb4749 100644
--- a/tests/Test.php
+++ b/tests/Test.php
@@ -96,6 +96,20 @@ class Test extends TestCase
file_put_contents($contentRoot . '/customername/pagedform/fields/second.toml', <<<EOF
[field.text]
placeholder = "Text placeholder"
+
+ [field.text.validation]
+ pattern = "\\\d+"
+
+ [field.manythings]
+ type = "select"
+
+ [field.manythings.options]
+ first = "First level"
+ second = "Second level"
+
+ [field.multiplethings]
+ type = "checkbox"
+ options = ["thing-1", "thing-2", "thing-3"]
EOF);
file_put_contents($contentRoot . '/customername/pagedform/fields/email.toml', <<<EOF
type = "email"
@@ -114,11 +128,11 @@ class Test extends TestCase
EOF);
}
- public function request($method, $path): GuzzleHttpResponse
+ public function request($method, $path, $options = []): GuzzleHttpResponse
{
$response = null;
try {
- $response = $this->client->request($method, $path, ['http_errors' => false]);
+ $response = $this->client->request($method, $path, ['http_errors' => false] + $options);
} catch (\Exception $e) {}
return $response;
@@ -150,7 +164,8 @@ class Test extends TestCase
$this->assertEquals(Response::HTTP_OK, $pagedformResponse->getStatusCode());
}
- public function testFields() {
+ public function testFields()
+ {
$formnameResponse = $this->request('GET', 'customername/formname/fields?key=' . $this->apiKey);
$formnameBody = json_decode((string)$formnameResponse->getBody(), true)['data'];
$this->assertArrayHasKey('name', $formnameBody);
@@ -162,7 +177,8 @@ class Test extends TestCase
$this->assertArrayHasKey('required', $formnameBody['email']);
}
- public function testFieldsPaged() {
+ public function testFieldsPaged()
+ {
// all pages
$pagedformResponse = $this->request('GET', 'customername/pagedform/fields?key=' . $this->apiKey);
$pagedformBody = json_decode((string)$pagedformResponse->getBody(), true)['data'];
@@ -187,6 +203,44 @@ class Test extends TestCase
$this->assertArrayNotHasKey('second', $pagedformBody);
$this->assertArrayHasKey('date', $pagedformBody);
$this->assertArrayHasKey('text', $pagedformBody);
+ $this->assertArrayHasKey('first', $pagedformBody['manythings']['options']);
+ $this->assertEquals('thing-2', $pagedformBody['multiplethings']['options'][1]);
+ }
+
+ public function testValidation()
+ {
+ $response = $this->request('POST', 'customername/pagedform/submit?key=' . $this->apiKey, [
+ 'form_params' => [
+ 'name' => 'NAME',
+ 'email' => 'EMAIL',
+ 'date' => 'DATE',
+ 'text' => '123',
+ 'manythings' => 'second',
+ 'multiplethings' => [
+ 'thing-1', 'thing-3',
+ ],
+ ],
+ ]);
+ $body = json_decode((string)$response->getBody(), true);
+ $this->assertArrayNotHasKey('error', $body);
+ $body = $body['data'];
+ $this->assertEquals(true, $body['one']['email']['is_valid']);
+ $this->assertEquals(true, $body['second']['date']['is_valid']);
+ $this->assertEquals(true, $body['second']['text']['is_valid']);
+
+ $response = $this->request('POST', 'customername/pagedform/submit?key=' . $this->apiKey, [
+ 'form_params' => [
+ 'name' => 'NAME',
+ 'date' => 'DATE',
+ 'text' => 'einszweidrei',
+ ],
+ ]);
+ $body = json_decode((string)$response->getBody(), true);
+ $this->assertArrayHasKey('error', $body);
+ $body = $body['data'];
+ $this->assertEquals(false, $body['one']['email']['is_valid']);
+ $this->assertEquals(true, $body['second']['date']['is_valid']);
+ $this->assertEquals(false, $body['second']['text']['is_valid']);
}
}