Hi there!
I'm working on new reminder type. But I encountered issue with validation when saving reminder with new custom type.
In class Espo\Modules\Crm\Classes\FieldValidators\Event\Rem inders\Valid on 69 line. I have a suggestion to refactor this class to something like that:
	
Of course, I can implement some overhead with entity manager hooks, but this improvement would be very helpful.
Thanks!
					I'm working on new reminder type. But I encountered issue with validation when saving reminder with new custom type.
In class Espo\Modules\Crm\Classes\FieldValidators\Event\Rem inders\Valid on 69 line. I have a suggestion to refactor this class to something like that:
PHP Code:
	
<?php
namespace Espo\Modules\Crm\Classes\FieldValidators\Event\Reminders;
use Espo\Core\FieldValidation\Validator;
use Espo\Core\FieldValidation\Validator\Data;
use Espo\Core\FieldValidation\Validator\Failure;
use Espo\Modules\Crm\Entities\Reminder;
use Espo\ORM\Entity;
use stdClass;
use Espo\ORM\Defs as OrmDefs;
/**
 * @implements Validator<Entity>
 */
class Valid implements Validator
{
    public function __construct(
        private OrmDefs $ormDefs
    ){}
    public function validate(Entity $entity, string $field, Data $data): ?Failure
    {
        $list = $entity->get($field);
        if ($list === null) {
            return null;
        }
        $reminderTypeList = $this->ormDefs
            ->getEntity(Reminder::ENTITY_TYPE)
            ->getField('type')
            ->getParam('options') ?? [];
        foreach ($list as $item) {
            if (!$item instanceof stdClass) {
                return Failure::create();
            }
            $seconds = $item->seconds ?? null;
            $type = $item->type ?? null;
            if (!is_int($seconds)) {
                return Failure::create();
            }
            if ($seconds < 0) {
                return Failure::create();
            }
            if (!in_array($type, $reminderTypeList)) {
                return Failure::create();
            }
        }
        return null;
    }
}
Thanks!

Comment