Skip to content
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

Issue#37: add support for radio button #149

Closed
wants to merge 16 commits into from
Closed
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
7 changes: 6 additions & 1 deletion src/css/brutusin-json-forms.css
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ form.brutusin-form table, form.brutusin-form input, form.brutusin-form select, f
width: 100% !important;
min-width: 80px;
}
form.brutusin-form input[type=checkbox]{

form.brutusin-form input[type=radio] {
margin: 0 15px 0 10px;
}

form.brutusin-form input[type=checkbox], form.brutusin-form input[type=radio] {
width: auto !important;
min-width: auto !important;
}
Expand Down
2 changes: 1 addition & 1 deletion src/js/brutusin-json-forms-bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if (("undefined" === typeof $ || "undefined" === typeof $.fn || "undefined" ===
BrutusinForms.addDecorator(function (element, schema) {
if (element.tagName) {
var tagName = element.tagName.toLowerCase();
if (tagName === "input" && element.type !== "checkbox" || tagName === "textarea") {
if (tagName === "input" && (element.type !== "checkbox" && element.type !== "radio") || tagName === "textarea") {
element.className += " form-control";
} else if (tagName === "select") {
element.className += " chosen-select form-control";
Expand Down
55 changes: 53 additions & 2 deletions src/js/brutusin-json-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ if (typeof brutusin === "undefined") {
"exclusiveMaximum": "Value must be **lower than** `{0}`",
"minProperties": "At least `{0}` properties are required",
"maxProperties": "At most `{0}` properties are allowed",
"email": "The email must at least consists an asterisk (@), following by a domain name with a dot (.)",
"uniqueItems": "Array items must be unique",
"addItem": "Add item",
"true": "True",
Expand Down Expand Up @@ -228,6 +229,8 @@ if (typeof brutusin === "undefined") {
input.type = "time";
} else if (s.format === "email") {
input.type = "email";
} else if (s.format === "password") {
input.type = "password";
} else if (s.format === "text") {
input = document.createElement("textarea");
} else {
Expand All @@ -252,9 +255,15 @@ if (typeof brutusin === "undefined") {
if (parentSchema && parentSchema.type === "object") {
if (parentSchema.required) {
return BrutusinForms.messages["required"];
} else if (parentSchema.requiredProperties) {
for (var i = 0; i < parentSchema.requiredProperties.length; i++) {
if (parentSchema.requiredProperties[i] === s.$id.substring(2)) {
return BrutusinForms.messages["required"];
}
}
} else {
for (var prop in parentObject) {
if (parentObject[prop] !== null) {
if (parentObject[prop] === null) {
return BrutusinForms.messages["required"];
}
}
Expand All @@ -277,6 +286,13 @@ if (typeof brutusin === "undefined") {
return BrutusinForms.messages["maxLength"].format(s.maxLength);
}
}
//Add a default regex pattern matching for email validation, or else user could use
//the `pattern` field for their own custom regex pattern
if (!s.pattern && s.format === "email") {
if (!value.match(/[^@\s]+@[^@\s]+\.[^@\s]+/)) {
return BrutusinForms.messages["email"];
}
}
}
if (value !== null && !isNaN(value)) {
if (s.multipleOf && value % s.multipleOf !== 0) {
Expand Down Expand Up @@ -321,6 +337,9 @@ if (typeof brutusin === "undefined") {
input.title = s.description;
input.placeholder = s.description;
}
if (s.class) {
input.className = s.class;
}
// if (s.pattern) {
// input.pattern = s.pattern;
// }
Expand All @@ -345,7 +364,29 @@ if (typeof brutusin === "undefined") {
var schemaId = getSchemaId(id);
var s = getSchema(schemaId);
var input;
if (s.required) {
if (s.format === "radio") {
input = document.createElement("div");
for (var i = 0; i < s.enum.length; i++) {
var radioInput = document.createElement("input");
radioInput.type = "radio";
radioInput.name = s.$id.substring(2);
radioInput.value = s.enum[i];
radioInput.id = s.enum[i];
var label = document.createElement("label");
label.htmlFor = s.enum[i];
var labelText = document.createTextNode(s.enum[i]);
appendChild(label, labelText);
if (value && s.enum[i] === value) {
radioInput.checked = true;
}
if (s.readOnly) {
radioInput.disabled = true;
}
appendChild(input, label);
appendChild(input, radioInput, s);
}
}
else if (s.required) {
input = document.createElement("input");
input.type = "checkbox";
if (value === true || value !== false && s.default) {
Expand Down Expand Up @@ -387,6 +428,9 @@ if (typeof brutusin === "undefined") {
};
input.schema = schemaId;
input.id = getInputId();
if (s.class) {
input.className = s.class;
}
inputCounter++;
if (s.description) {
input.title = s.description;
Expand Down Expand Up @@ -874,6 +918,10 @@ if (typeof brutusin === "undefined") {
}
}
var value = removeEmptiesAndNulls(object[prop], ss);
//Check if user assign an empty String in the default field, if true return empty string instead of null
if (ss.default == "" && value === null) {
value = "";
}
if (value !== null) {
clone[prop] = value;
nonEmpty = true;
Expand Down Expand Up @@ -1200,6 +1248,9 @@ if (typeof brutusin === "undefined") {
if (!value) {
if (typeof initialValue !== "undefined" && initialValue !== null) {
value = getInitialValue(id);
if (value === null && typeof s.default !== "undefined") {
value = s.default;
}
} else {
value = s.default;
}
Expand Down