Skip to content

Commit 6312325

Browse files
committed
CORS fix; Regexp validation fix; Migration fix
1 parent e531700 commit 6312325

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/.env
12
/.env.local
23
/.env.local.php
34
/.env.*.local

Diff for: migrations/Version20240109170611.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public function up(Schema $schema): void
3333
$this->addSql('CREATE INDEX forms_deleted_at_idx ON forms (deleted_at)');
3434
$this->addSql('CREATE TABLE submissions (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, form_id INTEGER NOT NULL, answers CLOB NOT NULL --(DC2Type:json)
3535
, created_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
36-
, read_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
37-
, flagged_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
38-
, deleted_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
36+
, read_at DATETIME --(DC2Type:datetime_immutable)
37+
, flagged_at DATETIME --(DC2Type:datetime_immutable)
38+
, deleted_at DATETIME --(DC2Type:datetime_immutable)
3939
, CONSTRAINT FK_3F6169F75FF69B7D FOREIGN KEY (form_id) REFERENCES forms (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
4040
$this->addSql('CREATE INDEX IDX_3F6169F75FF69B7D ON submissions (form_id)');
4141
$this->addSql('CREATE INDEX submissions_form_id_idx ON submissions (form_id, created_at)');

Diff for: src/FieldTypes/Validators/RegExpValidator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function validate($value): bool {
1212
if ($this->pattern === null) {
1313
return true;
1414
}
15-
return preg_match($this->pattern, $value) === 1;
15+
return preg_match('!' . $this->pattern . '!u', $value) === 1;
1616
}
1717

1818
public function getErrorMessage(): string {

Diff for: src/PublicApi/Controller/FormController.php

+18-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222
{
2323
}
2424

25-
#[Route('/api/forms/{hash}', name: 'public_api_form', methods: ['GET', 'POST'], format: 'json')]
25+
#[Route('/api/forms/{hash}', name: 'public_api_form', methods: ['GET', 'POST', 'OPTIONS'], format: 'json')]
2626
public function form(Request $request, string $hash): JsonResponse
2727
{
2828
$form = $this->formService->getByHash($hash);
@@ -49,21 +49,26 @@ public function form(Request $request, string $hash): JsonResponse
4949
}
5050

5151
$submitted = $this->formSubmissionService->submit($form->getId(), $data);
52+
$response = $this->json($submitted);
53+
} else {
54+
$formFields = $this->formFieldService->getAllByFormId($form->getId());
5255

53-
return $this->json($submitted);
56+
$response = $this->json([
57+
'fields' => array_map(fn($formField) => [
58+
'type' => $formField->getType(),
59+
'name' => $formField->getName(),
60+
'label' => $formField->getLabel(),
61+
'hint' => $formField->getHint(),
62+
'isRequired' => $formField->getIsRequired(),
63+
'validations' => $formField->getValidations(),
64+
], $formFields),
65+
]);
5466
}
5567

56-
$formFields = $this->formFieldService->getAllByFormId($form->getId());
68+
$response->headers->set('Access-Control-Allow-Origin', '*');
69+
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
70+
$response->headers->set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
5771

58-
return $this->json([
59-
'fields' => array_map(fn($formField) => [
60-
'type' => $formField->getType(),
61-
'name' => $formField->getName(),
62-
'label' => $formField->getLabel(),
63-
'hint' => $formField->getHint(),
64-
'isRequired' => $formField->getIsRequired(),
65-
'validations' => $formField->getValidations(),
66-
], $formFields),
67-
]);
72+
return $response;
6873
}
6974
}

0 commit comments

Comments
 (0)