Announcement

Collapse
No announcement yet.

Reminder validation improvement

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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 $entitystring $fieldData $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!

  • #2
    Ok. .

    Comment


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