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() ?? []; } diff --git a/public/modules/custom/asu_application/src/Form/ApplicationForm.php b/public/modules/custom/asu_application/src/Form/ApplicationForm.php index 52d94395..6994c08b 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'])) ? (bool) $formValues['applicant'][0]['has_additional_applicant'] : FALSE; + + if ($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);