# ivyforms/0.4/backend/src/ValueObjects/Field/Field.php

The Innovative Form Builder – IvyForms, version 0.4. 458 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.4/code/backend/src/ValueObjects/Field/Field.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.4/raw/backend/src/ValueObjects/Field/Field.php
- Modified: 2025-09-25T14:53:02+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.4/code/backend/src/ValueObjects/Field/Field.php#L10-L20`.

```php
<?php

/**
* @copyright © TMS-Plugins. All rights reserved.
* @licence   See LICENCE.md for license details.
*/

namespace IvyForms\ValueObjects\Field;

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

use IvyForms\Common\Exceptions\ValidationException;
use IvyForms\Services\Translations\BackendStrings;
use IvyForms\ValueObjects\Field\FieldGeneralSettings;
use IvyForms\ValueObjects\Field\FieldOptions;
use IvyForms\ValueObjects\Field\FieldAdvancedSettings;

/**
* Class Field
*
 * @SuppressWarnings(PHPMD)
* @package IvyForms\ValueObjects\Field
*/
final class Field
{
    /**
     * @var int
     */
    public int $id;
    /**
     * @var int
     */
    public int $formId;
    /**
     * @var int
     */
    public int $fieldIndex;
    /**
     * @var string
     */
    public string $type;
    /**
     * @var int
     */
    public int $position;
    /**
     * @var int|null
     */
    public ?int $parentId;
    /**
     * @var FieldGeneralSettings
     */
    public FieldGeneralSettings $generalSettings;
    /**
     * @var FieldOptions
     */
    public FieldOptions $fieldOptions;
    /**
     * @var int Number of rows (for textarea fields)
     */
    public int $rows;
    /** @var bool */
    public bool $confirmFieldEnabled;
    /** @var string */
    public string $confirmFieldLabel;
    /** @var string */
    public string $confirmFieldPlaceholder;
    /** @var bool */
    public bool $confirmFieldHideLabel;
    /**
     * @var FieldAdvancedSettings
     */
    public FieldAdvancedSettings $advancedSettings;

    /**
     * Field constructor.
     * @SuppressWarnings(PHPMD)
     *
     * @param int $id
     * @param int $formId
     * @param int $fieldIndex
     * @param string $type
     * @param int $position
     * @param int|null $parentId
     * @param FieldGeneralSettings $generalSettings
     * @param FieldOptions $fieldOptions
     * @param int $rows
     * @param bool $confirmFieldEnabled
     * @param string $confirmFieldLabel
     * @param string $confirmFieldPlaceholder
     * @param bool $confirmFieldHideLabel
     *  @param FieldAdvancedSettings $advancedSettings
     *
     * @throws ValidationException
     */
    public function __construct(
        int $id,
        int $formId,
        int $fieldIndex,
        string $type,
        int $position,
        ?int $parentId = null,
        FieldGeneralSettings $generalSettings,
        FieldOptions $fieldOptions,
        FieldAdvancedSettings $advancedSettings,
        int $rows = 0,
        bool $confirmFieldEnabled = false,
        string $confirmFieldLabel = '',
        string $confirmFieldPlaceholder = '',
        bool $confirmFieldHideLabel = false
    ) {
        $this->id = $this->validateId($id);
        $this->formId = $this->validateId($formId);
        $this->fieldIndex = $this->validateId($fieldIndex);
        $this->type = $this->validateString($type, 50, 'type');
        $this->position = $this->validatePosition($position);
        $this->parentId = $this->validateParentId($parentId);
        $this->generalSettings = $generalSettings;
        $this->fieldOptions = $fieldOptions;
        $this->advancedSettings = $advancedSettings;
        $this->rows = $this->validateRows($rows);
        $this->confirmFieldEnabled = $this->validateBool($confirmFieldEnabled);
        $this->confirmFieldLabel = $this->validateString($confirmFieldLabel, 255, 'confirmFieldLabel');
        $this->confirmFieldPlaceholder = $this->validateString(
            $confirmFieldPlaceholder,
            255,
            'confirmFieldPlaceholder'
        );
        $this->confirmFieldHideLabel = $this->validateBool($confirmFieldHideLabel);
    }

    /**
     * Validates the ID.
     *
     * @param int $id
     *
     * @return int
     * @throws ValidationException
     */
    public function validateId(int $id): int
    {
        if ($id < 0) {
            throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']);
        }
        return $id;
    }

    /**
     * Validates parent id which can be null or non-negative int.
     *
     * @param int|null $parentId
     *
     * @return int|null
     * @throws ValidationException
     */
    private function validateParentId(?int $parentId): ?int
    {
        if ($parentId === null) {
            return null;
        }
        if ($parentId < 0) {
            throw new ValidationException(BackendStrings::getExceptionStrings()['parentId_positive_integer']);
        }
        return $parentId;
    }

    /**
     * Validates a string value.
     *
     * @param string $value
     * @param int $maxLength
     * @param string $fieldName
     *
     * @return string
     *
     * @throws ValidationException
     */
    private function validateString(string $value, int $maxLength, string $fieldName): string
    {
        if (strlen($value) > $maxLength) {
            throw new ValidationException(
                sprintf(
                /* translators: 1: String value, 2: String max length. */
                    esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'),
                    esc_html($fieldName),
                    $maxLength
                )
            );
        }
        return $value;
    }

    /**
     * Validates the position.
     *
     * @param int $value
     *
     * @return int
     *
     * @throws ValidationException
     */
    private function validatePosition(int $value): int
    {
        if ($value < 0) {
            throw new ValidationException(
                esc_html__('position must be a non-negative integer.', 'ivyforms')
            );
        }
        return $value;
    }

    /**
     * Validates a boolean field.
     *
     * @param bool $value
     *
     * @return bool
     */
    private function validateBool(bool $value): bool
    {
        return $value;
    }

    /**
     * Validate rows.
     *
     * @param int $rows
     * @return int
     * @throws ValidationException
     */
    private function validateRows(int $rows): int
    {
        if ($rows < 0) {
            throw new ValidationException(
                esc_html__('rows must be at least 1.', 'ivyforms')
            );
        }
        return $rows;
    }

    /**
     * Get the form ID.
     *
     * @return int
     */
    public function getFormId(): int
    {
        return $this->formId;
    }

    /**
     * Get the field ID.
     *
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * Get the field index.
     *
     * @return int
     */
    public function getIndex(): int
    {
        return $this->fieldIndex;
    }

    /**
     * Get the field type.
     *
     * @return string
     */
    public function getType(): string
    {
        return $this->type;
    }

    /**
     * Get the field position.
     *
     * @return int
     */
    public function getPosition(): int
    {
        return $this->position;
    }

    /**
     * Get the parent field id.
     *
     * @return int|null
     */
    public function getParentId(): ?int
    {
        return $this->parentId;
    }

    /**
     * Get the general settings object.
     *
     * @return FieldGeneralSettings
     */
    public function getGeneralSettings(): FieldGeneralSettings
    {
        return $this->generalSettings;
    }

    /**
     * Get the field options.
     *
     * @return FieldOptions
     */
    public function getFieldOptions(): FieldOptions
    {
        return $this->fieldOptions;
    }

    /**
     * Get number of rows.
     * @return int
     */
    public function getRows(): int
    {
        return $this->rows;
    }

    /**
     * Set number of rows.
     * @param int $rows
     */
    public function setRows(int $rows): void
    {
        $this->rows = $rows;
    }

    /**
     * Check if the confirm field feature is enabled.
     *
     * @return bool
     */
    public function isConfirmFieldEnabled(): bool
    {
        return $this->confirmFieldEnabled;
    }

    /**
     * Get the confirm field label.
     *
     * @return string
     */
    public function getConfirmFieldLabel(): string
    {
        return $this->confirmFieldLabel;
    }

    /**
     * Get the confirm field placeholder.
     *
     * @return string
     */
    public function getConfirmFieldPlaceholder(): string
    {
        return $this->confirmFieldPlaceholder;
    }

    /**
     * Check if the confirm field label should be hidden.
     *
     * @return bool
     */
    public function isConfirmFieldHideLabel(): bool
    {
        return $this->confirmFieldHideLabel;
    }

    /**
     * Set the confirm field feature enabled/disabled.
     *
     * @param bool $enabled
     */
    public function setConfirmFieldEnabled(bool $enabled): void
    {
        $this->confirmFieldEnabled = $enabled;
    }

    /**
     * Set the confirm field label.
     *
     * @param string $label
     */
    public function setConfirmFieldLabel(string $label): void
    {
        $this->confirmFieldLabel = $label;
    }

    /**
     * Set the confirm field placeholder.
     *
     * @param string $ph
     */
    public function setConfirmFieldPlaceholder(string $ph): void
    {
        $this->confirmFieldPlaceholder = $ph;
    }

    /**
     * Set the confirm field label visibility.
     *
     * @param bool $hide
     */
    public function setConfirmFieldHideLabel(bool $hide): void
    {
        $this->confirmFieldHideLabel = $hide;
    }

    /**
     * Get the general settings object.
     *
     * @return FieldAdvancedSettings
     */
    public function getAdvancedSettings(): FieldAdvancedSettings
    {
        return $this->advancedSettings;
    }

    /**
     * Convert the field to an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'id'                    => $this->id,
            'formId'                => $this->formId,
            'fieldIndex'            => $this->fieldIndex,
            'type'                  => $this->type,
            'position'              => $this->position,
            'parentId'              => $this->parentId,
            'generalSettings'       => $this->generalSettings->toArray(),
            'fieldOptions'          => $this->fieldOptions->toArray(),
            'rows'                  => $this->rows,
            'confirmFieldEnabled'   => $this->confirmFieldEnabled,
            'confirmFieldLabel'     => $this->confirmFieldLabel,
            'confirmFieldPlaceholder' => $this->confirmFieldPlaceholder,
            'confirmFieldHideLabel' => $this->confirmFieldHideLabel,
            // Advanced options
            'advancedSettings'      => $this->advancedSettings->toArray()
        ];
    }
}

```
