Skip to content

Commit

Permalink
32988 Range fields can not be set as required (#501)
Browse files Browse the repository at this point in the history
* update allow required range fields

* format code

* refactor code

* 32988 range fields can not be set as required

* refactor code

* refactor code

* update unit test
  • Loading branch information
yeneastgate committed May 3, 2023
1 parent fe6d692 commit a8cabfe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
4 changes: 3 additions & 1 deletion plugin/FormData.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public function getMissingFields(): array
$missing = [];

if ($this->_formSent) {
$filledFormData = array_filter($this->_values);
$filledFormData = array_filter($this->_values, function ($v) {
return $v !== 0;
});
$requiredFields = array_flip($this->_requiredFields);
$filled = array_intersect_key($filledFormData, $requiredFields);
$missingKeyValues = array_diff_key($requiredFields, $filled);
Expand Down
24 changes: 23 additions & 1 deletion plugin/FormPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ private function buildFormData(DataFormConfiguration $pFormConfig, $formNo): For
$formFields = $this->getAllowedPostVars($pFormConfig);
$formData = $pFormFieldValidator->getValidatedValues($formFields, $this->_pFieldsCollection);
$requiredFields = $this->getAllowedRequiredFields($requiredFields);
$requiredFieldsAreAllowed = $this->getAllowedRequiredFieldsIsRangeField($requiredFields);
$pFormData = new FormData($pFormConfig, $formNo);
$pFormData->setRequiredFields($requiredFields);
$pFormData->setRequiredFields($requiredFieldsAreAllowed);
$pFormData->setFormtype($pFormConfig->getFormType());
$pFormData->setValues($formData);

Expand Down Expand Up @@ -269,6 +270,27 @@ protected function getAllowedRequiredFields($requiredFields): array
}
return $activeRequiredFields;
}

/**
*
* @param array $requiredFields
* @return string[]
*
*/

private function getAllowedRequiredFieldsIsRangeField(array $requiredFields): array
{
foreach ($requiredFields as $key => $value) {
$isRangeField = $this->_pFieldsCollection->getFieldByKeyUnsafe($value)->getIsRangeField();
if ($isRangeField) {
array_push($requiredFields, $value.'__von');
array_push($requiredFields, $value.'__bis');
unset($requiredFields[$key]);
}
}

return $requiredFields;
}


