Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"require": {
"php": ">=8.2",
"ext-fileinfo": "*",
"ipl/stdlib": ">=0.12.0",
"ipl/stdlib": ">=0.15.0",
"ipl/validator": ">=0.5.0",
"psr/http-message": "^1.1",
"guzzlehttp/psr7": "^2.5"
Expand Down
18 changes: 17 additions & 1 deletion src/FormElement/BaseFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ipl\Html\Form;
use ipl\I18n\Translation;
use ipl\Stdlib\Messages;
use ipl\Stdlib\Option;
use ipl\Validator\ValidatorChain;
use ReflectionProperty;

Expand Down Expand Up @@ -54,9 +55,17 @@ abstract class BaseFormElement extends BaseHtmlElement implements FormElement, V
public function __construct($name, $attributes = null)
{
$this->setName($name);

if ($attributes === null || is_array($attributes)) {
// This may look inefficient, but we have to resolve options even if there are none to be set.
// Options may be required, and without resolving them, such constraints would not be checked.
$attributes ??= [];
Option::resolveOptions($this, $attributes);
}

$this->init();

if ($attributes !== null) {
if (! empty($attributes)) {
$this->addAttributes($attributes);
}
}
Expand All @@ -73,6 +82,7 @@ public function getDescription()
*
* @return $this
*/
#[Option]
public function setDescription($description)
{
$this->description = $description;
Expand All @@ -92,6 +102,7 @@ public function getLabel()
*
* @return $this
*/
#[Option]
public function setLabel($label)
{
$this->label = $label;
Expand All @@ -111,6 +122,7 @@ public function getName()
*
* @return $this
*/
#[Option]
public function setName($name)
{
$this->name = $name;
Expand All @@ -130,6 +142,7 @@ public function isIgnored()
*
* @return $this
*/
#[Option]
public function setIgnored($ignored = true)
{
$this->ignored = (bool) $ignored;
Expand All @@ -149,6 +162,7 @@ public function isRequired()
*
* @return $this
*/
#[Option]
public function setRequired($required = true)
{
$this->required = (bool) $required;
Expand Down Expand Up @@ -188,6 +202,7 @@ public function getValidators()
*
* @return $this
*/
#[Option]
public function setValidators($validators)
{
$this
Expand Down Expand Up @@ -224,6 +239,7 @@ public function getValue()
return $this->value;
}

#[Option]
public function setValue($value)
{
if ($value === '') {
Expand Down
4 changes: 4 additions & 0 deletions src/FormElement/CheckboxElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ipl\Html\FormElement;

use ipl\Html\Attributes;
use ipl\Stdlib\Option;

class CheckboxElement extends InputElement
{
Expand Down Expand Up @@ -34,6 +35,7 @@ public function isChecked()
*
* @return $this
*/
#[Option]
public function setChecked($checked)
{
$this->checked = (bool) $checked;
Expand All @@ -58,6 +60,7 @@ public function getCheckedValue()
*
* @return $this
*/
#[Option]
public function setCheckedValue($checkedValue)
{
$this->checkedValue = $checkedValue;
Expand All @@ -82,6 +85,7 @@ public function getUncheckedValue()
*
* @return $this
*/
#[Option]
public function setUncheckedValue($uncheckedValue)
{
$this->uncheckedValue = $uncheckedValue;
Expand Down
2 changes: 2 additions & 0 deletions src/FormElement/FileElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use ipl\I18n\Translation;
use ipl\Stdlib\Option;
use ipl\Validator\FileValidator;
use ipl\Validator\ValidatorChain;
use Psr\Http\Message\UploadedFileInterface;
Expand Down Expand Up @@ -78,6 +79,7 @@ public function getDestination(): ?string
*
* @return $this
*/
#[Option]
public function setDestination(string $path): self
{
$this->destination = $path;
Expand Down
10 changes: 9 additions & 1 deletion src/FormElement/FormElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ipl\Html\FormDecorator\DecoratorInterface;
use ipl\Html\ValidHtml;
use ipl\Stdlib\Events;
use ipl\Stdlib\Option;
use ipl\Stdlib\Plugins;
use UnexpectedValueException;

Expand Down Expand Up @@ -157,7 +158,14 @@ public function createElement($type, $name, $options = null)
/** @var FormElement $element */
$element = new $class($name);

if ($options !== null) {
if ($options === null || is_array($options)) {
// This may look inefficient, but we have to resolve options even if there are none to be set.
// Options may be required, and without resolving them, such constraints would not be checked.
$options ??= [];
Option::resolveOptions($element, $options);
}

if (! empty($options)) {
$element->addAttributes($options);
}

Expand Down
3 changes: 2 additions & 1 deletion src/FormElement/InputElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace ipl\Html\FormElement;

use ipl\Html\Attribute;
use ipl\Html\Attributes;
use ipl\Stdlib\Option;

class InputElement extends BaseFormElement
{
Expand All @@ -29,6 +29,7 @@ public function getType()
*
* @return $this
*/
#[Option]
public function setType($type)
{
$this->type = (string) $type;
Expand Down
3 changes: 3 additions & 0 deletions src/FormElement/RadioElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use ipl\I18n\Translation;
use ipl\Stdlib\Option;
use ipl\Validator\DeferredInArrayValidator;
use ipl\Validator\ValidatorChain;

Expand All @@ -31,6 +32,7 @@ class RadioElement extends BaseFormElement
*
* @return $this
*/
#[Option]
public function setOptions(array $options): self
{
$this->options = [];
Expand Down Expand Up @@ -74,6 +76,7 @@ public function getOption($value): RadioOption
*
* @return $this
*/
#[Option]
public function setDisabledOptions(array $disabledOptions): self
{
if (! empty($this->options)) {
Expand Down
4 changes: 3 additions & 1 deletion src/FormElement/SelectElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace ipl\Html\FormElement;

use InvalidArgumentException;
use ipl\Html\Attributes;
use ipl\Html\Common\MultipleAttribute;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
use ipl\Stdlib\Option;
use ipl\Validator\DeferredInArrayValidator;
use ipl\Validator\ValidatorChain;
use UnexpectedValueException;
Expand Down Expand Up @@ -48,6 +48,7 @@ public function getOption($value): ?SelectOption
*
* @return $this
*/
#[Option(name: ['options', 'multiOptions'])]
public function setOptions(array $options): self
{
$this->options = [];
Expand All @@ -66,6 +67,7 @@ public function setOptions(array $options): self
*
* @return $this
*/
#[Option]
public function setDisabledOptions(array $disabledOptions): self
{
if (! empty($this->options)) {
Expand Down
2 changes: 2 additions & 0 deletions src/FormElement/SubmitButtonElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ipl\Html\Attributes;
use ipl\Html\Contract\FormSubmitElement;
use ipl\Stdlib\Option;

class SubmitButtonElement extends ButtonElement implements FormSubmitElement
{
Expand All @@ -29,6 +30,7 @@ public function getSubmitValue(): string
*
* @return $this
*/
#[Option(name: 'value')]
public function setSubmitValue(string $value): self
{
$this->submitValue = $value;
Expand Down
2 changes: 2 additions & 0 deletions src/FormElement/TextElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ipl\Html\FormElement;

use ipl\Html\Attributes;
use ipl\Stdlib\Option;

class TextElement extends InputElement
{
Expand All @@ -28,6 +29,7 @@ public function getPlaceholder(): ?string
*
* @return $this
*/
#[Option]
public function setPlaceholder(?string $placeholder): self
{
$this->placeholder = $placeholder;
Expand Down
Loading