- WIP on validations

This commit is contained in:
Dev 2026-06-09 19:30:53 +00:00
parent c82eae5be4
commit f292ac63ca
5 changed files with 29 additions and 6 deletions

View File

@ -0,0 +1,8 @@
<?php
namespace Lean\Api\Attribute;
enum CommonRegexFormatEnum : string
{
case BasicEmail = "/[^@ \t\r\n]+@[^@ \t\r\n]+\.[^@ \t\r\n]+/i";
}

View File

@ -12,7 +12,7 @@ class ContextField
{ {
public function __construct( public function __construct(
public string $description = "", public string $description = "",
public bool $mandatory = false, public bool|MandatoryGroup $mandatory = false,
public ?int $minLength = null, public ?int $minLength = null,
public ?int $maxLength = null, public ?int $maxLength = null,
public ?int $minValue = null, public ?int $minValue = null,
@ -25,7 +25,7 @@ class ContextField
public function assertValueSpecs(mixed $value, bool $isNew) : void public function assertValueSpecs(mixed $value, bool $isNew) : void
{ {
if ($isNew && ( $this->mandatory && $value === null )) { if ($isNew && ( $this->mandatory === true && $value === null )) {
throw new MandatoryFieldException("Mandatory field must be provided a value."); throw new MandatoryFieldException("Mandatory field must be provided a value.");
} }
elseif ($value !== null) { elseif ($value !== null) {

View File

@ -0,0 +1,11 @@
<?php
namespace Lean\Api\Attribute;
readonly class MandatoryGroup
{
public function __construct(
public string $name,
public int $needed = 1,
) {}
}

View File

@ -22,6 +22,10 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface {
protected bool $skipEntityLastModified = false; protected bool $skipEntityLastModified = false;
protected string $messageSuccessSave = 'lean.api.form.save.success.save';
protected string $messageWarningSave = 'lean.api.form.save.warning.entity';
protected array $reflectedProperties; protected array $reflectedProperties;
protected string $contextClass = FormContext::class; protected string $contextClass = FormContext::class;
@ -68,10 +72,10 @@ 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) ) {
method_exists($context, 'pushSuccessMessage') && $context->pushSuccessMessage('lean.api.form.save.success.save'); method_exists($context, 'pushSuccessMessage') && $context->pushSuccessMessage($this->messageSuccessSave);
} }
else { else {
method_exists($context, 'pushWarningMessage') && $context->pushWarningMessage('lean.api.form.save.warning.entity', [ 'entity' => substr($this->entity::class, strrpos($this->entity::class, '\\') + 1) ]); method_exists($context, 'pushWarningMessage') && $context->pushWarningMessage($this->messageWarningSave, [ 'entity' => substr($this->entity::class, strrpos($this->entity::class, '\\') + 1) ]);
} }
} }
catch(\PDOException $ex) { catch(\PDOException $ex) {

View File

@ -37,7 +37,7 @@ class RouteDescriptor
$base = $attribute ? $attribute->object->getBase() : ""; $base = $attribute ? $attribute->object->getBase() : "";
foreach($reflector->reflectMethods() as $method) { foreach($reflector->reflectMethods(includeParents: (bool) $attribute->object->inheritRoutes) as $method) {
foreach(array_reverse($method->getAttributes(Route::class)) as $routeAttribute) { foreach(array_reverse($method->getAttributes(Route::class)) as $routeAttribute) {
$itemBase = $routeAttribute->object->getBase() ?? $base; $itemBase = $routeAttribute->object->getBase() ?? $base;
$route = $routeAttribute->object; $route = $routeAttribute->object;