Skip to content

Commit

Permalink
CORS fix; Regexp validation fix; Migration fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sdkfuyr committed Jan 16, 2024
1 parent e531700 commit 6312325
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.env
/.env.local
/.env.local.php
/.env.*.local
Expand Down
6 changes: 3 additions & 3 deletions migrations/Version20240109170611.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function up(Schema $schema): void
$this->addSql('CREATE INDEX forms_deleted_at_idx ON forms (deleted_at)');
$this->addSql('CREATE TABLE submissions (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, form_id INTEGER NOT NULL, answers CLOB NOT NULL --(DC2Type:json)
, created_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, read_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, flagged_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, deleted_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, read_at DATETIME --(DC2Type:datetime_immutable)
, flagged_at DATETIME --(DC2Type:datetime_immutable)
, deleted_at DATETIME --(DC2Type:datetime_immutable)
, CONSTRAINT FK_3F6169F75FF69B7D FOREIGN KEY (form_id) REFERENCES forms (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_3F6169F75FF69B7D ON submissions (form_id)');
$this->addSql('CREATE INDEX submissions_form_id_idx ON submissions (form_id, created_at)');
Expand Down
2 changes: 1 addition & 1 deletion src/FieldTypes/Validators/RegExpValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function validate($value): bool {
if ($this->pattern === null) {
return true;
}
return preg_match($this->pattern, $value) === 1;
return preg_match('!' . $this->pattern . '!u', $value) === 1;
}

public function getErrorMessage(): string {
Expand Down
31 changes: 18 additions & 13 deletions src/PublicApi/Controller/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(
{
}

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

$submitted = $this->formSubmissionService->submit($form->getId(), $data);
$response = $this->json($submitted);
} else {
$formFields = $this->formFieldService->getAllByFormId($form->getId());

return $this->json($submitted);
$response = $this->json([
'fields' => array_map(fn($formField) => [
'type' => $formField->getType(),
'name' => $formField->getName(),
'label' => $formField->getLabel(),
'hint' => $formField->getHint(),
'isRequired' => $formField->getIsRequired(),
'validations' => $formField->getValidations(),
], $formFields),
]);
}

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

return $this->json([
'fields' => array_map(fn($formField) => [
'type' => $formField->getType(),
'name' => $formField->getName(),
'label' => $formField->getLabel(),
'hint' => $formField->getHint(),
'isRequired' => $formField->getIsRequired(),
'validations' => $formField->getValidations(),
], $formFields),
]);
return $response;
}
}

0 comments on commit 6312325

Please sign in to comment.