diff --git a/src/css/brutusin-json-forms.css b/src/css/brutusin-json-forms.css index 86a5a2a..7477bc2 100644 --- a/src/css/brutusin-json-forms.css +++ b/src/css/brutusin-json-forms.css @@ -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; } diff --git a/src/js/brutusin-json-forms-bootstrap.js b/src/js/brutusin-json-forms-bootstrap.js index b29e630..46fd20a 100644 --- a/src/js/brutusin-json-forms-bootstrap.js +++ b/src/js/brutusin-json-forms-bootstrap.js @@ -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"; diff --git a/src/js/brutusin-json-forms.js b/src/js/brutusin-json-forms.js index 49ee5ea..1f99f9c 100644 --- a/src/js/brutusin-json-forms.js +++ b/src/js/brutusin-json-forms.js @@ -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", @@ -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 { @@ -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"]; } } @@ -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) { @@ -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; // } @@ -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) { @@ -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; @@ -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; @@ -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; }