@@ -42,7 +42,7 @@ class Form
4242 */
4343 public $ button = [
4444 "label " => "Submit " ,
45- "color " => " bg-blue-400 "
45+ "color " => null
4646 ];
4747
4848 /**
@@ -56,6 +56,8 @@ public function __construct(array $options = [])
5656 $ this ->method = $ options ['method ' ];
5757 $ this ->data = array_key_exists ("data " , $ options ) ? $ options ['data ' ] : [];
5858
59+ $ this ->button ['color ' ] = config ('form_generator.style ' ) == "tailwind " ? "bg-blue-400 " : "btn-primary " ;
60+
5961 return $ this ;
6062 }
6163
@@ -64,7 +66,7 @@ public function __construct(array $options = [])
6466 *
6567 * @return void
6668 */
67- public function handle ()
69+ protected function handle ()
6870 {
6971 //
7072 }
@@ -74,71 +76,51 @@ public function handle()
7476 *
7577 * @param string $name
7678 * @param string $label
77- * @param mixed $value
79+ * @param array $options
7880 * @return void
7981 */
80- public function text (string $ name , string $ label , mixed $ value = null )
82+ protected function text (string $ name , string $ label , array $ options =[] )
8183 {
82- $ this ->fields [] = [
83- "label " => $ label ,
84- "type " => "text " ,
85- "name " => $ name ,
86- "value " => $ value ?? (array_key_exists ($ name , $ this ->data ) ? $ this ->data [$ name ] : null )
87- ];
84+ $ this ->fields [] = $ this ->parseField ("text " , $ name , $ label , $ options );
8885 }
8986
9087 /**
9188 * Form Password Input
9289 *
9390 * @param string $name
9491 * @param string $label
95- * @param mixed $value
92+ * @param array $options
9693 * @return void
9794 */
98- public function password (string $ name , string $ label , mixed $ value = null )
95+ public function password (string $ name , string $ label , array $ options =[] )
9996 {
100- $ this ->fields [] = [
101- "label " => $ label ,
102- "type " => "password " ,
103- "name " => $ name ,
104- "value " => $ value ?? (array_key_exists ($ name , $ this ->data ) ? $ this ->data [$ name ] : null )
105- ];
97+ $ this ->fields [] = $ this ->parseField ("password " , $ name , $ label , $ options );
10698 }
10799
108100 /**
109101 * Form Date Input
110102 *
111103 * @param string $name
112104 * @param string $label
113- * @param mixed $value
105+ * @param array $options
114106 * @return void
115107 */
116- public function date (string $ name , string $ label , mixed $ value = null )
108+ public function date (string $ name , string $ label , array $ options =[] )
117109 {
118- $ this ->fields [] = [
119- "label " => $ label ,
120- "type " => "date " ,
121- "name " => $ name ,
122- "value " => $ value ?? (array_key_exists ($ name , $ this ->data ) ? $ this ->data [$ name ] : null )
123- ];
110+ $ this ->fields [] = $ this ->parseField ("date " , $ name , $ label , $ options );
124111 }
125112
126113 /**
127114 * Form Text Area Input
128115 *
129116 * @param string $name
130117 * @param string $label
131- * @param mixed $value
118+ * @param array $options
132119 * @return void
133120 */
134- public function textArea (string $ name , string $ label , mixed $ value = null )
121+ public function textArea (string $ name , string $ label , array $ options =[] )
135122 {
136- $ this ->fields [] = [
137- "label " => $ label ,
138- "type " => "textarea " ,
139- "name " => $ name ,
140- "value " => $ value ?? (array_key_exists ($ name , $ this ->data ) ? $ this ->data [$ name ] : null )
141- ];
123+ $ this ->fields [] = $ this ->parseField ("textarea " , $ name , $ label , $ options );
142124 }
143125
144126 /**
@@ -147,18 +129,14 @@ public function textArea(string $name, string $label, mixed $value=null)
147129 * @param string $name
148130 * @param string $label
149131 * @param array $choices
150- * @param mixed $value
132+ * @param array $options
151133 * @return void
152134 */
153- public function select (string $ name , string $ label , array $ choices , mixed $ value = null )
135+ public function select (string $ name , string $ label , array $ choices , array $ options =[] )
154136 {
155- $ this ->fields [] = [
156- "label " => $ label ,
157- "type " => "select " ,
158- "name " => $ name ,
159- "value " => $ value ?? (array_key_exists ($ name , $ this ->data ) ? $ this ->data [$ name ] : null ),
160- "choices " => $ choices
161- ];
137+ $ field = $ this ->parseField ("select " , $ name , $ label , $ options );
138+ $ field ['choices ' ] = $ choices ;
139+ $ this ->fields [] = $ field ;
162140 }
163141
164142 /**
@@ -167,20 +145,14 @@ public function select(string $name, string $label, array $choices, mixed $value
167145 * @param string $name
168146 * @param string $label
169147 * @param array $choices
170- * @param mixed $value
148+ * @param array $options
171149 * @return void
172150 */
173- public function radio (string $ name , string $ label , array $ choices , mixed $ value = null )
151+ public function radio (string $ name , string $ label , array $ choices , array $ options =[] )
174152 {
175- $ this ->fields [] = [
176- "label " => $ label ,
177- "type " => "radio " ,
178- "name " => $ name ,
179- "value " => $ value ?? (array_key_exists ($ name , $ this ->data ) ? $ this ->data [$ name ] : null ),
180- "choices " => $ choices
181- ];
182-
183- return $ this ;
153+ $ field = $ this ->parseField ("radio " , $ name , $ label , $ options );
154+ $ field ['choices ' ] = $ choices ;
155+ $ this ->fields [] = $ field ;
184156 }
185157
186158 /**
@@ -189,20 +161,14 @@ public function radio(string $name, string $label, array $choices, mixed $value=
189161 * @param string $name
190162 * @param string $label
191163 * @param array $choices
192- * @param array $value
164+ * @param array $option
193165 * @return void
194166 */
195- public function checkBox (string $ name , string $ label , array $ choices , array $ value =[])
167+ public function checkBox (string $ name , string $ label , array $ choices , array $ options =[])
196168 {
197- $ this ->fields [] = [
198- "label " => $ label ,
199- "type " => "checkbox " ,
200- "name " => $ name ,
201- "value " => $ value ?? (array_key_exists ($ name , $ this ->data ) ? $ this ->data [$ name ] : []),
202- "choices " => $ choices
203- ];
204-
205- return $ this ;
169+ $ field = $ this ->parseField ("checkbox " , $ name , $ label , $ options , []);
170+ $ field ['choices ' ] = $ choices ;
171+ $ this ->fields [] = $ field ;
206172 }
207173
208174 /**
@@ -231,4 +197,32 @@ public function isNeedCsrf()
231197
232198 return true ;
233199 }
200+
201+ /**
202+ * Parse Field Array
203+ *
204+ * @param string $type
205+ * @param string $name
206+ * @param string $label
207+ * @param array $options
208+ * @param mixed $default_value
209+ * @return void
210+ */
211+ private function parseField (string $ type , string $ name , string $ label , array $ options , $ default_value =null )
212+ {
213+ $ field = [
214+ "label " => $ label ,
215+ "type " => $ type ,
216+ "name " => $ name ,
217+ "required " => array_key_exists ("required " , $ options ) ? $ options ['required ' ] : false ,
218+ ];
219+
220+ if (array_key_exists ("value " , $ options )) {
221+ $ field ['value ' ] = $ options ['value ' ];
222+ } else {
223+ $ field ['value ' ] = (array_key_exists ($ name , $ this ->data ) ? $ this ->data [$ name ] : $ default_value );
224+ }
225+
226+ return $ field ;
227+ }
234228}
0 commit comments