Skip to content

Allow form fields required state to be controlled with a function #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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