Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
33 / 33 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| Input | |
100.00% |
33 / 33 |
|
100.00% |
9 / 9 |
17 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| setRequired | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| setDisabled | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| setReadonly | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| setValue | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| resetValue | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| validate | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Form\Element; |
| 15 | |
| 16 | /** |
| 17 | * Form input element class |
| 18 | * |
| 19 | * @category Pop |
| 20 | * @package Pop\Form |
| 21 | * @author Nick Sagona, III <dev@noladev.com> |
| 22 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 23 | * @license https://www.popphp.org/license New BSD License |
| 24 | * @version 4.2.6 |
| 25 | */ |
| 26 | |
| 27 | class Input extends AbstractElement |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Constructor |
| 32 | * |
| 33 | * Instantiate the form input element, defaults to text |
| 34 | * |
| 35 | * @param string $name |
| 36 | * @param string $type |
| 37 | * @param ?string $value |
| 38 | * @param ?string $indent |
| 39 | */ |
| 40 | public function __construct(string $name, string $type = 'text', ?string $value = null, ?string $indent = null) |
| 41 | { |
| 42 | parent::__construct('input'); |
| 43 | |
| 44 | $this->setName($name); |
| 45 | $this->setAttributes([ |
| 46 | 'type' => $type, |
| 47 | 'name' => $name, |
| 48 | 'id' => $name, |
| 49 | 'value' => $value |
| 50 | ]); |
| 51 | |
| 52 | if ($indent !== null) { |
| 53 | $this->setIndent($indent); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Set whether the form element is required |
| 59 | * |
| 60 | * @param bool $required |
| 61 | * @param ?string $requiredMessage |
| 62 | * @return Input |
| 63 | */ |
| 64 | public function setRequired(bool $required, ?string $requiredMessage = 'This field is required.'): Input |
| 65 | { |
| 66 | if ($required) { |
| 67 | $this->setAttribute('required', 'required'); |
| 68 | } else { |
| 69 | $this->removeAttribute('required'); |
| 70 | } |
| 71 | return parent::setRequired($required, $requiredMessage); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Set whether the form element is disabled |
| 76 | * |
| 77 | * @param bool $disabled |
| 78 | * @return Input |
| 79 | */ |
| 80 | public function setDisabled(bool $disabled): Input |
| 81 | { |
| 82 | if ($disabled) { |
| 83 | $this->setAttribute('disabled', 'disabled'); |
| 84 | } else { |
| 85 | $this->removeAttribute('disabled'); |
| 86 | } |
| 87 | return parent::setDisabled($disabled); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set whether the form element is readonly |
| 92 | * |
| 93 | * @param bool $readonly |
| 94 | * @return Input |
| 95 | */ |
| 96 | public function setReadonly(bool $readonly): Input |
| 97 | { |
| 98 | if ($readonly) { |
| 99 | $this->setAttribute('readonly', 'readonly'); |
| 100 | } else { |
| 101 | $this->removeAttribute('readonly'); |
| 102 | } |
| 103 | return parent::setReadonly($readonly); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Set the value of the form input element object |
| 108 | * |
| 109 | * @param mixed $value |
| 110 | * @return Input |
| 111 | */ |
| 112 | public function setValue(mixed $value = null): Input |
| 113 | { |
| 114 | $this->setAttribute('value', $value); |
| 115 | return $this; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Reset the value of the form element |
| 120 | * |
| 121 | * @return Input |
| 122 | */ |
| 123 | public function resetValue(): Input |
| 124 | { |
| 125 | $this->setAttribute('value', ''); |
| 126 | return $this; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get the value of the form input element object |
| 131 | * |
| 132 | * @return ?string |
| 133 | */ |
| 134 | public function getValue(): ?string |
| 135 | { |
| 136 | return $this->getAttribute('value'); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get the type of the form input element object |
| 141 | * |
| 142 | * @return ?string |
| 143 | */ |
| 144 | public function getType(): ?string |
| 145 | { |
| 146 | return $this->getAttribute('type'); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Validate the form element object |
| 151 | * |
| 152 | * @param array $formValues |
| 153 | * @return bool |
| 154 | */ |
| 155 | public function validate(array $formValues = []): bool |
| 156 | { |
| 157 | $value = $this->getValue(); |
| 158 | |
| 159 | // Check if the element is required |
| 160 | if (($this->required) && empty($value) && !($this->getType() == 'number' && $value === '0')) { |
| 161 | $this->errors[] = $this->getRequiredMessage(); |
| 162 | } |
| 163 | |
| 164 | $this->validateValue($value, $formValues); |
| 165 | |
| 166 | return (count($this->errors) == 0); |
| 167 | } |
| 168 | |
| 169 | } |