Skip to content

Commit 151b41f

Browse files
committed
update
1 parent 9ccc7e5 commit 151b41f

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

src/Checkboxes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Checkboxes implements Control
1414
protected array $options = [];
1515
/** @var string[] $values */
1616
protected array $values = [];
17+
protected bool $required = false;
1718

1819
public function __construct()
1920
{
@@ -31,6 +32,12 @@ public function getName(): string
3132
return $this->name;
3233
}
3334

35+
public function required(): self
36+
{
37+
$this->required = true;
38+
return $this;
39+
}
40+
3441
/**
3542
* @param array<string,string> $options
3643
*/
@@ -71,6 +78,9 @@ public function extract(): array
7178

7279
public function validate(Validator $validator): string
7380
{
81+
if (!$this->required && !$this->values) {
82+
return '';
83+
}
7484
if (array_diff($this->values, array_keys($this->options))) {
7585
return 'Invalid checkbox value';
7686
}

src/Control.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function getId(): string;
1111
public function name(string $name): self;
1212
public function getName(): string;
1313
public function value(string $value): self;
14+
public function required(): self;
1415
/**
1516
* @param array<string, string|string[]|null> $data
1617
*/

src/Field.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ class Field
2525
public function __construct()
2626
{
2727
$this->tag('div');
28+
$this->class('form-group');
2829
}
2930

3031
public function disabled(): self
3132
{
33+
$this->class('disabled');
3234
$this->disabled = true;
3335
return $this;
3436
}
@@ -101,7 +103,7 @@ public function fill(array $data): void
101103
public function extract(): array
102104
{
103105
$data = [];
104-
if ($this->control) {
106+
if ($this->control && !$this->disabled) {
105107
$data = $this->control->extract();
106108
}
107109
return $data;

src/Input.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Input implements Control
1313
protected string $value = '';
1414
protected string $type = 'text';
1515
protected string $placeholder = '';
16+
protected bool $required = false;
1617

1718
public function __construct()
1819
{
@@ -36,6 +37,12 @@ public function getName(): string
3637
return $this->name;
3738
}
3839

40+
public function required(): self
41+
{
42+
$this->required = true;
43+
return $this;
44+
}
45+
3946
public function value(string $value): self
4047
{
4148
$this->value = $value;
@@ -77,6 +84,9 @@ public function extract(): array
7784

7885
public function validate(Validator $validator): string
7986
{
87+
if (!$this->required && !$this->value) {
88+
return '';
89+
}
8090
return $validator->validate($this->value);
8191
}
8292

src/Select.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Select implements Control
1414
protected array $options = [];
1515
/** @var string[] $values */
1616
protected array $values = [];
17+
protected bool $required = false;
1718
protected bool $multiple = false;
1819

1920
public function __construct()
@@ -38,6 +39,12 @@ public function getName(): string
3839
return $this->name;
3940
}
4041

42+
public function required(): self
43+
{
44+
$this->required = true;
45+
return $this;
46+
}
47+
4148
/**
4249
* @param array<string|int,string> $options
4350
*/
@@ -81,6 +88,9 @@ public function extract(): array
8188

8289
public function validate(Validator $validator): string
8390
{
91+
if (!$this->required && !$this->values) {
92+
return '';
93+
}
8494
if (array_diff($this->values, array_keys($this->options))) {
8595
return 'Invalid option value';
8696
}

0 commit comments

Comments
 (0)