diff --git a/.gitignore b/.gitignore index f0913a8..e825849 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/.env /.env.local /.env.local.php /.env.*.local diff --git a/migrations/Version20240109170611.php b/migrations/Version20240109170611.php index ec81069..3681250 100644 --- a/migrations/Version20240109170611.php +++ b/migrations/Version20240109170611.php @@ -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)'); diff --git a/src/FieldTypes/Validators/RegExpValidator.php b/src/FieldTypes/Validators/RegExpValidator.php index 2e36ed5..12d6d72 100644 --- a/src/FieldTypes/Validators/RegExpValidator.php +++ b/src/FieldTypes/Validators/RegExpValidator.php @@ -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 { diff --git a/src/PublicApi/Controller/FormController.php b/src/PublicApi/Controller/FormController.php index e035df7..d58ee64 100644 --- a/src/PublicApi/Controller/FormController.php +++ b/src/PublicApi/Controller/FormController.php @@ -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); @@ -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; } } \ No newline at end of file