Skip to content

Commit b0833d4

Browse files
committed
update
1 parent cc5dc49 commit b0833d4

File tree

4 files changed

+36
-32
lines changed

4 files changed

+36
-32
lines changed

src/Checkbox.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public function renderDom(\DOMDocument $doc): \DOMElement
7373
if ($this->checked) {
7474
$input->setAttribute('checked', 'checked');
7575
}
76+
if ($this->required) {
77+
$input->setAttribute('required', 'required');
78+
}
7679
return $input;
7780
}
7881
}

src/Select.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public function autocomplete(string $value): self
8282
public function options(array $options): self
8383
{
8484
$this->options = $options;
85+
if (!isset($this->options[''])) {
86+
$this->options = ['' => '...'] + $this->options; // Add empty option if not present
87+
}
8588
return $this;
8689
}
8790

@@ -150,10 +153,9 @@ public function renderDom(\DOMDocument $doc): \DOMElement
150153
if ($this->multiple) {
151154
$select->setAttribute('multiple', 'multiple');
152155
}
153-
$option = $doc->createElement('option');
154-
$option->setAttribute('value', '');
155-
$option->textContent = $this->placeholder;
156-
$select->appendChild($option);
156+
if ($this->required) {
157+
$select->setAttribute('required', 'required');
158+
}
157159
foreach ($this->options as $key => $value) {
158160
$option = $doc->createElement('option');
159161
$option->setAttribute('value', strval($key));

tests/Forms/BooleanFormTest.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ class BooleanFormTest extends TestCase
1313
private function createForm(string $style): Form
1414
{
1515
$booleans = [
16-
'' => 'Select an option',
1716
'yes' => 'Yes',
1817
'no' => 'No',
1918
];
2019
E::$style = $style;
2120
return E::form([
22-
E::field(E::select('bool', $booleans), E::label('Yes or No?'), [V::required('Field cannot be empty')]),
21+
E::field(E::select('bool', $booleans)->required(), E::label('Yes or No?'), [V::required('Field cannot be empty')]),
2322
]);
2423
}
2524

@@ -29,8 +28,8 @@ public function testRenderForm(): void
2928
$lines = [
3029
'<div>',
3130
' <label for="bool">Yes or No?</label>',
32-
' <select id="bool" name="bool">',
33-
' <option value="">Select an option</option>',
31+
' <select id="bool" name="bool" required="required">',
32+
' <option value="">...</option>',
3433
' <option value="yes">Yes</option>',
3534
' <option value="no">No</option>',
3635
' </select>',
@@ -46,8 +45,8 @@ public function testRenderBulma(): void
4645
'<div class="field">',
4746
' <label class="label" for="bool">Yes or No?</label>',
4847
' <div class="select">',
49-
' <select id="bool" name="bool">',
50-
' <option value="">Select an option</option>',
48+
' <select id="bool" name="bool" required="required">',
49+
' <option value="">...</option>',
5150
' <option value="yes">Yes</option>',
5251
' <option value="no">No</option>',
5352
' </select>',
@@ -64,8 +63,8 @@ public function testFillForm(): void
6463
$lines = [
6564
'<div>',
6665
' <label for="bool">Yes or No?</label>',
67-
' <select id="bool" name="bool">',
68-
' <option value="">Select an option</option>',
66+
' <select id="bool" name="bool" required="required">',
67+
' <option value="">...</option>',
6968
' <option value="yes" selected="selected">Yes</option>',
7069
' <option value="no">No</option>',
7170
' </select>',
@@ -82,8 +81,8 @@ public function testValidators(): void
8281
$lines = [
8382
'<div class="error">',
8483
' <label for="bool">Yes or No?</label>',
85-
' <select id="bool" name="bool">',
86-
' <option value="" selected="selected">Select an option</option>',
84+
' <select id="bool" name="bool" required="required">',
85+
' <option value="" selected="selected">...</option>',
8786
' <option value="yes">Yes</option>',
8887
' <option value="no">No</option>',
8988
' </select>',
@@ -96,8 +95,8 @@ public function testValidators(): void
9695
$lines = [
9796
'<div class="error">',
9897
' <label for="bool">Yes or No?</label>',
99-
' <select id="bool" name="bool">',
100-
' <option value="">Select an option</option>',
98+
' <select id="bool" name="bool" required="required">',
99+
' <option value="">...</option>',
101100
' <option value="yes">Yes</option>',
102101
' <option value="no">No</option>',
103102
' </select>',
@@ -110,8 +109,8 @@ public function testValidators(): void
110109
$lines = [
111110
'<div>',
112111
' <label for="bool">Yes or No?</label>',
113-
' <select id="bool" name="bool">',
114-
' <option value="">Select an option</option>',
112+
' <select id="bool" name="bool" required="required">',
113+
' <option value="">...</option>',
115114
' <option value="yes">Yes</option>',
116115
' <option value="no" selected="selected">No</option>',
117116
' </select>',
@@ -129,8 +128,8 @@ public function testValidatorsBulma(): void
129128
'<div class="field">',
130129
' <label class="label" for="bool">Yes or No?</label>',
131130
' <div class="select is-danger">',
132-
' <select id="bool" name="bool">',
133-
' <option value="" selected="selected">Select an option</option>',
131+
' <select id="bool" name="bool" required="required">',
132+
' <option value="" selected="selected">...</option>',
134133
' <option value="yes">Yes</option>',
135134
' <option value="no">No</option>',
136135
' </select>',
@@ -145,8 +144,8 @@ public function testValidatorsBulma(): void
145144
'<div class="field">',
146145
' <label class="label" for="bool">Yes or No?</label>',
147146
' <div class="select is-danger">',
148-
' <select id="bool" name="bool">',
149-
' <option value="">Select an option</option>',
147+
' <select id="bool" name="bool" required="required">',
148+
' <option value="">...</option>',
150149
' <option value="yes">Yes</option>',
151150
' <option value="no">No</option>',
152151
' </select>',
@@ -161,8 +160,8 @@ public function testValidatorsBulma(): void
161160
'<div class="field">',
162161
' <label class="label" for="bool">Yes or No?</label>',
163162
' <div class="select">',
164-
' <select id="bool" name="bool">',
165-
' <option value="">Select an option</option>',
163+
' <select id="bool" name="bool" required="required">',
164+
' <option value="">...</option>',
166165
' <option value="yes">Yes</option>',
167166
' <option value="no" selected="selected">No</option>',
168167
' </select>',

tests/Forms/ConfirmFormTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testRenderForm(): void
2424
$lines = [
2525
'<div>',
2626
' <label for="confirm">I agree to the terms and conditions</label>',
27-
' <input id="confirm" type="checkbox" name="confirm" value="on"/>',
27+
' <input id="confirm" type="checkbox" name="confirm" value="on" required="required"/>',
2828
'</div>',
2929
];
3030
$this->assertEquals(implode("\n", $lines), $form->toString(false, false));
@@ -35,7 +35,7 @@ public function testRenderBulma(): void
3535
$form = $this->createForm('bulma');
3636
$lines = [
3737
'<div class="field">',
38-
' <label class="checkbox"><input type="checkbox" name="confirm" value="on"/>I agree to the terms and conditions</label>',
38+
' <label class="checkbox"><input type="checkbox" name="confirm" value="on" required="required"/>I agree to the terms and conditions</label>',
3939
'</div>',
4040
];
4141
$this->assertEquals(implode("\n", $lines), $form->toString(false, false));
@@ -48,15 +48,15 @@ public function testFillForm(): void
4848
$lines = [
4949
'<div>',
5050
' <label for="confirm">I agree to the terms and conditions</label>',
51-
' <input id="confirm" type="checkbox" name="confirm" value="on" checked="checked"/>',
51+
' <input id="confirm" type="checkbox" name="confirm" value="on" checked="checked" required="required"/>',
5252
'</div>',
5353
];
5454
$this->assertEquals(implode("\n", $lines), $form->toString(false, false));
5555
$form->fill(['confirm' => '']);
5656
$lines = [
5757
'<div>',
5858
' <label for="confirm">I agree to the terms and conditions</label>',
59-
' <input id="confirm" type="checkbox" name="confirm" value="on"/>',
59+
' <input id="confirm" type="checkbox" name="confirm" value="on" required="required"/>',
6060
'</div>',
6161
];
6262
$this->assertEquals(implode("\n", $lines), $form->toString(false, false));
@@ -74,7 +74,7 @@ public function testValidators(): void
7474
$lines = [
7575
'<div class="error">',
7676
' <label for="confirm">I agree to the terms and conditions</label>',
77-
' <input id="confirm" type="checkbox" name="confirm" value="on"/>',
77+
' <input id="confirm" type="checkbox" name="confirm" value="on" required="required"/>',
7878
' <div>Field must be checked</div>',
7979
'</div>',
8080
];
@@ -87,7 +87,7 @@ public function testValidators(): void
8787
$lines = [
8888
'<div>',
8989
' <label for="confirm">I agree to the terms and conditions</label>',
90-
' <input id="confirm" type="checkbox" name="confirm" value="on" checked="checked"/>',
90+
' <input id="confirm" type="checkbox" name="confirm" value="on" checked="checked" required="required"/>',
9191
'</div>',
9292
];
9393
$this->assertEquals(implode("\n", $lines), $form->toString(false, false));
@@ -100,7 +100,7 @@ public function testValidatorsBulma(): void
100100
$this->assertFalse($form->validate());
101101
$lines = [
102102
'<div class="field">',
103-
' <label class="checkbox"><input type="checkbox" name="confirm" value="on"/>I agree to the terms and conditions</label>',
103+
' <label class="checkbox"><input type="checkbox" name="confirm" value="on" required="required"/>I agree to the terms and conditions</label>',
104104
' <p class="help is-danger">Field must be checked</p>',
105105
'</div>',
106106
];
@@ -112,7 +112,7 @@ public function testValidatorsBulma(): void
112112
$this->assertTrue($form->validate());
113113
$lines = [
114114
'<div class="field">',
115-
' <label class="checkbox"><input type="checkbox" name="confirm" value="on" checked="checked"/>I agree to the terms and conditions</label>',
115+
' <label class="checkbox"><input type="checkbox" name="confirm" value="on" checked="checked" required="required"/>I agree to the terms and conditions</label>',
116116
'</div>',
117117
];
118118
$this->assertEquals(implode("\n", $lines), $form->toString(false, false));

0 commit comments

Comments
 (0)