Reminder validation improvement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • a.slyzhko
    Member
    • Oct 2023
    • 90

    Reminder validation improvement

    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:

    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;
        }
    }
    Of course, I can implement some overhead with entity manager hooks, but this improvement would be very helpful.
    Thanks!
  • yuri
    Member
    • Mar 2014
    • 8440

    #2
    Ok. .
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment


    • a.slyzhko
      a.slyzhko commented
      Editing a comment
      Great thanks!
      In which release this change will be available?
Working...