From 0e0f004bc1fb43a0d9b48a63b58c11e0e1c20746 Mon Sep 17 00:00:00 2001 From: Dave M Date: Thu, 17 Jul 2025 18:39:57 +0000 Subject: [PATCH] - WIP on Forms --- src/Form/FormTrait.php | 2 +- src/Form/Save.php | 3 +-- src/Lib/FormContext.php | 46 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/Form/FormTrait.php b/src/Form/FormTrait.php index fd861e6..992d84e 100644 --- a/src/Form/FormTrait.php +++ b/src/Form/FormTrait.php @@ -24,7 +24,7 @@ trait FormTrait } } - protected function lang(string $key, array $variables = []) + public function lang(string $key, array $variables = []) { return $this->languageHandler->languageFromKey($key, $variables); } diff --git a/src/Form/Save.php b/src/Form/Save.php index 67794d8..984e4e3 100644 --- a/src/Form/Save.php +++ b/src/Form/Save.php @@ -66,10 +66,9 @@ abstract class Save implements \Picea\Ui\Method\FormInterface { $field = $property->getAttribute(Field::class)->object; if (! $field->readonly || ! $entity->isLoaded()) { $apiField = $property->getAttribute(EntityField::class)->object ?? null; + if ($apiField) { $var = $apiField->field ?: $key; - - if ( isset($context->{$var}) ) { if ($apiField->setterMethod) { # Use a setter method diff --git a/src/Lib/FormContext.php b/src/Lib/FormContext.php index 4c922e9..5520121 100644 --- a/src/Lib/FormContext.php +++ b/src/Lib/FormContext.php @@ -2,4 +2,48 @@ namespace Lean\Api\Lib; -class FormContext extends \Picea\Ui\Method\FormContext { } \ No newline at end of file +use Lean\Api\Factory\MessageFactoryInterface; +use Lean\LanguageHandler; +use Psr\Http\Message\ServerRequestInterface; + +class FormContext extends \Picea\Ui\Method\FormContext { + + public function __construct( + protected LanguageHandler $languageHandler, + protected MessageFactoryInterface $message, + ServerRequestInterface $request, + ? string $formName = null + ) + { + parent::__construct($request, $formName); + } + + public function lang(string $key, array $variables = []) + { + return $this->languageHandler->languageFromKey($key, $variables); + } + + public function pushSuccessMessage($key, $variables = []) { + $this->pushMessage($this->message::generateSuccess( + $this->lang($key, $variables) + )); + } + + public function pushWarningMessage($key, $variables = []) { + $this->pushMessage($this->message::generateWarning( + $this->lang($key, $variables) + )); + } + + public function pushErrorMessage($key, $variables = []) { + $this->pushMessage($this->message::generateError( + $this->lang($key, $variables) + )); + } + + public function pushInfoMessage($key, $variables = []) { + $this->pushMessage($this->message::generateInfo( + $this->lang($key, $variables) + )); + } +} \ No newline at end of file