Fix logic on hidden, loading on edit submission (#75)
This commit is contained in:
@@ -94,7 +94,7 @@ class FormLogicConditionChecker
|
||||
return str_starts_with($fieldValue, $condition['value']);
|
||||
}
|
||||
|
||||
private function checkendsWith ($condition, $fieldValue): bool {
|
||||
private function checkEndsWith ($condition, $fieldValue): bool {
|
||||
return str_ends_with($fieldValue, $condition['value']);
|
||||
}
|
||||
|
||||
@@ -206,7 +206,7 @@ class FormLogicConditionChecker
|
||||
case 'starts_with':
|
||||
return $this->checkStartsWith($propertyCondition, $value);
|
||||
case 'ends_with':
|
||||
return $this->checkendsWith($propertyCondition, $value);
|
||||
return $this->checkEndsWith($propertyCondition, $value);
|
||||
case 'is_empty':
|
||||
return $this->checkIsEmpty($propertyCondition, $value);
|
||||
case 'is_not_empty':
|
||||
|
||||
@@ -23,6 +23,11 @@ class FormLogicPropertyResolver
|
||||
return (new self($property, $values))->shouldBeRequired();
|
||||
}
|
||||
|
||||
public static function isHidden(array $property, array $values): bool
|
||||
{
|
||||
return (new self($property, $values))->shouldBeHidden();
|
||||
}
|
||||
|
||||
public function shouldBeRequired(): bool
|
||||
{
|
||||
if(!isset($this->property['required'])){
|
||||
@@ -42,4 +47,24 @@ class FormLogicPropertyResolver
|
||||
return $this->property['required'];
|
||||
}
|
||||
}
|
||||
|
||||
public function shouldBeHidden(): bool
|
||||
{
|
||||
if (! isset($this->property['hidden'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->logic) {
|
||||
return $this->property['hidden'];
|
||||
}
|
||||
|
||||
$conditionsMet = FormLogicConditionChecker::conditionsMet($this->logic['conditions'], $this->formData);
|
||||
if ($conditionsMet && $this->property['hidden'] && count($this->logic['actions']) > 0 && in_array('show-block', $this->logic['actions'])) {
|
||||
return false;
|
||||
} elseif ($conditionsMet && !$this->property['hidden'] && count($this->logic['actions']) > 0 && in_array('hide-block', $this->logic['actions'])) {
|
||||
return true;
|
||||
} else {
|
||||
return $this->property['hidden'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,8 +29,14 @@ class FormSubmissionFormatter
|
||||
|
||||
private $showRemovedFields = false;
|
||||
|
||||
/**
|
||||
* Logic resolver needs an array id => value, so we create it here
|
||||
*/
|
||||
private $idFormData = null;
|
||||
|
||||
public function __construct(private Form $form, private array $formData)
|
||||
{
|
||||
$this->initIdFormData();
|
||||
}
|
||||
|
||||
public function createLinks()
|
||||
@@ -88,9 +94,9 @@ class FormSubmissionFormatter
|
||||
continue;
|
||||
}
|
||||
|
||||
// If should hide hidden fields
|
||||
// If hide hidden fields
|
||||
if (!$this->showHiddenFields) {
|
||||
if (isset($field['hidden']) && $field['hidden']) {
|
||||
if (FormLogicPropertyResolver::isHidden($field, $this->idFormData)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -149,7 +155,7 @@ class FormSubmissionFormatter
|
||||
|
||||
// If hide hidden fields
|
||||
if (!$this->showHiddenFields) {
|
||||
if (isset($field['hidden']) && $field['hidden']) {
|
||||
if (FormLogicPropertyResolver::isHidden($field, $this->idFormData)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -204,4 +210,16 @@ class FormSubmissionFormatter
|
||||
return $transformedFields;
|
||||
}
|
||||
|
||||
private function initIdFormData() {
|
||||
$formProperties = collect($this->form->properties);
|
||||
foreach ($this->formData as $key => $value) {
|
||||
$property = $formProperties->first(function ($item) use ($key) {
|
||||
return $item['id'] == $key;
|
||||
});
|
||||
if ($property) {
|
||||
$this->idFormData[$property['id']] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user