- WIP on save and delete forms
This commit is contained in:
		
							parent
							
								
									ad310ffee5
								
							
						
					
					
						commit
						3ce356e1f2
					
				@ -36,5 +36,5 @@ return [
 | 
			
		||||
        ],*/
 | 
			
		||||
    ],
 | 
			
		||||
 | 
			
		||||
    \Lean\Api\Factory\MessageFactoryInterface::class => DI\autowire(\Lean\Lib\Message::class),
 | 
			
		||||
    \Lean\Api\Factory\MessageFactoryInterface::class => DI\autowire(\Lean\Api\Lib\Message::class),
 | 
			
		||||
];
 | 
			
		||||
@ -11,9 +11,11 @@
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
  "form": {
 | 
			
		||||
    "error": {
 | 
			
		||||
      "entity": "Une propriété $entity est nécessaire dans ce form"
 | 
			
		||||
    },
 | 
			
		||||
    "save": {
 | 
			
		||||
      "error": {
 | 
			
		||||
        "entity": "Une propriété $entity est nécessaire dans ce form",
 | 
			
		||||
        "save": "Une erreur est survenue en tenant de sauvegarder les données",
 | 
			
		||||
        "pdo": "Une erreur est survenue : '{$error}'"
 | 
			
		||||
      },
 | 
			
		||||
 | 
			
		||||
@ -15,6 +15,9 @@ trait DescriptorTrait
 | 
			
		||||
        elseif ($value instanceof \UnitEnum) {
 | 
			
		||||
            return $value::class . "::" . $value->name;
 | 
			
		||||
        }
 | 
			
		||||
        elseif (is_array($value)) {
 | 
			
		||||
            return sprintf('[ %s ]', implode(', ', array_map([ self::class, 'displayValue' ], $value)));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $value;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -9,13 +9,6 @@ use Ulmus\Entity\EntityInterface;
 | 
			
		||||
abstract class Delete implements \Picea\Ui\Method\FormInterface {
 | 
			
		||||
    use FormTrait;
 | 
			
		||||
 | 
			
		||||
    public function getEntity() : EntityInterface
 | 
			
		||||
    {
 | 
			
		||||
        return $this->entity;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function initialize(FormContextInterface $context) : void {}
 | 
			
		||||
 | 
			
		||||
    public function validate(FormContextInterface $context) : bool
 | 
			
		||||
    {
 | 
			
		||||
        if ( ! $this->entity->isLoaded() ) {
 | 
			
		||||
@ -24,7 +17,7 @@ abstract class Delete implements \Picea\Ui\Method\FormInterface {
 | 
			
		||||
            ));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $context->valid();
 | 
			
		||||
        return $context->valid($this->getEntity()->isLoaded() ? $this->getEntity() : null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function execute(FormContextInterface $context) : void
 | 
			
		||||
 | 
			
		||||
@ -4,6 +4,8 @@ namespace Lean\Api\Form;
 | 
			
		||||
 | 
			
		||||
use Lean\Api\Factory\MessageFactoryInterface;
 | 
			
		||||
use Lean\LanguageHandler;
 | 
			
		||||
use Picea\Ui\Method\FormContextInterface;
 | 
			
		||||
use Ulmus\Entity\EntityInterface;
 | 
			
		||||
 | 
			
		||||
trait FormTrait
 | 
			
		||||
{
 | 
			
		||||
@ -13,8 +15,27 @@ trait FormTrait
 | 
			
		||||
        # public EntityInterface $entity,
 | 
			
		||||
    ) {}
 | 
			
		||||
 | 
			
		||||
    public function initialize(FormContextInterface $context) : void
 | 
			
		||||
    {
 | 
			
		||||
        if(isset($this->contextClass) && ! $context instanceof $this->contextClass ) {
 | 
			
		||||
            throw new \LogicException(
 | 
			
		||||
                sprintf("Your context type should be a %s", $this->contextClass)
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function lang(string $key, array $variables = [])
 | 
			
		||||
    {
 | 
			
		||||
        return $this->languageHandler->languageFromKey($key, $variables);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getEntity() : EntityInterface
 | 
			
		||||
    {
 | 
			
		||||
        if (! isset($this->entity)) {
 | 
			
		||||
            throw new \InvalidArgumentException($this->lang("lean.api.form.error.entity"));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $this->entity;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -2,6 +2,7 @@
 | 
			
		||||
 | 
			
		||||
namespace Lean\Api\Form;
 | 
			
		||||
 | 
			
		||||
use CSLSJ\Lean\Form\Session\EmailContext;
 | 
			
		||||
use Picea\Ui\Method\{ FormContextInterface, };
 | 
			
		||||
 | 
			
		||||
use Ulmus\Attribute\Property\Field;
 | 
			
		||||
@ -13,14 +14,7 @@ use Ulmus\Entity\Field\Datetime;
 | 
			
		||||
abstract class Save implements \Picea\Ui\Method\FormInterface {
 | 
			
		||||
    use FormTrait;
 | 
			
		||||
 | 
			
		||||
    public function getEntity() : EntityInterface
 | 
			
		||||
    {
 | 
			
		||||
        if (! isset($this->entity)) {
 | 
			
		||||
            throw new \InvalidArgumentException($this->lang("lean.api.form.save.error.entity"));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $this->entity;
 | 
			
		||||
    }
 | 
			
		||||
    protected string $contextClass;
 | 
			
		||||
 | 
			
		||||
    public function validate(FormContextInterface $context) : bool
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
<?php declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace Lean\Lib;
 | 
			
		||||
namespace Lean\Api\Lib;
 | 
			
		||||
 | 
			
		||||
use Lean\Api\Factory\MessageFactoryInterface;
 | 
			
		||||
use Picea\Ui\Method\FormMessage;
 | 
			
		||||
@ -53,7 +53,7 @@ class Message implements FormMessage, MessageFactoryInterface {
 | 
			
		||||
 | 
			
		||||
    public ? string $header = null;
 | 
			
		||||
 | 
			
		||||
    public function __construct(string $message, string $type = "error", ? string $header = null, ? string $class = null)
 | 
			
		||||
    public function __construct(string $message = "", string $type = "error", ? string $header = null, ? string $class = null)
 | 
			
		||||
    {
 | 
			
		||||
        $this->message = $message;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user