Skip to content

Commit a6165c8

Browse files
author
Duncan Lock
committed
Add support for schema.legend & field id prefixes
* Add support for an optional legend for each schema/fieldset. Expects the schema to look something like this: ``` schema: { legend: "Contact Details", fields: [ { type: "input", inputType: "text", label: "Name", model: "name" }, ... ``` * Add support for field id prefixes, at the form level. So you can add a `fieldIdPrefix: 'prefix_here_'` to your `formOptions` and this will be prepended to _all_ field id's.
1 parent 5552079 commit a6165c8

File tree

6 files changed

+99
-10
lines changed

6 files changed

+99
-10
lines changed

dev/multipleforms/app.vue

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<template lang="html">
2+
<form>
3+
<vue-form-generator :schema="section1" :model="model" :options="formOptions"></vue-form-generator>
4+
<vue-form-generator :schema="section2" :model="model" :options="formOptions"></vue-form-generator>
5+
</form>
6+
</template>
7+
8+
<script>
9+
import Vue from "vue";
10+
11+
export default {
12+
data () {
13+
return {
14+
model: {
15+
name: 'Brian Blessed',
16+
17+
pref_1: 'blah'
18+
},
19+
20+
section1: {
21+
legend: "Contact Details",
22+
fields: [
23+
{
24+
type: "input",
25+
inputType: "text",
26+
label: "Name",
27+
model: "name"
28+
},
29+
{
30+
type: "input",
31+
inputType: "email",
32+
label: "Email",
33+
model: "email"
34+
}
35+
]
36+
},
37+
38+
section2: {
39+
fields: [
40+
{
41+
type: "input",
42+
inputType: "text",
43+
label: "Pref 1",
44+
model: "pref_1"
45+
}
46+
]
47+
},
48+
49+
formOptions: {
50+
fieldIdPrefix: 'frm1_'
51+
}
52+
}
53+
},
54+
55+
created() {
56+
window.app = this;
57+
}
58+
}
59+
</script>

dev/multipleforms/index.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>vue-form-generator multiple forms demo</title>
6+
</head>
7+
<body>
8+
<div class="container-fluid"></div>
9+
<div id="app"></div>
10+
<script src="/mforms.js"></script>
11+
</body>
12+
</html>

dev/multipleforms/main.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Vue from "vue";
2+
import VueFormGenerator from "../../src";
3+
Vue.use(VueFormGenerator);
4+
5+
import App from './app.vue';
6+
7+
new Vue({
8+
...App
9+
}).$mount('#app');

src/fields/abstractField.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ export default {
167167
// then slugify it.
168168
if (typeof schema.id !== "undefined") {
169169
// If an ID's been explicitly set, use it unchanged
170-
return schema.id;
170+
return schema.idPrefix + schema.id;
171171
} else {
172172
// Return the slugified version of either:
173-
return (schema.inputName || schema.label || schema.model)
173+
return schema.idPrefix + (schema.inputName || schema.label || schema.model)
174174
// NB: This is a very simple, conservative, slugify function,
175175
// avoiding extra dependencies.
176176
.toString()

src/formGenerator.vue

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template lang="pug">
22
div
33
fieldset.vue-form-generator(v-if='schema != null', :is='tag')
4+
legend(v-if='schema.legend') {{ schema.legend }}
45
template(v-for='field in fields')
56
.form-group(v-if='fieldVisible(field)', :class='getFieldRowClasses(field)')
67
label(v-if="fieldTypeHasLabel(field)", :for="getFieldID(field)")
@@ -122,6 +123,13 @@ div
122123
}
123124
},
124125
126+
beforeMount() {
127+
// Add idPrefix to fields if fieldIdPrefix is set
128+
for (let field of this.schema.fields) {
129+
field.idPrefix = this.options.fieldIdPrefix || "";
130+
}
131+
},
132+
125133
mounted() {
126134
this.$nextTick(() => {
127135
if (this.model) {

webpack.dev.config.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@ var loaders = [
1717
test: /\.json$/,
1818
loader: 'json'
1919
},
20-
{
21-
test: /\.(woff2?|svg)$/,
22-
loader: "url"
23-
//loader: "url?limit=10000"
20+
{
21+
test: /\.(woff2?|svg)$/,
22+
loader: "url"
23+
//loader: "url?limit=10000"
2424
},
25-
{
26-
test: /\.(ttf|eot)$/,
27-
loader: "url"
25+
{
26+
test: /\.(ttf|eot)$/,
27+
loader: "url"
2828
}
2929
];
3030

3131
module.exports = {
3232
devtool: "source-map",
33-
33+
3434
entry: {
3535
full: path.resolve("dev", "full", "main.js"),
3636
mselect: path.resolve("dev", "multiselect", "main.js"),
37+
mforms: path.resolve("dev", "multipleforms", "main.js"),
3738
checklist: path.resolve("dev", "checklist", "main.js")
3839
},
3940

0 commit comments

Comments
 (0)