. */ namespace SP\Modules\Web\Forms; use SP\Core\Acl\ActionsInterface; use SP\Core\Exceptions\ValidationException; use SP\DataModel\CustomFieldDefinitionData; /** * Class CustomFieldDefForm * * @package SP\Modules\Web\Forms */ final class CustomFieldDefForm extends FormBase implements FormInterface { protected ?CustomFieldDefinitionData $customFieldDefData; /** * Validar el formulario * * @param int $action * @param int|null $id * * @return CustomFieldDefForm|FormInterface * @throws ValidationException */ public function validateFor(int $action, ?int $id = null): FormInterface { switch ($action) { case ActionsInterface::CUSTOMFIELD_CREATE: case ActionsInterface::CUSTOMFIELD_EDIT: $this->analyzeRequestData(); $this->checkCommon(); break; } return $this; } /** * Analizar los datos de la petición HTTP * * @return void */ protected function analyzeRequestData(): void { $this->customFieldDefData = new CustomFieldDefinitionData(); $this->customFieldDefData->setId($this->itemId); $this->customFieldDefData->setName($this->request->analyzeString('name')); $this->customFieldDefData->setTypeId($this->request->analyzeInt('type')); $this->customFieldDefData->setModuleId($this->request->analyzeInt('module')); $this->customFieldDefData->setHelp($this->request->analyzeString('help')); $this->customFieldDefData->setRequired($this->request->analyzeBool('required', false)); $this->customFieldDefData->setIsEncrypted($this->request->analyzeBool('encrypted', false)); } /** * @throws ValidationException */ protected function checkCommon(): void { if (!$this->customFieldDefData->getName()) { throw new ValidationException(__u('Field name not set')); } if (0 === $this->customFieldDefData->getTypeId()) { throw new ValidationException(__u('Field type not set')); } if (0 === $this->customFieldDefData->getModuleId()) { throw new ValidationException(__u('Field module not set')); } } public function getItemData(): ?CustomFieldDefinitionData { return $this->customFieldDefData; } }