# ivyforms/0.2.1/backend/src/Services/Field/FieldService.php

The Innovative Form Builder – IvyForms, version 0.2.1. 131 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.2.1/code/backend/src/Services/Field/FieldService.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.2.1/raw/backend/src/Services/Field/FieldService.php
- Modified: 2025-08-08T07:58:08+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ivyforms/0.2.1/code/backend/src/Services/Field/FieldService.php#L10-L20`.

```php
<?php

namespace IvyForms\Services\Field;

// phpcs:disable PSR1.Files.SideEffects
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

use IvyForms\Common\Exceptions\NotFoundException;
use IvyForms\Common\Exceptions\ValidationException;
use IvyForms\Entity\Field\Field;
use IvyForms\Common\Exceptions\InvalidArgumentException;
use IvyForms\Factory\Field\FieldFactory;
use IvyForms\Repository\Field\FieldRepositoryInterface;

class FieldService
{
    private FieldRepositoryInterface $fieldRepository;

    // Constructor injection for the FieldRepository via PHP-DI
    public function __construct(FieldRepositoryInterface $fieldRepository)
    {
        $this->fieldRepository = $fieldRepository;
    }

    /**
     * Create a new field.
     *
     * @param Field $fieldData
     *
     * @return int Form ID
     *
     * @throws InvalidArgumentException If the field data is invalid
     */
    public function addField(Field $fieldData): int
    {
        // Validate form data
        if (empty($fieldData->getFormId())) {
            throw new InvalidArgumentException('Form id is required.');
        }

        // Create form via repository and return new form ID
        return $this->fieldRepository->add($fieldData);
    }


    /**
     * Update an existing field.
     *
     * @param int $fieldId
     * @param Field $fieldData
     *
     * @return bool
     *
     * @throws InvalidArgumentException If the form data is invalid
     */
    public function updateField(int $fieldId, Field $fieldData): bool
    {
        // Validate form data before updating
        if (empty($fieldData->getId())) {
            throw new InvalidArgumentException('Filed id is required.');
        }

        return $this->fieldRepository->update($fieldId, $fieldData);
    }

    /**
     * Delete a field by its ID.
     *
     * @param int $fieldId
     * @return int
     */
    public function deleteField(int $fieldId): int
    {
        return $this->fieldRepository->delete($fieldId);
    }

    /**
     * Get fields by form ID
     *
     * @param int $id
     *
     * @return mixed[]|null
     */
    public function getAllFields(int $id): ?array
    {
        return $this->fieldRepository->getFieldsById($id);
    }

    /**
     * Delete fields by form ID
     *
     * @param int $id
     *
     * @throws NotFoundException
     * @throws InvalidArgumentException
     */
    public function deleteFieldsByFormId(int $id): void
    {
        $fields = $this->getAllFields($id);

        if (!$fields) {
            return;
        }

        foreach ($fields as $field) {
            $this->deleteField($field->getId());
        }
    }

    /**
     * @param int $originalFormId
     * @param int $newFormId
     *
     * @return void
     * @throws InvalidArgumentException|ValidationException
     */
    public function duplicateFields(int $originalFormId, int $newFormId): void
    {
        $fields = $this->getAllFields($originalFormId);
        foreach ($fields as $field) {
            $fieldData = $field->toArray();
            unset($fieldData['id']);
            $fieldData['formId'] = $newFormId;

            $this->addField(FieldFactory::create($fieldData));
        }
    }
}

```
