Skip to content

Commit 5a0ffbd

Browse files
authored
Merge pull request #96 from systopia/do-not-lose-read-only-values-on-submit
Ensure read only values don't get lost on submit
2 parents 473fecb + 9db4256 commit 5a0ffbd

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Form/Control/Util/BasicFormPropertiesFactory.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,18 @@ public static function createFieldProperties(ControlDefinition $definition, Form
8383
'#_nullable' => $definition->isNullable(),
8484
];
8585

86+
$readOnlyValuePath = array_merge(['readOnlyValues'], $definition->getPropertyPath());
8687
if ((!$formState->isCached() || $definition->isReadOnly())
87-
&& $formState->hasTemporaryValue($definition->getPropertyPath())) {
88+
&& $formState->hasTemporaryValue($definition->getPropertyPath())
89+
) {
8890
$form['#default_value'] = $formState->getTemporaryValue($definition->getPropertyPath());
91+
if ($definition->isReadOnly() && !$definition->isCalculated()) {
92+
// Ensure read only values don't get lost on submit.
93+
$formState->set($readOnlyValuePath, $form['#default_value']);
94+
}
95+
}
96+
elseif ($definition->isReadOnly() && $formState->has($readOnlyValuePath)) {
97+
$form['#default_value'] = $formState->get($readOnlyValuePath);
8998
}
9099
elseif (NULL !== $definition->getDefault()) {
91100
$form['#default_value'] = $definition->getDefault();

src/JsonForms/Definition/Control/ControlDefinition.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,17 @@ public function getType(): string {
338338
return $this->propertySchema->type;
339339
}
340340

341+
public function isCalculated(): bool {
342+
return property_exists($this->propertySchema, '$calculate');
343+
}
344+
341345
public function isNullable(): bool {
342346
return in_array('null', (array) $this->propertySchema->type, TRUE);
343347
}
344348

345349
public function isReadOnly(): bool {
346350
return $this->controlSchema->options->readonly ?? $this->propertySchema->readOnly
347-
?? (property_exists($this->propertySchema, '$calculate') || $this->parentUiReadonly);
351+
?? ($this->isCalculated() || $this->parentUiReadonly);
348352
}
349353

350354
/**

0 commit comments

Comments
 (0)