@@ -24,12 +24,13 @@ class Form
2424 protected $ action ;
2525 protected $ theme ;
2626 protected $ class ;
27- protected $ elements = [];
28- protected $ errors = [];
27+ protected $ elements = [];
28+ protected $ errors = [];
2929 protected $ validated = false ;
30- protected $ name = false ;
31- protected $ isAjax = false ;
30+ protected $ name = false ;
31+ protected $ isAjax = false ;
3232 protected $ enctype ;
33+ protected $ disabled = false ;
3334
3435 public function __construct ()
3536 {
@@ -44,47 +45,81 @@ public static function form()
4445 public function method ($ method = null )
4546 {
4647 $ this ->method = $ method ;
48+
4749 return $ this ;
4850 }
4951
5052 public function isAjax ($ isAjax )
5153 {
5254 $ this ->isAjax = $ isAjax ;
55+
5356 return $ this ;
5457 }
5558
5659 public function route ($ routeName , $ parameters = [])
5760 {
5861 $ this ->action = route ($ routeName , $ parameters );
62+
5963 return $ this ;
6064 }
6165
62- public function action ($ action ) {
66+ public function action ($ action )
67+ {
6368 $ this ->action = $ action ;
69+
6470 return $ this ;
6571 }
6672
6773 public function view ()
6874 {
6975 return view ('form:: ' . $ this ->theme . '.form ' , [
7076 'elements ' => $ this ->elements ,
71- 'method ' => $ this ->method ,
72- 'action ' => $ this ->action ,
73- 'name ' => $ this ->name ,
74- 'class ' => $ this ->class ,
75- 'isAjax ' => $ this ->isAjax ,
76- 'enctype ' => $ this ->enctype ,
77+ 'method ' => $ this ->method ,
78+ 'action ' => $ this ->action ,
79+ 'name ' => $ this ->name ,
80+ 'class ' => $ this ->class ,
81+ 'isAjax ' => $ this ->isAjax ,
82+ 'enctype ' => $ this ->enctype ,
7783 ]);
7884 }
7985
86+ public function disable ()
87+ {
88+ $ this ->disabled = true ;
89+
90+ return $ this ;
91+ }
92+
93+ public function isDisabled ()
94+ {
95+ return $ this ->disabled ;
96+ }
97+
8098 public function render ()
8199 {
82100 $ this ->setupFormName ();
83- $ this ->addElementHidden ($ this ->name )->setValue ($ this ->name );
84- return new HtmlString ($ this ->view ()->render ());
101+ $ this ->disableIfNeeded ();
102+ $ this ->addElementHidden ($ this ->name )
103+ ->setValue ($ this ->name );
104+
105+ return new HtmlString ($ this ->view ()
106+ ->render ());
85107 }
86108
87- public function isSubmitted () {
109+ public function disableIfNeeded ()
110+ {
111+ if ($ this ->disabled )
112+ {
113+ foreach ($ this ->elements as $ element )
114+ {
115+ $ element ->setDisabled (true )
116+ ->setIgnored (true );
117+ }
118+ }
119+ }
120+
121+ public function isSubmitted ()
122+ {
88123 $ this ->setupFormName ();
89124
90125 return request ()->has ($ this ->name ) || old ($ this ->name );
@@ -93,30 +128,35 @@ public function isSubmitted() {
93128 public function setTranslationMask ($ translationMask = '' )
94129 {
95130 $ this ->translationMask = $ translationMask ;
131+
96132 return $ this ;
97133 }
98134
99135 public function setPlaceholderTranslationMask ($ placeholderTranslationMask = '' )
100136 {
101137 $ this ->placeholderTranslationMask = $ placeholderTranslationMask ;
138+
102139 return $ this ;
103140 }
104141
105142 public function setName ($ name = '' )
106143 {
107144 $ this ->name = $ name ;
145+
108146 return $ this ;
109147 }
110148
111149 public function setClass ($ class = '' )
112150 {
113151 $ this ->class = $ class ;
152+
114153 return $ this ;
115154 }
116155
117156 public function addClass ($ class )
118157 {
119158 $ this ->class .= ' ' . $ class ;
159+
120160 return $ this ;
121161 }
122162
@@ -131,6 +171,7 @@ public function addElementText($name, $validators = '')
131171 $ element ->setTheme ($ this ->theme );
132172
133173 $ this ->elements [$ name ] = $ element ;
174+
134175 return $ this ->elements [$ name ];
135176 }
136177
@@ -145,6 +186,7 @@ public function addElementDateTime($name, $validators = '')
145186 $ element ->setTheme ($ this ->theme );
146187
147188 $ this ->elements [$ name ] = $ element ;
189+
148190 return $ this ->elements [$ name ];
149191 }
150192
@@ -159,6 +201,7 @@ public function addElementDate($name, $validators = '')
159201 $ element ->setTheme ($ this ->theme );
160202
161203 $ this ->elements [$ name ] = $ element ;
204+
162205 return $ this ->elements [$ name ];
163206 }
164207
@@ -173,6 +216,7 @@ public function addElementRange($name, $validators = '')
173216 $ element ->setTheme ($ this ->theme );
174217
175218 $ this ->elements [$ name ] = $ element ;
219+
176220 return $ this ->elements [$ name ];
177221 }
178222
@@ -187,8 +231,8 @@ public function addElementFile($name, $validators = '')
187231 $ element ->setTheme ($ this ->theme );
188232
189233 $ this ->elements [$ name ] = $ element ;
190- $ this ->enctype = 'multipart/form-data ' ;
191-
234+ $ this ->enctype = 'multipart/form-data ' ;
235+
192236 return $ this ->elements [$ name ];
193237 }
194238
@@ -202,15 +246,17 @@ public function addElementDelimiter($name, $view = '', $data = [])
202246 {
203247 $ element = new Element \Delimiter ($ name , '' );
204248 $ element ->setTheme ($ this ->theme )
205- ->setContent ($ view , $ data );
249+ ->setContent ($ view , $ data );
206250
207251 $ this ->elements [$ name ] = $ element ;
252+
208253 return $ this ->elements [$ name ];
209254 }
210255
211256 public function addElement (Element $ element )
212257 {
213258 $ this ->elements [$ element ->getName ()] = $ element ;
259+
214260 return $ this ->elements [$ element ->getName ()];
215261 }
216262
@@ -227,6 +273,7 @@ public function addElementHtml($name, $content = '')
227273 $ element ->setTheme ($ this ->theme );
228274
229275 $ this ->elements [$ name ] = $ element ;
276+
230277 return $ this ->elements [$ name ];
231278 }
232279
@@ -258,6 +305,7 @@ public function addElementTextarea($name, $validators = '')
258305 $ element ->setTheme ($ this ->theme );
259306
260307 $ this ->elements [$ name ] = $ element ;
308+
261309 return $ this ->elements [$ name ];
262310 }
263311
@@ -272,6 +320,7 @@ public function addElementHidden($name, $validators = '')
272320 $ element ->setTheme ($ this ->theme );
273321
274322 $ this ->elements [$ name ] = $ element ;
323+
275324 return $ this ->elements [$ name ];
276325 }
277326
@@ -286,6 +335,7 @@ public function addElementCheckbox($name, $validators = '')
286335 $ element ->setTheme ($ this ->theme );
287336
288337 $ this ->elements [$ name ] = $ element ;
338+
289339 return $ this ->elements [$ name ];
290340 }
291341
@@ -300,6 +350,7 @@ public function addElementCheckboxGroup($name, $validators = '')
300350 $ element ->setTheme ($ this ->theme );
301351
302352 $ this ->elements [$ name ] = $ element ;
353+
303354 return $ this ->elements [$ name ];
304355 }
305356
@@ -314,6 +365,7 @@ public function addElementPassword($name, $validators = '')
314365 $ element ->setTheme ($ this ->theme );
315366
316367 $ this ->elements [$ name ] = $ element ;
368+
317369 return $ this ->elements [$ name ];
318370 }
319371
@@ -357,6 +409,7 @@ public function addElementSubmit($name = 'submit')
357409 $ element ->setTheme ($ this ->theme );
358410
359411 $ this ->elements [$ name ] = $ element ;
412+
360413 return $ this ->elements [$ name ];
361414 }
362415
@@ -368,26 +421,34 @@ public function getElement($name)
368421
369422 public function getElements ($ names = null )
370423 {
371- if ($ names === null ) {
424+ if ($ names === null )
425+ {
372426 return $ this ->elements ;
373427 }
374- if (!is_array ($ names )) {
428+ if (!is_array ($ names ))
429+ {
375430 $ names = func_get_args ();
376431 }
432+
377433 return array_only ($ this ->elements , $ names );
378434 }
379435
380436 public function validate ($ force = false )
381437 {
382- if (!$ this ->isSubmitted () && !$ force ) {
438+ if (!$ this ->isSubmitted () && !$ force )
439+ {
383440 return false ;
384441 }
385442
386- if (!$ this ->validated ) {
443+ if (!$ this ->validated )
444+ {
387445 $ keys = array_keys ($ this ->elements );
388- foreach ($ this ->elements as $ name => $ element ) {
389- if (!$ element ->isIgnored ()) {
390- if (!$ element ->validate ($ keys )) {
446+ foreach ($ this ->elements as $ name => $ element )
447+ {
448+ if (!$ element ->isIgnored ())
449+ {
450+ if (!$ element ->validate ($ keys ))
451+ {
391452 array_set ($ this ->errors , $ name , $ element ->error ());
392453 }
393454 }
@@ -400,19 +461,24 @@ public function validate($force = false)
400461
401462 public function errors ()
402463 {
403- if (!$ this ->validated ) {
464+ if (!$ this ->validated )
465+ {
404466 $ this ->validate ();
405467 }
468+
406469 return $ this ->errors ;
407470 }
408471
409472 public function values ($ skipEmptyString = false )
410473 {
411474 $ data = [];
412- foreach ($ this ->elements as $ name => $ element ) {
413- if (!$ element ->isIgnored ()) {
475+ foreach ($ this ->elements as $ name => $ element )
476+ {
477+ if (!$ element ->isIgnored ())
478+ {
414479 $ value = $ element ->value ();
415- if (!$ skipEmptyString || $ skipEmptyString && $ value !== '' ) {
480+ if (!$ skipEmptyString || $ skipEmptyString && $ value !== '' )
481+ {
416482 array_set ($ data , $ name , $ value );
417483 }
418484 }
@@ -428,10 +494,12 @@ public function value($name, $default = null)
428494
429495 public function setValues ($ values = [])
430496 {
431- foreach ($ this ->elements as $ name => $ element ) {
497+ foreach ($ this ->elements as $ name => $ element )
498+ {
432499 $ value = array_get ($ values , $ name );
433500
434- if ($ value !== null ) {
501+ if ($ value !== null )
502+ {
435503 $ element ->setValue ($ value );
436504 }
437505 }
@@ -442,22 +510,32 @@ public function setValues($values = [])
442510 public function setTheme ($ theme = '' )
443511 {
444512 $ this ->theme = $ theme ;
513+
445514 return $ this ;
446515 }
447516
448517 public function redirectToRoute ($ route , $ attributes = [])
449518 {
450- return redirect ()->route ($ route , $ attributes )->withErrors ($ this ->errors )->withInput ();
519+ return redirect ()
520+ ->route ($ route , $ attributes )
521+ ->withErrors ($ this ->errors )
522+ ->withInput ();
451523 }
452524
453- public function redirectBack ($ additionalErrors = []) {
525+ public function redirectBack ($ additionalErrors = [])
526+ {
454527 $ errors = array_merge ($ additionalErrors , $ this ->errors ());
455528
456- return redirect ()->back ()->withErrors ($ errors )->withInput ();
529+ return redirect ()
530+ ->back ()
531+ ->withErrors ($ errors )
532+ ->withInput ();
457533 }
458534
459- private function setupFormName () {
460- if (!$ this ->name ) {
535+ private function setupFormName ()
536+ {
537+ if (!$ this ->name )
538+ {
461539 $ this ->name = md5 (get_class ($ this ));
462540 }
463541 }
0 commit comments