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

The Innovative Form Builder – IvyForms, version 0.3.1. 434 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.3.1/code/backend/src/ValueObjects/Field/Field.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.3.1/raw/backend/src/ValueObjects/Field/Field.php
- Modified: 2025-09-04T07:37:14+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.3.1/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\InvalidArgumentException;
use IvyForms\Common\Exceptions\ValidationException;

/**
* Class Field
*
* @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 string
     */
    public string $label;
    /**
     * @var string
     */
    public string $defaultValue;
    /**
     * @var bool
     */
    public bool $required;
    /**
     * @var string
     */
    public string $placeholder;
    /**
     * @var int
     */
    public int $position;
    /**
     * @var bool
     */
    public bool $hideLabel;
    /**
     * @var bool
     */
    public bool $readOnly;
    /**
     * @var string
     */
    public string $description;
    /**
     * @var string
     */
    public string $requiredMessage;
    /**
     * @var string
     */
    public string $cssClasses;

    /**
     * Field constructor.
     * @SuppressWarnings(PHPMD)
     *
     * @param int $id
     * @param int $formId
     * @param int $fieldIndex
     * @param string $type
     * @param string $label
     * @param string $defaultValue
     * @param bool $required
     * @param string $placeholder
     * @param int $position
     * @param bool $hideLabel
     * @param bool $readOnly
     * @param string $description
     * @param string $requiredMessage
     * @param string $cssClasses
     *
     * @throws ValidationException
     */
    public function __construct(
        int $id,
        int $formId,
        int $fieldIndex,
        string $type,
        string $label,
        string $defaultValue,
        bool $required,
        string $placeholder,
        int $position,
        bool $hideLabel,
        bool $readOnly,
        string $description = '',
        string $requiredMessage = '',
        string $cssClasses = ''
    ) {
        $this->id              = $this->validateId($id);
        $this->formId          = $this->validateId($formId) ;
        $this->fieldIndex      = $this->validateId($fieldIndex);
        $this->type            = $this->validateString($type, 50, 'type');
        $this->label           = $this->validateString($label, 255, 'label');
        $this->defaultValue    = $this->validateString($defaultValue, 255, 'defaultValue');
        $this->required        = $this->validateRequired($required);
        $this->placeholder     = $this->validateString($placeholder, 255, 'placeholder');
        $this->position        = $this->validatePosition($position);
        $this->hideLabel       = $this->validateBool($hideLabel);
        $this->readOnly        = $this->validateBool($readOnly);
        $this->description     = $this->validateString($description, 255, 'description');
        $this->requiredMessage = $this->validateString($requiredMessage, 255, 'requiredMessage');
        $this->cssClasses      = $this->validateString($cssClasses, 255, 'cssClasses');
    }

    /**
     * Validates the ID.
     *
     * @param int $id
     *
     * @return int
     * @throws ValidationException
     */
    public function validateId(int $id): int
    {
        if ($id < 0) {
            throw new ValidationException("Id must be a positive integer.");
        }
        return $id;
    }

    /**
     * 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 required field.
     *
     * @param bool $value
     *
     * @return bool
     *
     */
    private function validateRequired(bool $value): bool
    {
        return $value;
    }

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

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

    /**
     * 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 label.
     *
     * @return string
     */
    public function getLabel(): string
    {
        return $this->label;
    }

    /**
     * Get the default value.
     *
     * @return string
     */
    public function getDefaultValue(): string
    {
        return $this->defaultValue;
    }

    /**
     * Check if the field is required.
     *
     * @return bool
     */
    public function isRequired(): bool
    {
        return $this->required;
    }

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

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

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

    /**
     * Set the hideLabel setting.
     *
     * @param bool $hideLabel
     */
    public function setHideLabel(bool $hideLabel): void
    {
        $this->hideLabel = $hideLabel;
    }

    /**
     * Check if the field is read-only.
     *
     * @return bool
     */
    public function isReadOnly(): bool
    {
        return $this->readOnly;
    }

    /**
     * Set the readOnly setting.
     *
     * @param bool $readOnly
     */
    public function setReadOnly(bool $readOnly): void
    {
        $this->readOnly = $readOnly;
    }

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

    /**
     * Set the description setting.
     *
     * @param string $description
     */
    public function setDescription(string $description): void
    {
        $this->description = $description;
    }

    /**
     * Get the field required message.
     *
     * @return string
     */
    public function getRequiredMessage(): string
    {
        return $this->requiredMessage;
    }

    /**
     * Set the required message setting.
     *
     * @param string $requiredMessage
     */
    public function setRequiredMessage(string $requiredMessage): void
    {
        $this->requiredMessage = $requiredMessage;
    }

    /**
     * Get the field CSS classes.
     *
     * @return string
     */
    public function getCssClasses(): string
    {
        return $this->cssClasses;
    }

    /**
     * Set the CSS classes setting.
     *
     * @param string $cssClasses
     */
    public function setCssClasses(string $cssClasses): void
    {
        $this->cssClasses = $cssClasses;
    }

    /**
     * Convert the field to an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'id'              => $this->getId(),
            'formId'          => $this->getFormId(),
            'fieldIndex'      => $this->getIndex(),
            'type'            => $this->getType(),
            'label'           => $this->getLabel(),
            'defaultValue'    => $this->getDefaultValue(),
            'required'        => $this->isRequired(),
            'placeholder'     => $this->getPlaceholder(),
            'position'        => $this->getPosition(),
            'hideLabel'       => $this->isLabelHidden(),
            'readOnly'        => $this->isReadOnly(),
            'description'     => $this->getDescription(),
            'requiredMessage' => $this->getRequiredMessage(),
            'cssClasses'      => $this->getCssClasses(),
        ];
    }
}

```
