- Handled default value on ContextField.

This commit is contained in:
Dev 2025-12-02 00:14:03 +00:00
parent 3d50cfaccb
commit 696386bb06
2 changed files with 30 additions and 4 deletions

View File

@ -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"
}

View File

@ -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;
}
}