/**
Expand Down
27 changes: 23 additions & 4 deletions tests/TestClassFormPostInterest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public function prepare()
'kaufpreis__von' => onOfficeSDK::MODULE_SEARCHCRITERIA,
'kaufpreis__bis' => onOfficeSDK::MODULE_SEARCHCRITERIA,
'krit_bemerkung_oeffentlich' => onOfficeSDK::MODULE_SEARCHCRITERIA,
'stp_anzahl__von' => onOfficeSDK::MODULE_SEARCHCRITERIA,
'stp_anzahl__bis' => onOfficeSDK::MODULE_SEARCHCRITERIA,
]));

$this->_pFieldsCollectionBuilderShort = $this->getMockBuilder(FieldsCollectionBuilderShort::class)
Expand Down Expand Up @@ -136,6 +138,11 @@ public function prepare()
$pField4->setLabel('Comment');
$pFieldsCollection->addField($pField4);

$pField5 = new Field('stp_anzahl', onOfficeSDK::MODULE_SEARCHCRITERIA);
$pField5->setType(FieldTypes::FIELD_TYPE_FLOAT);
$pField5->setIsRangeField(true);
$pFieldsCollection->addField($pField5);

return $this->_pFieldsCollectionBuilderShort;
}));

Expand Down Expand Up @@ -174,6 +181,11 @@ public function prepare()
$pField2->setLabel('Kaufpreis');
$pFieldsCollection->addField($pField2);

$pFieldStpAnzahl = new Field('stp_anzahl', onOfficeSDK::MODULE_ESTATE);
$pFieldStpAnzahl->setType(FieldTypes::FIELD_TYPE_FLOAT);
$pFieldStpAnzahl->setLabel('Stp_anzahl');
$pFieldsCollection->addField($pFieldStpAnzahl);

return $this->_pFieldsCollectionBuilderShort;
}));

Expand Down Expand Up @@ -216,7 +228,9 @@ public function testInitialCheck()
'kaufpreis__von' => '200000.00',
'kaufpreis__bis' => '800000.00',
'objekttyp' => ['reihenendhaus', 'reihenhaus'],
'krit_bemerkung_oeffentlich' => 'comment3'
'krit_bemerkung_oeffentlich' => 'comment3',
'stp_anzahl__von' => '20.00',
'stp_anzahl__bis' => '40.00',
];

$pConfig = $this->getNewDataFormConfigurationInterest();
Expand Down Expand Up @@ -245,6 +259,8 @@ public function testUnsuccessful()
'vermarktungsart' => 'kauf',
'kaufpreis__von' => '200000.00',
'kaufpreis__bis' => '800000.00',
'stp_anzahl__von' => '20.00',
'stp_anzahl__bis' => '40.00'
];

$unsuccessfulCombinations = [
Expand Down Expand Up @@ -287,7 +303,7 @@ public function testMissingFields()
$pFormData = $this->_pFormPostInterest->getFormDataInstance('interestform', 3);

$this->assertEquals(FormPost::MESSAGE_REQUIRED_FIELDS_MISSING, $pFormData->getStatus());
$this->assertEquals(['Name', 'vermarktungsart'], $pFormData->getMissingFields());
$this->assertEquals(['Name', 'vermarktungsart', 'stp_anzahl__von' , 'stp_anzahl__bis'], $pFormData->getMissingFields());
}


Expand All @@ -305,11 +321,12 @@ private function getNewDataFormConfigurationInterest(): DataFormConfigurationInt
$pConfig->addInput('Email', onOfficeSDK::MODULE_ADDRESS);
$pConfig->addInput('vermarktungsart', onOfficeSDK::MODULE_SEARCHCRITERIA);
$pConfig->addInput('kaufpreis', onOfficeSDK::MODULE_SEARCHCRITERIA);
$pConfig->addInput('stp_anzahl', onOfficeSDK::MODULE_SEARCHCRITERIA);
$pConfig->addInput('message', '');
$pConfig->addInput('krit_bemerkung_oeffentlich', onOfficeSDK::MODULE_SEARCHCRITERIA);
$pConfig->setFormName('interestform');
$pConfig->setRecipient('test@my-onoffice.com');
$pConfig->setRequiredFields(['Vorname', 'Name', 'vermarktungsart']);
$pConfig->setRequiredFields(['Vorname', 'Name', 'vermarktungsart', 'stp_anzahl']);
$pConfig->setSubject('Interest');
$pConfig->setTemplate('testtemplate.php');
$pConfig->setFormType(Form::TYPE_INTEREST);
Expand Down Expand Up @@ -381,6 +398,8 @@ private function addApiResponseCreateSearchCriteria(bool $success)
'krit_bemerkung_oeffentlich' => 'comment3',
'kaufpreis__von' => 200000.00,
'kaufpreis__bis' => 800000.00,
'stp_anzahl__von' => 20.00,
'stp_anzahl__bis' => 40.00,
],
'addressid' => 294,
];
Expand Down Expand Up @@ -433,7 +452,7 @@ private function addApiResponseSendMail(bool $success)
'Name' => 'Doe',
'Email' => 'john@doemail.com'
],
'message' => "\nSuchkriterien des Interessenten:\nVermarktungsart: Kauf\nComment: comment3\nKaufpreis (min): 200000\nKaufpreis (max): 800000",
'message' => "\nSuchkriterien des Interessenten:\nVermarktungsart: Kauf\nComment: comment3\nKaufpreis (min): 200000\nKaufpreis (max): 800000\nStp_anzahl (min): 20\nStp_anzahl (max): 40",
'subject' => 'Interest',
'formtype' => Form::TYPE_INTEREST,
"referrer" => "",
Expand Down

0 comments on commit a8cabfe

Please sign in to comment.