From 696386bb060303c6bb67ccbce9de198954f5007d Mon Sep 17 00:00:00 2001 From: Dev Date: Tue, 2 Dec 2025 00:14:03 +0000 Subject: [PATCH] - Handled default value on ContextField. --- meta/i18n/fr/lean.api.json | 4 +++- src/Form/Save.php | 30 +++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/meta/i18n/fr/lean.api.json b/meta/i18n/fr/lean.api.json index 01ebd76..99a7bc8 100644 --- a/meta/i18n/fr/lean.api.json +++ b/meta/i18n/fr/lean.api.json @@ -16,9 +16,11 @@ }, "save": { "error": { - "entity": "Une erreur est survenue en tenant de sauvegarder les données pour l'entité {$entity}.", "pdo": "Une erreur est survenue : '{$error}'" }, + "warning": { + "entity": "Aucun changement apporté pour l'objet '{$entity}'." + }, "success": { "save": "Sauvegarde effectué avec succès" } diff --git a/src/Form/Save.php b/src/Form/Save.php index c906545..c47c34b 100644 --- a/src/Form/Save.php +++ b/src/Form/Save.php @@ -7,6 +7,7 @@ use Picea\Ui\Method\{ FormContextInterface, }; use Lean\Api\Attribute\ContextField; use Lean\Api\Exception\MandatoryFieldException; +use Notes\Common\ReflectedProperty; use Notes\ObjectReflection; use Ulmus\Attribute\Property\Field; use Lean\Api\Attribute\EntityField; @@ -16,6 +17,8 @@ use Ulmus\Entity\Field\Datetime; abstract class Save extends Form implements \Picea\Ui\Method\FormInterface { + protected array $reflectedProperties; + protected string $contextClass; public function initialize(FormContextInterface $context) : void @@ -60,12 +63,12 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface { if ( $saved = $entity::repository()->save($entity) ) { $context->pushMessage($this->message::generateSuccess( - $this->lang('lean.api.form.save.success.entity') + $this->lang('lean.api.form.save.success.save') )); } else { $context->pushMessage($this->message::generateWarning( - $this->lang('lean.api.form.save.error.entity', [ 'entity' => $this->entity::class ]) + $this->lang('lean.api.form.save.warning.entity', [ 'entity' => substr($this->entity::class, strrpos($this->entity::class, '\\') + 1) ]) )); } } @@ -81,7 +84,7 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface { protected function validateContextFields(FormContextInterface $context) : void { - foreach (ObjectReflection::fromClass($context::class)->reflectProperties(\ReflectionProperty::IS_PUBLIC) as $name => $property) { + foreach ($this->reflectedProperties($context) as $name => $property) { $attribute = $property->getAttribute(ContextField::class); if ($attribute) { @@ -118,8 +121,29 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface { $entity->$key = $context->{$var}; } } + elseif (! $entity->isLoaded()) { + # Applies 'default' value assigned to ContextField property + $attr = $this->reflectedProperties($context)[$var] ?? null; + + if ($attr) { + $contextField = $attr->getAttribute(ContextField::class); + + if ($contextField) { + if (isset($contextField->object->default)) { + $entity->$key = $contextField->object->default; + } + } + } + } } } } } + + private function reflectedProperties(FormContextInterface $context) : array + { + $this->reflectedProperties ??= ObjectReflection::fromClass($context::class)->reflectProperties(\ReflectionProperty::IS_PUBLIC); + + return $this->reflectedProperties; + } }