Skip to content

Commit c154fa7

Browse files
committed
update
1 parent 45c3bc5 commit c154fa7

File tree

6 files changed

+52
-41
lines changed

6 files changed

+52
-41
lines changed

docs/classes.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ A Form element has the following internal tree structure:
4242
- Label
4343
- Control (Input/Select/Checkbox/TextArea)
4444
- Error
45-
- Validator[]
45+
- Validator[]
46+
47+
##

docs/index.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
159159

160160
<body class="container">
161161
<h1 class="title">Login</h1>
162-
<form method="post">
163-
<?php $form->render(); ?>
164-
</form>
162+
<?php $form->render(); ?>
165163
</body>
166164

167165
</html>

docs/methods.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
# Methods
22

3-
The Form object has the following methods:
3+
## Form
4+
5+
The Form object has the following data methods:
46

57
- **fill**: Fill the form with an array of data (e.g. $_POST)
68
- **extract**: Extract the filled in form values
79
- **validate**: Validate the form and add errors where needed
810
- **addErrors**: Add custom errors (after validation)
911
- **render**: Output the form with or without root element
1012

13+
And the following structure building methods:
14+
15+
- **fieldsets**: Add multiple Fieldset elements
16+
- **fieldset**: Add a single Fieldset element
17+
- **fields**: Add multiple Field elements
18+
- **field**: Add a single Field element
19+
20+
## Fieldset
21+
1122

12-
##

example.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@
5050

5151
<body class="container">
5252
<h1 class="title">Login</h1>
53-
<form method="post">
54-
<?php $form->render(); ?>
55-
</form>
53+
<?php $form->render(); ?>
5654
</body>
5755

5856
</html>

src/Form.php

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ class Form
1414

1515
protected bool $hideFieldsets = false;
1616

17-
public function __construct() {}
17+
public function __construct()
18+
{
19+
$this->tag('form');
20+
}
1821

1922
public function action(string $action): self
2023
{
@@ -166,7 +169,7 @@ public function addErrors(array $messages): void
166169
public function renderDom(\DOMDocument $doc): \DOMElement
167170
{
168171
// Create a new DOMElement for the form
169-
$formElement = $doc->createElement('form');
172+
$formElement = $this->renderElement($doc);
170173
if (!empty($this->action)) {
171174
$formElement->setAttribute('action', $this->action);
172175
}
@@ -194,34 +197,4 @@ public function renderDom(\DOMDocument $doc): \DOMElement
194197
}
195198
return $formElement;
196199
}
197-
198-
public function toString(bool $withRoot = true, bool $asHtml = true): string
199-
{
200-
// save the DOMElement to a string
201-
$domDocument = new \DOMDocument('1.0', 'UTF-8');
202-
$form = $this->renderDom($domDocument);
203-
$domDocument->appendChild($form);
204-
if ($asHtml) {
205-
// save the HTML to a string
206-
$str = $domDocument->saveHTML($form);
207-
} else {
208-
// format the output as XML (for testing purposes)
209-
$domDocument->formatOutput = true;
210-
$str = $domDocument->saveXML($form);
211-
}
212-
if (!$str) {
213-
throw new \RuntimeException('Failed to save XML');
214-
}
215-
if (!$withRoot) {
216-
$str = str_replace("\n ", "\n", $str);
217-
$str = preg_replace('/^<form[^>]*>/', '', $str);
218-
$str = preg_replace('/<\/form>$/', '', $str);
219-
}
220-
return trim($str);
221-
}
222-
223-
public function render(bool $withRoot = false): void
224-
{
225-
echo $this->toString($withRoot);
226-
}
227200
}

src/HtmlElement.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,34 @@ protected function renderElement(\DOMDocument $doc): \DOMElement
105105
}
106106
return $element;
107107
}
108+
109+
public function toString(bool $withRoot = true, bool $asHtml = true): string
110+
{
111+
// save the DOMElement to a string
112+
$domDocument = new \DOMDocument('1.0', 'UTF-8');
113+
$form = $this->renderDom($domDocument);
114+
$domDocument->appendChild($form);
115+
if ($asHtml) {
116+
// save the HTML to a string
117+
$str = $domDocument->saveHTML($form);
118+
} else {
119+
// format the output as XML (for testing purposes)
120+
$domDocument->formatOutput = true;
121+
$str = $domDocument->saveXML($form);
122+
}
123+
if (!$str) {
124+
throw new \RuntimeException('Failed to save XML');
125+
}
126+
if (!$withRoot) {
127+
$str = str_replace("\n ", "\n", $str);
128+
$str = preg_replace('/^<' . $this->tag . '[^>]*>/', '', $str);
129+
$str = preg_replace('/<\/' . $this->tag . '>$/', '', $str);
130+
}
131+
return trim($str);
132+
}
133+
134+
public function render(bool $withRoot = true): void
135+
{
136+
echo $this->toString($withRoot);
137+
}
108138
}

0 commit comments

Comments
 (0)