1010namespace Nette \Bridges \FormsLatte ;
1111
1212use Nette ;
13+ use Nette \Forms \Container ;
14+ use Nette \Forms \Control ;
1315use Nette \Forms \Form ;
1416use Nette \Utils \Html ;
1517use function end , explode , is_object , parse_url , preg_replace , preg_split , urldecode ;
2224 */
2325class Runtime
2426{
25- use Nette \StaticClass;
26-
27- public static function initializeForm (Form $ form ): void
28- {
29- $ form ->fireRenderEvents ();
30- foreach ($ form ->getControls () as $ control ) {
31- $ control ->setOption ('rendered ' , false );
32- }
33- }
27+ /** @var Container[] */
28+ private array $ stack = [];
3429
3530
3631 /**
3732 * Renders form begin.
3833 */
39- public static function renderFormBegin (Form $ form , array $ attrs , bool $ withTags = true ): string
34+ public function renderFormBegin (array $ attrs , bool $ withTags = true ): string
4035 {
36+ $ form = $ this ->current ();
4137 $ el = $ form ->getElementPrototype ();
4238 $ el ->action = (string ) $ el ->action ;
4339 $ el = clone $ el ;
@@ -53,8 +49,9 @@ public static function renderFormBegin(Form $form, array $attrs, bool $withTags
5349 /**
5450 * Renders form end.
5551 */
56- public static function renderFormEnd (Form $ form , bool $ withTags = true ): string
52+ public function renderFormEnd (bool $ withTags = true ): string
5753 {
54+ $ form = $ this ->current ();
5855 $ s = '' ;
5956 if ($ form ->isMethod ('get ' )) {
6057 foreach (preg_split ('#[;&]# ' , (string ) parse_url ($ form ->getElementPrototype ()->action , PHP_URL_QUERY ), -1 , PREG_SPLIT_NO_EMPTY ) as $ param ) {
@@ -77,12 +74,37 @@ public static function renderFormEnd(Form $form, bool $withTags = true): string
7774 }
7875
7976
80- public static function item ($ item , $ global ): object
77+ public function item (mixed $ item , string $ type = Control::class): Control |Container
78+ {
79+ $ item = is_object ($ item ) ? $ item : $ this ->current ()[$ item ];
80+ if (!$ item instanceof $ type ) {
81+ throw new Nette \InvalidArgumentException ("Expected instance of $ type, " . get_debug_type ($ item ) . ' given. ' );
82+ }
83+ return $ item ;
84+ }
85+
86+
87+ public function begin (Container $ form ): void
8188 {
82- if (is_object ($ item )) {
83- return $ item ;
89+ $ this ->stack [] = $ form ;
90+
91+ if ($ form instanceof Form) {
92+ $ form ->fireRenderEvents ();
93+ foreach ($ form ->getControls () as $ control ) {
94+ $ control ->setOption ('rendered ' , false );
95+ }
8496 }
85- $ form = end ($ global ->formsStack ) ?: throw new \LogicException ('Form declaration is missing, did you use {form} or <form n:name> tag? ' );
86- return $ form [$ item ];
97+ }
98+
99+
100+ public function end (): void
101+ {
102+ array_pop ($ this ->stack );
103+ }
104+
105+
106+ public function current (): Container
107+ {
108+ return end ($ this ->stack ) ?: throw new Nette \InvalidStateException ('Form declaration is missing, did you use {form} or <form n:name> tag? ' );
87109 }
88110}
0 commit comments