Skip to content

Commit

Permalink
🐛 fix: Handle non selected lists
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed May 10, 2016
1 parent 9bd87a9 commit 1714776
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 32 deletions.
2 changes: 1 addition & 1 deletion dev/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
| Delete

.errors.text-center
div.alert.alert-danger(v-for="item in validationErrors") {{ item.field.label}}:
div.alert.alert-danger(v-for="item in validationErrors", track-by="$index") {{ item.field.label}}:
strong {{ item.error }}

vue-form-generator(:schema='schema', :model='model', :options='formOptions', :multiple="selected.length > 1", v-ref:form, :is-new-model="isNewModel")
Expand Down
27 changes: 16 additions & 11 deletions dev/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,26 @@ module.exports = {
type: "masked",
label: "Mobile",
model: "mobile",
mask: "(99) 999-9999"
mask: "(99) 999-9999",
validator: validators.required
},
{
type: "spectrum",
label: "Color",
model: "favoriteColor",
required: true,
colorOptions: {
//preferredFormat: "rgb"
}
},
validator: validators.required
},
{
type: "image",
label: "Avatar",
model: "avatar",
browse: true
required: true,
browse: true,
validator: validators.required

},
{
Expand All @@ -157,6 +162,7 @@ module.exports = {
label: "DOB",
model: "dob",
multi: true,
required: true,
placeholder: "User's birth of date",
min: moment("1900-01-01").toDate(),
max: moment("2016-01-01").toDate(),
Expand Down Expand Up @@ -208,13 +214,11 @@ module.exports = {
multi: true,
min: 1,
max: 10,
required: true,
sliderOptions: {
grid: true
},
validator: [
validators.integer,
validators.number
]
validator: validators.integer
},

{
Expand Down Expand Up @@ -244,7 +248,7 @@ module.exports = {
{ id: "it", name: "Italic" },
{ id: "fr", name: "French" }
],
default: "en_GB"
validator: validators.required
},
{
type: "selectEx",
Expand All @@ -253,13 +257,14 @@ module.exports = {
multi: true,
required: true,
values: faker.definitions.address.country,
default: "United Kingdom",
//default: "United Kingdom",
multiSelect: false,
selectOptions: {
// https://silviomoreto.github.io/bootstrap-select/options/
liveSearch: true,
size: 10
}
},
validator: validators.required
},
{
type: "text",
Expand Down Expand Up @@ -351,7 +356,7 @@ module.exports = {
{ id: "user", name: "Registered User"},
{ id: "visitor", name: "Visitor"}
],
default: null
validator: validators.required
},
{
type: "label",
Expand Down
1 change: 1 addition & 0 deletions src/fields/fieldSelect.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template lang="jade">
select.form-control(v-model="value", :disabled="disabled")
option(:disabled="schema.required", :value="null", :selected="value == undefined") &lt;Not selected&gt;
option(v-for="item in items", :value="getItemID(item)") {{ getItemName(item) }}
</template>

Expand Down
1 change: 1 addition & 0 deletions src/fields/fieldSelectEx.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template lang="jade">
select.selectpicker(v-model="value", :disabled="disabled", :multiple="schema.multiSelect", :title="schema.placeholder", data-width="100%")
option(:disabled="schema.required", v-if="schema.multiSelect !== true", :value="null", :selected="value == undefined") &lt;Not selected&gt;
option(v-for="item in items", :value="getItemID(item)") {{ getItemName(item) }}
</template>

Expand Down
8 changes: 6 additions & 2 deletions src/formGenerator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
button.btn.btn-default(v-for="btn in field.buttons", @click="btn.onclick(model, field)", :class="btn.classes") {{ btn.label }}
.hint(v-if="field.hint") {{ field.hint }}
.errors(v-if="field.errors && field.errors.length > 0")
span(v-for="error in field.errors") {{ error }}
span(v-for="error in field.errors", track-by="$index") {{ error }}
</template>

<script>
Expand Down Expand Up @@ -65,7 +65,11 @@
watch: {
// new model loaded
model: function() {
model: function(newModel, oldModel) {
if (oldModel == newModel) // model got a new property
return;
console.log("Model changed!");
if (this.options.validateAfterLoad === true && this.isNewModel !== true)
this.validate();
else
Expand Down
4 changes: 4 additions & 0 deletions src/utils/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function checkEmpty(value, required) {

module.exports = {

required(value, field) {
return checkEmpty(value, field.required);
},

number(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;

Expand Down
35 changes: 26 additions & 9 deletions test/unit/specs/fields/fieldSelect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe("fieldSelect.vue", () => {
type: "select",
label: "Cities",
model: "city",
required: false,
values: [
"London",
"Paris",
Expand All @@ -46,11 +47,17 @@ describe("fieldSelect.vue", () => {

it("should contain option elements", () => {
let options = input.querySelectorAll("option");
expect(options.length).to.be.equal(4);
expect(options.length).to.be.equal(4 + 1); // +1 for <non selected>

expect(options[1].value).to.be.equal("Paris");
expect(options[1].textContent).to.be.equal("Paris");
expect(options[1].selected).to.be.true;
expect(options[2].value).to.be.equal("Paris");
expect(options[2].textContent).to.be.equal("Paris");
expect(options[2].selected).to.be.true;
});

it("should contain a <non selected> element", () => {
let options = input.querySelectorAll("option");
expect(options[0].disabled).to.be.false;
expect(options[0].textContent).to.be.equal("<Not selected>");
});

it("should contain the value", (done) => {
Expand Down Expand Up @@ -88,6 +95,16 @@ describe("fieldSelect.vue", () => {

});

it("should contain a disabled <non selected> element if required", (done) => {
schema.required = true;
vm.$nextTick( () => {
let options = input.querySelectorAll("option");
expect(options[0].disabled).to.be.true;
expect(options[0].textContent).to.be.equal("<Not selected>");
done();
});
});

});

describe("check static values with { id, name } objects", () => {
Expand All @@ -112,12 +129,12 @@ describe("fieldSelect.vue", () => {

it("should contain option elements", () => {
let options = input.querySelectorAll("option");
expect(options.length).to.be.equal(4);
expect(options.length).to.be.equal(4 + 1); // +1 for <non selected>

expect(options[1].value).to.be.equal("2");
expect(options[1].textContent).to.be.equal("Paris");
expect(options[1].selected).to.be.true;
expect(options[0].selected).to.be.false;
expect(options[2].value).to.be.equal("2");
expect(options[2].textContent).to.be.equal("Paris");
expect(options[2].selected).to.be.true;
expect(options[1].selected).to.be.false;
});

it("should contain the value", (done) => {
Expand Down
38 changes: 29 additions & 9 deletions test/unit/specs/fields/fieldSelectEx.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe("fieldSelectEx.vue", () => {
label: "Cities",
model: "city",
multiSelect: false,
required: false,
values: [
"London",
"Paris",
Expand Down Expand Up @@ -47,13 +48,19 @@ describe("fieldSelectEx.vue", () => {

it("should contain option elements", () => {
let options = input.querySelectorAll("option");
expect(options.length).to.be.equal(4);
expect(options.length).to.be.equal(4 + 1); // +1 for <non selected>

expect(options[1].value).to.be.equal("Paris");
expect(options[1].textContent).to.be.equal("Paris");
expect(options[1].selected).to.be.true;
expect(options[2].value).to.be.equal("Paris");
expect(options[2].textContent).to.be.equal("Paris");
expect(options[2].selected).to.be.true;
});

it("should contain a <non selected> element", () => {
let options = input.querySelectorAll("option");
expect(options[0].disabled).to.be.false;
expect(options[0].textContent).to.be.equal("<Not selected>");
});

it("should contain the value", (done) => {
vm.$nextTick( () => {
expect(input.value).to.be.equal("Paris");
Expand Down Expand Up @@ -89,10 +96,23 @@ describe("fieldSelectEx.vue", () => {

});

it("should contain a disabled <non selected> element if required", (done) => {
schema.required = true;
vm.$nextTick( () => {
let options = input.querySelectorAll("option");
//expect(options[0].disabled).to.be.true;
expect(options[0].textContent).to.be.equal("<Not selected>");
done();
});
});

it("should not be multiple", (done) => {
schema.multiSelect = true;
vm.$nextTick( () => {
expect(input.multiple).to.be.true;
let options = input.querySelectorAll("option");
expect(options.length).to.be.equal(4); // no <non selected>

done();
});
});
Expand Down Expand Up @@ -121,12 +141,12 @@ describe("fieldSelectEx.vue", () => {

it("should contain option elements", () => {
let options = input.querySelectorAll("option");
expect(options.length).to.be.equal(4);
expect(options.length).to.be.equal(4 + 1); // +1 for <non selected>

expect(options[1].value).to.be.equal("2");
expect(options[1].textContent).to.be.equal("Paris");
expect(options[1].selected).to.be.true;
expect(options[0].selected).to.be.false;
expect(options[2].value).to.be.equal("2");
expect(options[2].textContent).to.be.equal("Paris");
expect(options[2].selected).to.be.true;
expect(options[1].selected).to.be.false;
});

it("should contain the value", (done) => {
Expand Down

0 comments on commit 1714776

Please sign in to comment.