diff --git a/src/Attribute/ContextField.php b/src/Attribute/ContextField.php index ac2f127..7305f86 100644 --- a/src/Attribute/ContextField.php +++ b/src/Attribute/ContextField.php @@ -18,7 +18,7 @@ class ContextField public ?string $regexFormat = null, public ?string $example = null, public bool $hidden = false, - public mixed $default = false, + public mixed $default = null, ) {} public function assertValueSpecs(mixed $value, bool $isNew) : void diff --git a/src/Form/Save.php b/src/Form/Save.php index c47c34b..cee94c3 100644 --- a/src/Form/Save.php +++ b/src/Form/Save.php @@ -23,13 +23,12 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface { public function initialize(FormContextInterface $context) : void { - if ( ! $this->getEntity()->isLoaded() || $context->formSent() ) { + if ( ! $this->getEntity()->isLoaded() ) { if (method_exists($context, 'initializeEntity')) { $context->initializeEntity($this->getEntity()); } - else { - $this->assignContextToEntity($context); - } + + $this->assignContextToEntity($context); } parent::initialize($context); @@ -62,21 +61,17 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface { $this->assignContextToEntity($context); if ( $saved = $entity::repository()->save($entity) ) { - $context->pushMessage($this->message::generateSuccess( - $this->lang('lean.api.form.save.success.save') - )); + $context->pushSuccessMessage('lean.api.form.save.success.save'); } else { - $context->pushMessage($this->message::generateWarning( - $this->lang('lean.api.form.save.warning.entity', [ 'entity' => substr($this->entity::class, strrpos($this->entity::class, '\\') + 1) ]) - )); + $context->pushWarningMessage('lean.api.form.save.warning.entity', [ 'entity' => substr($this->entity::class, strrpos($this->entity::class, '\\') + 1) ]); } } catch(\PDOException $ex) { throw new \PDOException($this->lang('lean.api.form.save.error.pdo', [ 'error' => $ex->getMessage() ])); } catch(\Throwable $ex) { - throw $ex; + $context->pushErrorMessage($ex->getMessage()); } return $saved; @@ -130,6 +125,7 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface { if ($contextField) { if (isset($contextField->object->default)) { + $entity->$key = $contextField->object->default; } }