Skip to content

Commit

Permalink
Allow form fields required state to be controlled with a function,
Browse files Browse the repository at this point in the history
just like it's already possible with "visible" and "disabled"
  • Loading branch information
mpociot committed Jan 12, 2017
1 parent ea91365 commit e08dbbb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/formGenerator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ div
disabled: this.fieldDisabled(field),
readonly: field.readonly,
featured: field.featured,
required: field.required
required: this.fieldRequired(field)
};
if (isArray(field.styleClasses)) {
Expand Down Expand Up @@ -139,6 +139,17 @@ div
return field.disabled;
},
// Get required prop of field
fieldRequired(field) {
if (isFunction(field.required))
return field.required(this.model);
if (isNil(field.required))
return false;
return field.required;
},
// Get visible prop of field
fieldVisible(field) {
if (isFunction(field.visible))
Expand Down
35 changes: 35 additions & 0 deletions test/unit/specs/VueFormGenerator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,41 @@ describe("VueFormGenerator.vue", () => {

});

describe("check fieldRequired with function", () => {
let schema = {
fields: [
{
type: "text",
label: "Name",
model: "name",
required(model) { return model.status; }
}
]
};

let model = {
name: "John Doe",
status: true
};

before( () => {
createFormGenerator(schema, model);
});

it("should be required", () => {
expect(el.querySelector(".form-group").classList.contains("required")).to.be.true;
});

it("should be optional", (done) => {
model.status = false;
vm.$nextTick(() => {
expect(el.querySelector(".form-group").classList.contains("required")).to.be.false;
done();
});
});

});

describe("check fieldVisible with function", () => {
let schema = {
fields: [
Expand Down

0 comments on commit e08dbbb

Please sign in to comment.