- 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 string $description = "",
public bool $mandatory = false,
public bool|MandatoryGroup $mandatory = false,
public ?int $minLength = null,
public ?int $maxLength = null,
public ?int $minValue = null,
@ -25,7 +25,7 @@ class ContextField
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.");
}
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 string $messageSuccessSave = 'lean.api.form.save.success.save';
protected string $messageWarningSave = 'lean.api.form.save.warning.entity';
protected array $reflectedProperties;
protected string $contextClass = FormContext::class;
@ -66,12 +70,12 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface {
try {
$this->assignContextToEntity($context);
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 {
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) {

View File

@ -37,7 +37,7 @@ class RouteDescriptor
$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) {
$itemBase = $routeAttribute->object->getBase() ?? $base;
$route = $routeAttribute->object;