summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/App.php19
-rw-r--r--tests/Test.php43
2 files changed, 59 insertions, 3 deletions
diff --git a/src/App.php b/src/App.php
index 1a12f6a..0c0773b 100644
--- a/src/App.php
+++ b/src/App.php
@@ -9,6 +9,8 @@ use Yosymfony\Toml\TomlBuilder;
class App
{
+ public string $formPath;
+
/**
* App constructor.
*/
@@ -42,7 +44,7 @@ class App
// GET
if ($method == 'GET') {
if (str_ends_with($path, '/fields')) {
- $formPath = $contentRoot . str_replace('/fields', '', $path);
+ $this->formPath = $formPath = $contentRoot . str_replace('/fields', '', $path);
$fields = $this->buildFields($formPath, $_GET['page'] ?? null);
@@ -62,7 +64,7 @@ class App
// POST
else if ($method == 'POST') {
if (str_ends_with($path, '/validate')) {
- $formPath = $contentRoot . str_replace('/validate', '', $path);
+ $this->formPath = $formPath = $contentRoot . str_replace('/validate', '', $path);
$fields = $this->buildFields($formPath, $_GET['page'] ?? null);
@@ -70,7 +72,7 @@ class App
}
else if (str_ends_with($path, '/submit')) {
- $formPath = $contentRoot . str_replace('/submit', '', $path);
+ $this->formPath = $formPath = $contentRoot . str_replace('/submit', '', $path);
$fields = $this->buildFields($formPath);
@@ -268,6 +270,11 @@ class App
$field['is_valid'] = false;
}
+ $validationFunctionName = 'validate_' . basename($this->formPath) . '_' . $field['name'];
+ if (function_exists($validationFunctionName)) {
+ $field = call_user_func($validationFunctionName, $field, $value);
+ }
+
return $field;
}
@@ -288,6 +295,12 @@ class App
$config['api']['keys'] = $apiKeys;
}
+ // include custom functions
+ $functionsFile = $currentDirectory . '/config/functions.php';
+ if (file_exists($functionsFile)) {
+ include_once $functionsFile;
+ }
+
if (str_ends_with($currentDirectory, '/' . basename($_ENV['app']['contentFolderPath'])) || $currentDirectory == '/') {
break;
}
diff --git a/tests/Test.php b/tests/Test.php
index 37e5f65..b8a02f3 100644
--- a/tests/Test.php
+++ b/tests/Test.php
@@ -22,6 +22,7 @@ class Test extends TestCase
@mkdir($contentRoot . '/config');
@mkdir($contentRoot . '/customername/formname/config');
@mkdir($contentRoot . '/customername/formname/fields');
+ @mkdir($contentRoot . '/customername/pagedform/config');
@mkdir($contentRoot . '/customername/pagedform/fields');
# root config
@@ -78,6 +79,18 @@ class Test extends TestCase
data-value = 123
EOF);
+ file_put_contents($contentRoot . '/customername/formname/config/functions.php', '
+ <?php
+ function validate_formname_name($field, $value)
+ {
+ if ($value !== \'Adelbert\') {
+ $field[\'is_valid\'] = false;
+ }
+
+ return $field;
+ }
+ ');
+
# pagedform fields
file_put_contents($contentRoot . '/customername/pagedform/fields/_fields.toml', <<<EOF
[page.one.field.name]
@@ -273,6 +286,36 @@ class Test extends TestCase
$this->assertArrayNotHasKey('second', $body);
$this->assertEquals(true, $body['date']['is_valid']);
$this->assertEquals(true, $body['text']['is_valid']);
+
+ // valid response validation_function
+ $response = $this->request('POST', 'customername/formname/submit?key=' . $this->apiKey, [
+ 'form_params' => [
+ 'name' => 'Adelbert',
+ 'email' => 'EMAIL',
+ 'date' => 'DATE',
+ ],
+ ]);
+ $body = json_decode((string)$response->getBody(), true);
+ $this->assertArrayNotHasKey('error', $body);
+ $body = $body['data'];
+ $this->assertEquals(true, $body['name']['is_valid']);
+ $this->assertEquals(true, $body['email']['is_valid']);
+ $this->assertEquals(true, $body['date']['is_valid']);
+
+ // invalid response validation_function
+ $response = $this->request('POST', 'customername/formname/submit?key=' . $this->apiKey, [
+ 'form_params' => [
+ 'name' => 'NAME',
+ 'email' => 'EMAIL',
+ 'date' => 'DATE',
+ ],
+ ]);
+ $body = json_decode((string)$response->getBody(), true);
+ $this->assertArrayHasKey('error', $body);
+ $body = $body['data'];
+ $this->assertEquals(false, $body['name']['is_valid']);
+ $this->assertEquals(true, $body['email']['is_valid']);
+ $this->assertEquals(true, $body['date']['is_valid']);
}
}