- Big WIP on forms component

This commit is contained in:
mcndave committed 2025-12-01 15:46:02 +00:00
1 parent c0b0681fee
commit 3d50cfaccb
14 files changed
+114 -20

No files matched your search

+38 -3
View File
@@ -5,20 +5,38 @@ namespace Lean\Api\Form;
use CSLSJ\Lean\Form\Session\EmailContext;
use Picea\Ui\Method\{ FormContextInterface, };
use Lean\Api\Attribute\ContextField;
use Lean\Api\Exception\MandatoryFieldException;
use Notes\ObjectReflection;
use Ulmus\Attribute\Property\Field;
use Lean\Api\Attribute\EntityField;
use Ulmus\Entity\EntityInterface;
use Ulmus\Entity\Field\Datetime;
abstract class Save implements \Picea\Ui\Method\FormInterface {
use FormTrait;
abstract class Save extends Form implements \Picea\Ui\Method\FormInterface {
protected string $contextClass;
public function initialize(FormContextInterface $context) : void
{
if ( ! $this->getEntity()->isLoaded() || $context->formSent() ) {
if (method_exists($context, 'initializeEntity')) {
$context->initializeEntity($this->getEntity());
}
else {
$this->assignContextToEntity($context);
}
}
parent::initialize($context);
}
public function validate(FormContextInterface $context) : bool
{
# Context is validating inputs
# Context validates ContextField attributes containing properties on empty entity
$this->validateContextFields($context);
return $context->valid($this->getEntity()->isLoaded() ? $this->getEntity() : null);
}
@@ -61,13 +79,30 @@ abstract class Save implements \Picea\Ui\Method\FormInterface {
return $saved;
}
protected function validateContextFields(FormContextInterface $context) : void
{
foreach (ObjectReflection::fromClass($context::class)->reflectProperties(\ReflectionProperty::IS_PUBLIC) as $name => $property) {
$attribute = $property->getAttribute(ContextField::class);
if ($attribute) {
try {
$attribute->object->assertValueSpecs($context->$name ?? null, $this->getEntity()->isLoaded());
} catch (MandatoryFieldException $e) {
throw new MandatoryFieldException("An error occured with field '$name': " . $e->getMessage());
}
}
}
}
protected function assignContextToEntity(FormContextInterface $context) : void
{
$entity = $this->getEntity();
foreach($entity::resolveEntity()->fieldList() as $key => $property) {
$field = $property->getAttribute(Field::class)->object;
if (! $field->readonly || ! $entity->isLoaded()) {
$apiField = $property->getAttribute(EntityField::class)->object ?? null;
if ($apiField) {