From 24ce54a5ea7eda319ab74cd56ffe760cb5914f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tuomas=20V=C3=A4limaa?= Date: Thu, 16 Nov 2023 12:26:26 +0200 Subject: [PATCH 1/3] Application form applicant checks --- .../src/Form/ApplicationForm.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/public/modules/custom/asu_application/src/Form/ApplicationForm.php b/public/modules/custom/asu_application/src/Form/ApplicationForm.php index 52d94395..d0a90dec 100644 --- a/public/modules/custom/asu_application/src/Form/ApplicationForm.php +++ b/public/modules/custom/asu_application/src/Form/ApplicationForm.php @@ -200,6 +200,26 @@ public function validateForm(array &$form, FormStateInterface $form_state) { } } + $has_additional_applicant = (!empty($formValues['applicant'][0]['has_additional_applicant'])) ? $formValues['applicant'][0]['has_additional_applicant'] : NULL; + + if ($has_additional_applicant && $has_additional_applicant) { + foreach ($formValues['applicant'][0] as $applicant_field => $applicant_value) { + if ($applicant_field == 'has_additional_applicant') { + continue; + } + + if ($applicant_field == 'personal_id' && strlen($applicant_value) != 4) { + $fieldTitle = (string) $form["applicant"]['widget'][0][$applicant_field]['#title']; + $form_state->setErrorByName($field, t('Check @field', ['@field' => $fieldTitle])); + } + + if (empty($applicant_value) || $applicant_value == '-' || strlen($applicant_value) < 2) { + $fieldTitle = (string) $form["applicant"]['widget'][0][$applicant_field]['#title']; + $form_state->setErrorByName($applicant_field, t('Field @field cannot be empty', ['@field' => $fieldTitle])); + } + } + } + $triggerName = $form_state->getTriggeringElement()['#name']; if ($triggerName == 'submit-application') { parent::validateForm($form, $form_state); From 7bc20df6e8c622405ec2503ba3f41a3ccf3f56cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tuomas=20V=C3=A4limaa?= Date: Thu, 16 Nov 2023 12:52:24 +0200 Subject: [PATCH 2/3] Condition fix --- .../custom/asu_application/src/Form/ApplicationForm.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/modules/custom/asu_application/src/Form/ApplicationForm.php b/public/modules/custom/asu_application/src/Form/ApplicationForm.php index d0a90dec..6994c08b 100644 --- a/public/modules/custom/asu_application/src/Form/ApplicationForm.php +++ b/public/modules/custom/asu_application/src/Form/ApplicationForm.php @@ -200,9 +200,9 @@ public function validateForm(array &$form, FormStateInterface $form_state) { } } - $has_additional_applicant = (!empty($formValues['applicant'][0]['has_additional_applicant'])) ? $formValues['applicant'][0]['has_additional_applicant'] : NULL; + $has_additional_applicant = (!empty($formValues['applicant'][0]['has_additional_applicant'])) ? (bool) $formValues['applicant'][0]['has_additional_applicant'] : FALSE; - if ($has_additional_applicant && $has_additional_applicant) { + if ($has_additional_applicant) { foreach ($formValues['applicant'][0] as $applicant_field => $applicant_value) { if ($applicant_field == 'has_additional_applicant') { continue; From 311c67ef73367aa334663398f4136646eef4ba01 Mon Sep 17 00:00:00 2001 From: Tuomas Suutari Date: Thu, 16 Nov 2023 13:26:00 +0200 Subject: [PATCH 3/3] Fix getApplicant method naming Use better method name "getAdditionalApplicant" in the CreateApplicationRequest and SalesCreateApplicationRequest and "getAdditionalApplicants" in Application. This should make the code less confusing, since it's now clear that there are indeed ADDITIONAL applicants and that the one that returns an array has the "s" in the end of the name for plural. This fixes a bug in SalesCreateApplicationRequest, which used to call `application->getApplicants()` but the method name was previously `getApplicant`. --- .../src/Api/BackendApi/Request/CreateApplicationRequest.php | 6 +++--- .../BackendApi/Request/SalesCreateApplicationRequest.php | 6 +++--- .../custom/asu_application/src/Entity/Application.php | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/public/modules/custom/asu_api/src/Api/BackendApi/Request/CreateApplicationRequest.php b/public/modules/custom/asu_api/src/Api/BackendApi/Request/CreateApplicationRequest.php index eaebbb4b..62c05dd0 100644 --- a/public/modules/custom/asu_api/src/Api/BackendApi/Request/CreateApplicationRequest.php +++ b/public/modules/custom/asu_api/src/Api/BackendApi/Request/CreateApplicationRequest.php @@ -53,7 +53,7 @@ public function toArray(): array { 'application_type' => $this->application->bundle(), 'ssn_suffix' => $this->application->main_applicant[0]->personal_id, 'has_children' => $this->application->getHasChildren(), - 'additional_applicant' => $this->getApplicant(), + 'additional_applicant' => $this->getAdditionalApplicant(), 'right_of_residence' => NULL, 'project_id' => $this->projectUuid, 'apartments' => $this->getApartments(), @@ -105,11 +105,11 @@ protected function getApartments(): array { * @return object * Applicant information. */ - protected function getApplicant(): ?object { + protected function getAdditionalApplicant(): ?object { if (!$this->application->hasAdditionalApplicant()) { return NULL; } - $applicant = $this->application->getApplicant()[0]; + $applicant = $this->application->getAdditionalApplicants()[0]; return (object) [ 'first_name' => $applicant['first_name'], 'last_name' => $applicant['last_name'], diff --git a/public/modules/custom/asu_api/src/Api/BackendApi/Request/SalesCreateApplicationRequest.php b/public/modules/custom/asu_api/src/Api/BackendApi/Request/SalesCreateApplicationRequest.php index f225228b..9b81552e 100644 --- a/public/modules/custom/asu_api/src/Api/BackendApi/Request/SalesCreateApplicationRequest.php +++ b/public/modules/custom/asu_api/src/Api/BackendApi/Request/SalesCreateApplicationRequest.php @@ -61,7 +61,7 @@ public function toArray(): array { 'application_type' => $this->application->bundle(), 'ssn_suffix' => $this->application->main_applicant[0]->personal_id, 'has_children' => $this->application->getHasChildren(), - 'additional_applicant' => $this->getApplicant(), + 'additional_applicant' => $this->getAdditionalApplicant(), 'right_of_residence' => NULL, 'is_new_permit_number' => NULL, 'project_id' => $this->projectUuid, @@ -115,11 +115,11 @@ private function getApartments() { * @return array * Applicant information. */ - private function getApplicant() { + private function getAdditionalApplicant() { if (!$this->application->hasAdditionalApplicant()) { return NULL; } - $applicant = $this->application->getApplicants()[0]; + $applicant = $this->application->getAdditionalApplicants()[0]; return [ 'first_name' => $applicant['first_name'], 'last_name' => $applicant['last_name'], diff --git a/public/modules/custom/asu_application/src/Entity/Application.php b/public/modules/custom/asu_application/src/Entity/Application.php index 5ed6d70b..9ea8dde4 100644 --- a/public/modules/custom/asu_application/src/Entity/Application.php +++ b/public/modules/custom/asu_application/src/Entity/Application.php @@ -144,7 +144,7 @@ public function getMainApplicant(): ?array { * @return array * Array of applicants. */ - public function getApplicant(): array { + public function getAdditionalApplicants(): array { return $this->applicant->getValue() ?? []; }