Skip to content

Commit fcc7ebd

Browse files
committed
update
1 parent e8fcba3 commit fcc7ebd

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

src/SelectOrType.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,13 @@ public function renderDom(\DOMDocument $doc): \DOMElement
160160
$i = 0;
161161
foreach ($this->options as $value) {
162162
$caption = $value ?: '...';
163+
$isLast = $i == count($this->options) - 1;
163164
$option = $doc->createElement('option', $caption);
164-
$option->setAttribute('value', $value);
165-
if ($value == $this->value) {
166-
$option->setAttribute('selected', 'selected');
165+
if (!$isLast) {
166+
$option->setAttribute('value', $value);
167+
if ($value == $this->value) {
168+
$option->setAttribute('selected', 'selected');
169+
}
167170
}
168171
$select->appendChild($option);
169172
$i += 1;

tests/Forms/DifferentFormTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testRenderForm(): void
3737
' <option value="Magazine">Magazine</option>',
3838
' <option value="Newspaper">Newspaper</option>',
3939
' <hr/>',
40-
' <option value="!type!">Different ...</option>',
40+
' <option value="">Different ...</option>',
4141
' </select>',
4242
'</div>',
4343
];
@@ -58,7 +58,7 @@ public function testRenderBulma(): void
5858
' <option value="Magazine">Magazine</option>',
5959
' <option value="Newspaper">Newspaper</option>',
6060
' <hr/>',
61-
' <option value="!type!">Different ...</option>',
61+
' <option value="">Different ...</option>',
6262
' </select>',
6363
' </div>',
6464
'</div>',

tests/manual/room.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
// alias the most used classes
4+
use MintyPHP\Form\Elements as E;
5+
use MintyPHP\Form\Validator\Validators as V;
6+
7+
// ensure all classes are (auto)loaded:
8+
require_once __DIR__ . '/../../vendor/autoload.php';
9+
10+
// set style to Bulma
11+
E::$style = 'bulma';
12+
13+
// create a form object
14+
$form = E::form([
15+
E::field(E::selectOrType('room', ['Bathroom', 'Kitchen'], 'Type a room name ...')->required(), E::label('Select a room')),
16+
E::field(E::submit('Save')),
17+
]);
18+
19+
// check if the form has been submitted
20+
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
21+
// fill the form with the submitted data
22+
$form->fill($_POST);
23+
// if the form is valid, process the data
24+
if ($form->validate()) {
25+
// extract the data
26+
$data = $form->extract();
27+
// process the data (e.g., login, save to database, etc.)
28+
if (strtolower($data['room']) == 'living') {
29+
// redirect to the dashboard page
30+
die('OK');
31+
} else {
32+
// invalidate credentials
33+
$form->addErrors([
34+
'room' => 'Invalid room name, try "Living"',
35+
]);
36+
}
37+
}
38+
} else {
39+
// if the form is not submitted, fill it with empty values
40+
$form->fill([]);
41+
}
42+
?>
43+
<!DOCTYPE html>
44+
<html lang="en">
45+
46+
<head>
47+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/1.0.3/css/bulma.min.css">
48+
</head>
49+
50+
<body class="container p-5">
51+
<?php $form->render(); ?>
52+
</body>
53+
54+
</html>

0 commit comments

Comments
 (0)