Skip to content

Commit b849cc0

Browse files
committed
Modify validation error handling, handle if schema is null
1 parent d856740 commit b849cc0

File tree

7 files changed

+83
-31
lines changed

7 files changed

+83
-31
lines changed

dev/app.vue

+40-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
.col-md-10.col-md-offset-1
44
data-table(:rows="rows", :selected="selected", :select="selectRow")
55

6-
.row(v-if="model")
6+
.row(v-show="model")
77
.col-md-6
88
.buttons.text-center
99
button.btn.btn-default.new(@click="newModel") New
10-
button.btn.btn-primary.save(@click="saveModel") Save
10+
button.btn.btn-primary.save(@click="saveModel")
11+
| Save
12+
i.fa.fa-warning(v-if="showWarning()")
1113
button.btn.btn-danger.delete(@click="deleteModel") Delete
1214

15+
.errors.text-center
16+
div.alert.alert-danger(v-for="item in validationErrors") {{ item.field.label}}:
17+
strong {{ item.error }}
18+
1319
vue-form-generator(:schema='schema', :model='model', :options='formOptions', :multiple="selected.length > 1", v-ref:form, :is-new-model="isNewModel")
1420

1521

@@ -51,14 +57,30 @@
5157
5258
formOptions: {
5359
validateAfterLoad: true,
54-
validateAfterChanged: true,
60+
validateAfterChanged: false,
5561
validateBeforeSave: true
5662
}
5763
}
5864
},
5965
66+
computed: {
67+
validationErrors() {
68+
if (this.$refs.form && this.$refs.form.errors)
69+
return this.$refs.form.errors;
70+
71+
return [];
72+
}
73+
},
74+
6075
methods: {
76+
showWarning() {
77+
if (this.$refs.form && this.$refs.form.errors) {
78+
return this.$refs.form.errors.length > 0
79+
}
80+
},
81+
6182
selectRow(event, row, add) {
83+
this.isNewModel = false;
6284
if ( (add || (event && event.ctrlKey))) {
6385
if (this.selected.indexOf(row) != -1)
6486
this.selected.$remove(row);
@@ -181,6 +203,21 @@
181203
.boolean { color: magenta; }
182204
.null { color: red; }
183205
.key { color: green; }
206+
}
207+
208+
.buttons {
209+
button {
210+
margin: 0.2em 0.3em;
211+
padding: 6px 20px;
212+
position: relative;
213+
}
214+
215+
i.fa.fa-warning {
216+
position: absolute;
217+
top: 0px;
218+
right: 0px;
219+
color: Orange;
220+
}
184221
}
185222
186223
</style>

dev/data.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import faker from 'faker';
22
import moment from 'moment';
33

4-
module.exports = {
5-
roles: [
6-
{ id: "admin", name: "Administrator"},
7-
{ id: "moderator", name: "Moderator"},
8-
{ id: "user", name: "Registered User"},
9-
{ id: "visitor", name: "Visitor"}
10-
],
4+
let roles = [
5+
{ id: "admin", name: "Administrator"},
6+
{ id: "moderator", name: "Moderator"},
7+
{ id: "user", name: "Registered User"},
8+
{ id: "visitor", name: "Visitor"}
9+
]
10+
11+
let skills = [ "HTML5", "Javascript", "CSS3", "CoffeeScript", "AngularJS", "ReactJS", "VueJS" ];
1112

12-
skills: [ "HTML5", "Javascript", "CSS3", "CoffeeScript", "AngularJS", "ReactJS", "VueJS" ],
13+
module.exports = {
14+
roles,
15+
skills,
1316

1417
users: (function() {
1518
let res = [];
@@ -28,12 +31,12 @@ module.exports = {
2831
min: 1,
2932
max: 10
3033
});
31-
user.role = faker.helpers.randomize(module.exports.roles).id;
34+
user.role = faker.helpers.randomize(roles).id;
3235

3336

3437
user.skills = [];
35-
user.skills.push(faker.helpers.randomize(module.exports.skills));
36-
user.skills.push(faker.helpers.randomize(module.exports.skills));
38+
user.skills.push(faker.helpers.randomize(skills));
39+
user.skills.push(faker.helpers.randomize(skills));
3740

3841
user.language = lang;
3942
user.status = faker.helpers.randomize([true, false, true]);
@@ -66,7 +69,7 @@ module.exports = {
6669
user.accountHistory = undefined;
6770

6871
res.push(user);
69-
console.log(user);
72+
//console.log(user);
7073
}
7174
//console.log(res);
7275
return res;

dev/dataTable.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
tbody
1111
tr(v-for="row in rows", @click="select($event, row)", :class="{ active: isSelected(row) }")
1212
td {{ row.id }}
13-
td {{ row.name }}
13+
td
14+
| {{ row.name }}
15+
.label.label-warning(v-if="!row.status") Inactive
1416
td {{ row.email }}
1517
td {{ row.address.country }}
1618
</template>

dev/main.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
$(function() {
2-
console.log($.fn.selectpicker);
32
let App = require("./App.vue");
43

54
new Vue({

dev/schema.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ module.exports = {
8585
label: "Company name",
8686
model: "company.name",
8787
styleClasses: "company",
88-
visible(model) { return model.type == "business"; }
88+
visible(model) { return model && model.type == "business"; }
8989
},
9090
{
9191
type: "text",
@@ -95,7 +95,7 @@ module.exports = {
9595
pattern: "^\\+[0-9]{2}-[237]0-[0-9]{3}-[0-9]{4}$",
9696
placeholder: "User's phone number",
9797
hint: "Format: +36-(20|30|70)-000-0000",
98-
visible(model) { return model.type == "business"; }
98+
visible(model) { return model && model.type == "business"; }
9999
},
100100
{
101101
type: "email",
@@ -200,7 +200,7 @@ module.exports = {
200200
{
201201
type: "text",
202202
label: "Street",
203-
model: "address.street",
203+
model: "address.streetC",
204204
required: true
205205
},
206206
{
@@ -220,10 +220,15 @@ module.exports = {
220220
if (!model.address.geo)
221221
model.address.geo = {};
222222

223-
if (values.length > 0)
223+
if (values.length > 0 && values[0].trim() != "")
224224
model.address.geo.lat = parseFloat(values[0].trim());
225-
if (values.length > 1)
225+
else
226+
model.address.geo.lat = 0
227+
228+
if (values.length > 1 && values[1].trim() != "")
226229
model.address.geo.lng = parseFloat(values[1].trim());
230+
else
231+
model.address.geo.lng = 0
227232
},
228233
buttons: [
229234
{
@@ -282,7 +287,7 @@ module.exports = {
282287
type: "label",
283288
label: "Created",
284289
model: "created",
285-
get(model) { return model.created ? moment(model.created).format("LLL") : "-"; }
290+
get(model) { return model && model.created ? moment(model.created).format("LLL") : "-"; }
286291
},
287292
{
288293
type: "checkbox",

src/fields/abstractField.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default {
1515
if (isFunction(this.schema.get))
1616
val = this.schema.get(this.model);
1717

18-
else if (this.schema.model)
18+
else if (this.model && this.schema.model)
1919
val = this.$get("model." + this.schema.model);
2020

2121
if (isFunction(this.formatValueToField))
@@ -70,7 +70,6 @@ export default {
7070
let err = validator(this.value, this.schema);
7171
if (err && err.length > 0) {
7272
Array.prototype.push.apply(this.schema.errors, err);
73-
// console.log(this.schema.errors);
7473
}
7574
});
7675

src/formGenerator.vue

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template lang="jade">
2-
table
2+
table(v-if="schema != null")
33
thead
44
tbody
55
tr(v-for="field in fields", v-if="fieldVisible(field)", :class="getFieldRowClasses(field)")
@@ -52,10 +52,12 @@
5252
computed: {
5353
fields() {
5454
let res = [];
55-
each(this.schema.fields, (field) => {
56-
if (!this.multiple || field.multi === true)
57-
res.push(field);
58-
});
55+
if (this.schema) {
56+
each(this.schema.fields, (field) => {
57+
if (!this.multiple || field.multi === true)
58+
res.push(field);
59+
});
60+
}
5961
6062
return res;
6163
}
@@ -129,7 +131,12 @@
129131
if (isFunction(child.validate))
130132
{
131133
let err = child.validate();
132-
Array.prototype.push.apply(this.errors, err);
134+
each(err, (err) => {
135+
this.errors.push({
136+
field: child.schema,
137+
error: err
138+
})
139+
});
133140
}
134141
});
135142

0 commit comments

Comments
 (0)