Skip to content

Commit 00d07e3

Browse files
authored
update using bootstrap (#3)
1 parent e4b9317 commit 00d07e3

20 files changed

+259
-162
lines changed

composer.json

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
{
2-
"name": "gustiawan/laravel-form-builder",
3-
"description": "Laravel Form Builder",
4-
"keywords": ["laravel", "form", "builder"],
5-
"type": "library",
6-
"authors": [
7-
{
8-
"name": "gustiawan",
9-
"email": "[email protected]"
10-
}
11-
],
12-
"autoload": {
13-
"psr-0": {
14-
"Gustiawan\\FormBuilder": "src/"
15-
}
16-
},
17-
"require": {
18-
"php": ">=7.1",
19-
"illuminate/support": "^7.0|^8.0",
20-
"illuminate/validation": "^7.0|^8.0"
21-
},
22-
"minimum-stability": "dev",
23-
"prefer-stable": true,
24-
"extra": {
25-
"laravel": {
26-
"providers": [
27-
"Gustiawan\\FormBuilder\\FormBuilderServiceProvider"
28-
]
29-
}
30-
}
31-
}
1+
{
2+
"name": "gustiawan/laravel-form-builder",
3+
"description": "Laravel Form Builder",
4+
"keywords": ["laravel", "form", "builder"],
5+
"type": "library",
6+
"authors": [
7+
{
8+
"name": "gustiawan",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"autoload": {
13+
"psr-0": {
14+
"Gustiawan\\FormBuilder": "src/"
15+
}
16+
},
17+
"require": {
18+
"php": ">=7.1",
19+
"illuminate/support": "^7.0|^8.0",
20+
"illuminate/validation": "^7.0|^8.0"
21+
},
22+
"minimum-stability": "dev",
23+
"prefer-stable": true,
24+
"extra": {
25+
"laravel": {
26+
"providers": [
27+
"Gustiawan\\FormBuilder\\FormBuilderServiceProvider"
28+
]
29+
}
30+
}
31+
}

src/Gustiawan/FormBuilder/Components/Form.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ public function __construct($form)
2525
*/
2626
public function render()
2727
{
28-
return view('form-generator::components.form');
28+
$view = view('form-generator::components.tailwind.base');
29+
if (config('form_generator.style') == "bootstrap") {
30+
$view = view('form-generator::components.bootstrap.base');
31+
}
32+
33+
if (config('form_generator.form_template') != null) {
34+
return view(config('form_generator.form_template'));
35+
}
36+
37+
return $view;
2938
}
3039
}

src/Gustiawan/FormBuilder/Form.php

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Gustiawan/FormBuilder/FormBuilderServiceProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function boot()
3131

3232
$this->loadViewsFrom(__DIR__.'/views', 'form-generator');
3333

34+
$this->mergeConfigFrom(
35+
__DIR__.'/config/form_generator.php', 'form_generator'
36+
);
37+
38+
$this->publishes([
39+
__DIR__.'/config/form_generator.php' => config_path('form_generator.php'),
40+
]);
41+
3442
if ($this->app->runningInConsole()) {
3543
$this->commands([
3644
CreateForm::class
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
return [
4+
/**
5+
* Set your default styling
6+
* the option is [tailwind, bootstrap]
7+
*/
8+
"style" => "tailwind",
9+
10+
/**
11+
* Fill this with your view path
12+
*/
13+
"form_template" => null
14+
];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<form action="{{ $form->action }}"
2+
method="{{ $form->method != "GET" ? "POST" : "GET" }}"
3+
class="flex justify-center"
4+
>
5+
@if($form->isNeedCsrf())
6+
@csrf
7+
@endif
8+
9+
@if($form->method != "POST" || $form->method != "GET")
10+
@method($form->method)
11+
@endif
12+
@include('form-generator::components.bootstrap.inputs', ["form" => $form])
13+
</form>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@foreach($field['choices'] as $value => $choice)
2+
<div class="form-check">
3+
<input type="checkbox"
4+
class="form-check-input"
5+
name="{{ $field['name'] }}[]"
6+
id="{{ $field['name']."-{$value}" }}"
7+
value="{{ $value }}"
8+
{{ in_array($value, (old($field['name']) ?? $field['value'])) ? "checked" : "" }}>
9+
<label class="form-check-label" for="{{ $field['name']."-{$value}" }}">{{ $choice }}</label>
10+
</div>
11+
@endforeach
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<input class="form-control"
2+
id="{{ $field['name'] }}"
3+
type="{{ $field['type'] }}"
4+
name="{{ $field['name'] }}"
5+
value="{{ old($field['name']) ?? $field['value'] }}"
6+
{{ $field['required'] ? "required" : "" }}>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@foreach($field['choices'] as $value => $choice)
2+
<div class="form-check">
3+
<input type="radio"
4+
class="form-check-input"
5+
name="{{ $field['name'] }}"
6+
value="{{ $value }}"
7+
id="{{ $field['name']."-{$value}" }}"
8+
{{ (old($field['name']) ?? $field['value']) == $value ? "checked" : "" }}
9+
{{ $field['required'] ? "required" : "" }}>
10+
<label class="form-check-label" for="{{ $field['name']."-{$value}" }}">{{ $choice }}</label>
11+
</div>
12+
@endforeach
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<select name="{{ $field['name'] }}" id="{{ $field['name'] }}" class="form-control" {{ $field['required'] ? "required" : "" }}>
2+
<option>Select</option>
3+
@foreach($field['choices'] as $value => $choice)
4+
<option value="{{ $value }}" {{ (old($field['name']) ?? $field['value']) == $value ? "selected" : "" }} >{{ $choice }}</option>
5+
@endforeach
6+
</select>

0 commit comments

Comments
 (0)