Skip to content

Commit dc0b197

Browse files
committed
fixed form upload, added specs and docs
1 parent 79982c0 commit dc0b197

File tree

9 files changed

+362
-21
lines changed

9 files changed

+362
-21
lines changed

app/concepts/matestack/ui/core/form/form.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ const componentDef = {
4242
},
4343
setProps: function (flat, newVal) {
4444
for (var i in flat) {
45-
if (typeof flat[i] === "object" && !(flat[i] instanceof Array)) {
45+
if (flat[i] instanceof File){
46+
flat[i] = newVal;
47+
this.$refs["input."+i].value = newVal
48+
} else if (flat[i] instanceof Array) {
49+
flat[i] = newVal
50+
this.$refs["input."+i].value = newVal
51+
} else if (typeof flat[i] === "object" && !(flat[i] instanceof Array)) {
4652
setProps(flat[i], newVal);
4753
return;
4854
} else {
@@ -57,7 +63,6 @@ const componentDef = {
5763
this.data[key] = [];
5864
for (let index in files) {
5965
if (files[index] instanceof File) {
60-
console.log(files[index]);
6166
this.data[key].push(files[index]);
6267
}
6368
}
@@ -120,20 +125,19 @@ const componentDef = {
120125
const self = this;
121126
let payload = {};
122127
payload[self.componentConfig["for"]] = self.data;
123-
console.log(this);
124128
let axios_config = {};
125-
// TODO check file uploads
126-
if (this.$vnode.data.attrs.enctype == "multipart/form-data") {
129+
if (self.componentConfig["multipart"] == true ) {
127130
let form_data = new FormData();
128131
for (let key in self.data) {
129132
if (key.endsWith("[]")) {
130133
for (let i in self.data[key]) {
131134
let file = self.data[key][i];
132-
console.log(file);
133-
form_data.append(self.componentConfig["for"] + "[" + key.slice(0, -2) + "][" + i + "]", file, "test_" + i);
135+
form_data.append(self.componentConfig["for"] + "[" + key.slice(0, -2) + "][]", file);
134136
}
135137
} else {
136-
form_data.append(self.componentConfig["for"] + "[" + key + "]", self.data[key]);
138+
if (self.data[key] != null){
139+
form_data.append(self.componentConfig["for"] + "[" + key + "]", self.data[key]);
140+
}
137141
}
138142
}
139143
axios_config = {

app/concepts/matestack/ui/core/form/form.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def setup
99
@component_config[:submit_path] = submit_path
1010
@component_config[:method] = options[:method]
1111
@component_config[:success] = options[:success]
12+
@component_config[:multipart] = options[:multipart] == true
1213
unless options[:success].nil?
1314
unless options[:success][:transition].nil?
1415
@component_config[:success][:transition][:path] = transition_path options[:success]

app/concepts/matestack/ui/core/form/input/input.haml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@
3939
"@change": "inputChanged(\"#{attr_key}\"); filesAdded('#{attr_key}')",
4040
ref: "input.#{attr_key}",
4141
placeholder: placeholder,
42-
"init-value": init_value,
4342
multiple: options[:multiple] }
4443

4544
%span{ class: "errors", "v-if": error_key }
4645
%span{ class: "error", "v-for": "error in #{error_key}" }
47-
{{ error }}
46+
{{ error }}

docs/components/form.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ my_object: {
5757
}
5858
```
5959

60-
6160
#### set by Active Record class name
6261

6362
```ruby
@@ -106,6 +105,13 @@ Using the standard Rails params, we can pass information to our route!
106105
params: { id: 42 }
107106
```
108107

108+
### Multipart
109+
110+
If you want to perform file uploads within this form, you have to set `multipart` to true. It will send the form data with `"Content-Type": "multipart/form-data"`
111+
112+
```ruby
113+
multipart: true # default is false which results in form submission via "Content-Type": "application/json"
114+
```
109115

110116
### Success
111117

@@ -1348,4 +1354,34 @@ datalist id: 'datalist-id' do
13481354
option value: 10
13491355
option value: 20
13501356
end
1351-
```
1357+
```
1358+
1359+
#### Example 11: File Upload
1360+
1361+
In order to perform a single file upload, add this `form_input` component
1362+
1363+
```ruby
1364+
form_input key: :some_file, type: :file
1365+
```
1366+
1367+
In order to perform multiple file uploads, add this `form_input` component
1368+
1369+
```ruby
1370+
form_input key: :some_files, type: :file, multiple: true
1371+
```
1372+
1373+
Don't forget to add the `multiple: true` attribute to your `form_config`!
1374+
1375+
In order to accept multiple files, you should permit params on your controller like:
1376+
1377+
`some_controller.rb`
1378+
```ruby
1379+
#...
1380+
params.require(:my_form_wrapper_key).permit(
1381+
:some_file,
1382+
some_files: []
1383+
)
1384+
#...
1385+
```
1386+
1387+
Please be aware that the `form_input` components with a `type: :file` can not be initialized with a given file.

0 commit comments

Comments
 (0)