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