11# micro-form
22
3- [ ![ Latest Stable Version] ( https://poser.pugx.org/fullstackpe/micro-form/v/stable )] ( https://packagist.org/packages/fullstackpe/micro-form ) [ ![ Build Status] ( https://travis-ci.org/marcomilon/micro-form.svg?branch=master )] ( https://travis-ci.org/marcomilon/micro-form )
4-
5- Micro-form is a library to translate Json objects into Html forms. Boostrap forms are used as the default template but can easily add new templates.
3+ Micro-form is a library to translate any datasource into into html form elements. Only Json Objects can be use as datasources.
64
75### Installation
86
@@ -15,271 +13,122 @@ If you prefer you can create a composer.json in your project folder.
1513``` json
1614{
1715 "require" : {
18- "fullstackpe/micro-form" : " ^2 .0"
16+ "fullstackpe/micro-form" : " ^3 .0"
1917 }
2018}
2119```
2220
23- #### How it works?
21+ ### How it works?
2422
25- Micro-form reads a json file to render an Html form. The json file is just an object with three properties: name, inputs, repeat .
23+ Create a ` jsform ` object.
2624
27- * _ name_ is the name of the array of inputs.
28- * _ inputs_ is an array objects that represents the inputs.
29- * _ repeat_ is a boolean value to set the array of inputs repeatable.
25+ ```
26+ use micro\FormFactory;
27+ $jsonForm = FormFactory::jsonForm();
28+ echo $jsonForm->render($json);
29+ ```
3030
31- The json used to render the form has to validated against this [ Json-schema ] ( https://github.com/marcomilon/micro-form/blob/master/src/form/schemas/input.json ) .
31+ then call render to get the form elements. The render method accepts a Json Object .
3232
3333** Examples**
3434
35- Create an instance of the class micro\form\Builder
35+ ** Input **
3636
3737``` php
3838<?php
3939
40- $json = '{
41- "inputs": [
42- {
43- "name": "name"
44- },
45- {
46- "name": "lastname"
47- }
48- ]
49- }';
50-
51- $builder = new \micro\form\Builder();
52- $form = $builder->render($json);
53- echo $form;
54- ```
40+ $json = '[
41+ {
42+ "tag": "input",
43+ "type": "text",
44+ "name": "username",
45+ "class": "form-control"
46+ }
47+ ]';
5548
56- The php code will produce the following html form inputs.
49+ use micro\FormFactory;
50+ $jsonForm = FormFactory::jsonForm();
51+ echo $jsonForm->render($json);
52+ ```
5753
54+ * output*
5855
5956``` html
60- <div class =" form-group" >
61- <label >Name</label >
62- <input type =" text" class =" form-control" name =" name" value =" marco" >
63- </div >
64- <div class =" form-group" >
65- <label >Lastname</label >
66- <input type =" text" class =" form-control" name =" lastname" value =" milon" >
67- </div >
57+ <input type =" text" name =" username" class =" form-control" >
6858```
6959
70- It is possible to use more complex Json objects for example
60+ ** Textarea **
7161
7262``` php
7363<?php
7464
75- $json = '{
76- "inputs": [
77- {
78- "type": "text",
79- "name": "username",
80- "id": "username",
81- "placeholder": "Username",
82- "label": "Enter username"
83- }
84- ]
85- }';
86-
87- $builder = new \micro\form\Builder();
88- $form = $builder->render($json);
89- echo $form;
90- ```
91-
92- The example will output the following html inputs.
93-
94- ``` html
95- <div class =" form-group" >
96- <label for =" username" >Enter username</label >
97- <input type =" text" class =" form-control" name =" username" placeholder =" Username" id =" username" >
98- </div >
99- ```
100-
101- Repeaters are supported out of the box for example
102-
103- ``` php
104- <?php
65+ $json = '[
66+ {
67+ "tag": "textarea",
68+ "id": "story",
69+ "name": "story",
70+ "rows": "5",
71+ "cols": "33",
72+ "value": "It was a dark and stormy night..."
73+ }
74+ ]';
10575
106- $json = '{
107- "inputs": [
108- {
109- "type": "text",
110- "name": "username",
111- "repeat": true
112- }
113- ]
114- }';
115-
116- $builder = new \micro\form\Builder();
117- $form = $builder->render($json);
118- echo $form;
76+ use micro\FormFactory;
77+ $jsonForm = FormFactory::jsonForm();
78+ echo $jsonForm->render($json);
11979```
12080
121- html output
81+ * output*
12282
12383``` html
124- <div class =" toolbar--add" >
125- <a href =" #" class =" toolbar--add__add" >Add Item</a >
126- </div >
127- <div class =" repeater" >
128- <div class =" element" >
129- <div class =" toolbar--delete" >
130- <a href =" #" class =" toolbar--delete__delete" >Delete Item</a >
131- </div >
132- <div class =" form-group" >
133- <label >Username</label >
134- <input type =" text" class =" form-control" name =" username[]" >
135- </div >
136- </div >
137- </div >
84+ <textarea id =" story" name =" story" rows =" 5" cols =" 33" >
85+ It was a dark and stormy night...
86+ </textarea >
13887```
13988
140- You can create block with inputs to repeat for example
89+ ** Select **
14190
14291``` php
14392<?php
14493
145- // Json represent a block with id "users" and with two imputs: username and password.
146- $json = '{
147- "name": "users",
148- "repeat": true,
149- "inputs": [
150- {
151- "type": "text",
152- "name": "username"
153- },
154- {
155- "type": "text",
156- "name": "password"
157- }
158- ]
159- }';
160-
161- $builder = new \micro\form\Builder();
162- $form = $builder->render($json);
163- echo $form;
164- ```
165-
166- html output
167-
168- ``` html
169- <div class =" repeater" >
170- <div class =" element" >
171- <div class =" toolbar--delete" style =" display :none " >
172- <a href =" #" class =" toolbar__delete" >Delete Item</a >
173- </div >
174- <div class =" form-group" >
175- <label >Username</label >
176- <input type =" text" class =" form-control" name =" users[0][username]" >
177- </div >
178- <div class =" form-group" >
179- <label >Password</label >
180- <input type =" text" class =" form-control" name =" users[0][password]" >
181- </div >
182- </div >
183- </div >
184- ```
185-
186- > Please note that in order to the repeater to work you need to add the file micro-form.js to your project.
187-
188- Micro-form supports default values too. Just send a key value array as the second parameter for the render method, for example
189-
190- ``` php
191- <?php
94+ $json = '[
95+ {
96+ "tag": "select",
97+ "name": "pets",
98+ "id": "pet-select",
99+ "value": [
100+ {
101+ "tag": "option",
102+ "label": "--Please choose an option--",
103+ "value": ""
104+ },
105+ {
106+ "tag": "option",
107+ "label": "Dog",
108+ "value": "dog"
109+ },
110+ {
111+ "tag": "option",
112+ "label": "Cat",
113+ "value": "cat"
114+ }
115+ ]
116+ }
117+ ]';
192118
193- $json = '{
194- "inputs": [
195- {
196- "name": "name"
197- },
198- {
199- "name": "lastname"
200- }
201- ]
202- }]';
203-
204- $builder = new \micro\form\Builder();
205- $form = $builder->render($json, ['name' => 'marco', 'lastname' => 'milon']);
206- echo $form;
119+ use micro\FormFactory;
120+ $jsonForm = FormFactory::jsonForm();
121+ echo $jsonForm->render($json);
207122```
208123
209- output
124+ * output*
210125
211126``` html
212- <div class =" form-group" >
213- <label >Name</label >
214- <input type =" text" class =" form-control" name =" name" value =" marco" >
215- </div >
216- <div class =" form-group" >
217- <label >Lastname</label >
218- <input type =" text" class =" form-control" name =" lastname" value =" milon" >
219- </div >
220- ```
221-
222- ### Templates
223-
224- Templates are easy to add. for now only too types of templates are supported: input-text and textarea.
225- To add a template just create a php file as following
226-
227- #### input-text
228-
229- A bootstrap horizontal input text will look like this::
230-
231- ``` php
232- <?php
233- // Template variables
234- $for = isset($id) ? " for=\"$id\"" : "";
235- $id = isset($id) ? " id=\"$id\"" : "";
236- $value = isset($value) ? " value=\"$value\"" : "";
237- $placeholder = isset($placeholder) ? " placeholder=\"$placeholder\"" : "";
238- ?>
239- <div class =" form-group row" >
240- <label <? = $for ? > class="col-sm-2 col-form-label"><?= $label ?></label >
241- <div class =" col-sm-10" >
242- <input type =" <?= $type ?>" class =" form-control" name =" <?= $name ?>" <? = $placeholder . $id . $value ? >>
243- </div >
244- </div >
245- ```
246-
247- #### text-area
248-
249- A bootstrap horizontal textarea will look like this:
250-
251- ``` php
252- <?php
253- // Template variables
254- $for = isset($id) ? " for=\"$id\"" : "";
255- $id = isset($id) ? " id=\"$id\"" : "";
256- $value = isset($value) ? $value : "";
257- $rows = isset($row) ? $row : '3';
258- $placeholder = isset($placeholder) ? " placeholder=\"$placeholder\"" : "";
259- ?>
260- <div class =" form-group row" >
261- <label <? = $for ? > class="col-sm-2 col-form-label"><?= $label ?></label >
262- <div class =" col-sm-10" >
263- <textarea class =" form-control" <? = $id ? > rows="<?= $rows ?>" name="<?= $name ?>"><?= $value ?></textarea >
264- </div >
265- </div >
266- ```
267-
268- #### How to use your custom templates
269-
270- You only need to pass the path to your custom template as an array to the constructor, for example:
271-
272- ``` php
273- <?php
274-
275- $templates = [
276- 'textarea' => dirname(__FILE__) . '/../../../templates/horizontal/textarea.php'
277- ];
278-
279-
280- $builder = new \micro\form\Builder($templates);
281- $form = $builder->render($microForm);
282- $this->string($form)->isEqualToContentsOfFile($expectedRendering);
127+ <select name =" pets" id =" pet-select" >
128+ <option value =" " >--Please choose an option--</option >
129+ <option value =" dog" >Dog</option >
130+ <option value =" cat" >Cat</option >
131+ </select >
283132```
284133
285134### Contribution
0 commit comments