- WIP on form/context init method

This commit is contained in:
Dev 2025-12-02 20:58:13 +00:00
parent 696386bb06
commit ae212a4377
2 changed files with 8 additions and 12 deletions

View File

@ -18,7 +18,7 @@ class ContextField
public ?string $regexFormat = null, public ?string $regexFormat = null,
public ?string $example = null, public ?string $example = null,
public bool $hidden = false, public bool $hidden = false,
public mixed $default = false, public mixed $default = null,
) {} ) {}
public function assertValueSpecs(mixed $value, bool $isNew) : void public function assertValueSpecs(mixed $value, bool $isNew) : void

View File

@ -23,14 +23,13 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface {
public function initialize(FormContextInterface $context) : void public function initialize(FormContextInterface $context) : void
{ {
if ( ! $this->getEntity()->isLoaded() || $context->formSent() ) { if ( ! $this->getEntity()->isLoaded() ) {
if (method_exists($context, 'initializeEntity')) { if (method_exists($context, 'initializeEntity')) {
$context->initializeEntity($this->getEntity()); $context->initializeEntity($this->getEntity());
} }
else {
$this->assignContextToEntity($context); $this->assignContextToEntity($context);
} }
}
parent::initialize($context); parent::initialize($context);
} }
@ -62,21 +61,17 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface {
$this->assignContextToEntity($context); $this->assignContextToEntity($context);
if ( $saved = $entity::repository()->save($entity) ) { if ( $saved = $entity::repository()->save($entity) ) {
$context->pushMessage($this->message::generateSuccess( $context->pushSuccessMessage('lean.api.form.save.success.save');
$this->lang('lean.api.form.save.success.save')
));
} }
else { else {
$context->pushMessage($this->message::generateWarning( $context->pushWarningMessage('lean.api.form.save.warning.entity', [ 'entity' => substr($this->entity::class, strrpos($this->entity::class, '\\') + 1) ]);
$this->lang('lean.api.form.save.warning.entity', [ 'entity' => substr($this->entity::class, strrpos($this->entity::class, '\\') + 1) ])
));
} }
} }
catch(\PDOException $ex) { catch(\PDOException $ex) {
throw new \PDOException($this->lang('lean.api.form.save.error.pdo', [ 'error' => $ex->getMessage() ])); throw new \PDOException($this->lang('lean.api.form.save.error.pdo', [ 'error' => $ex->getMessage() ]));
} }
catch(\Throwable $ex) { catch(\Throwable $ex) {
throw $ex; $context->pushErrorMessage($ex->getMessage());
} }
return $saved; return $saved;
@ -130,6 +125,7 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface {
if ($contextField) { if ($contextField) {
if (isset($contextField->object->default)) { if (isset($contextField->object->default)) {
$entity->$key = $contextField->object->default; $entity->$key = $contextField->object->default;
} }
} }