BelongsTo( 'MetaTemplates' ); $this->hasMany('MetaFields'); $this->setDisplayField('field'); $this->loadTypeHandlers(); } public function beforeSave($event, $entity, $options) { if (empty($entity->meta_template_id)) { $event->stopPropagation(); $event->setResult(false); return; } } public function validationDefault(Validator $validator): Validator { $validator ->notEmptyString('field') ->notEmptyString('type') ->requirePresence(['field', 'type'], 'create'); return $validator; } public function loadTypeHandlers(): void { if (empty($this->typeHandlers)) { $typeHandlers = [ new TextType(), new IPv4Type(), new IPv6Type(), ]; foreach ($typeHandlers as $handler) { $this->typeHandlers[$handler::TYPE] = $handler; } } } public function getTypeHandlers(): array { return $this->typeHandlers; } public function getTypeHandler($type) { return $this->typeHandlers[$type] ?? false; } }