@@ -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}
0 commit comments