Skip to content

Commit

Permalink
Dropped "required" attribute for checkbox fields
Browse files Browse the repository at this point in the history
In JSON Schema, defining a boolean schema key as "required" means
that the property must appear in the generated object. Whether it
is true or false does not matter.

In HTML5, the "required" attribute on a checkbox means "the checkbox
must be checked to proceed". That's not really the same meaning.

JSONForm does not add the "required" attribute to a checkbox anymore
to remain compliant with the definition of "required" in JSON Schema.
As a matter of fact, since falsy values are now set in the generated
object, the "required" constraint on a boolean schema key is always
enforced.
  • Loading branch information
François Daoust committed Jun 4, 2013
1 parent 8d3bb01 commit 4a00a47
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/jsonform.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ var initializeTabs = function (tabs) {
jsonform.fieldTemplate = function(inner) {
return '<div class="control-group jsonform-error-<%= keydash %>' +
'<%= elt.htmlClass ? " " + elt.htmlClass : "" %>' +
'<%= (node.schemaElement && node.schemaElement.required ? " jsonform-required" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.required && (node.schemaElement.type !== "boolean") ? " jsonform-required" : "") %>' +
'<%= (node.formElement && node.formElement.readonly ? " jsonform-readonly" : "") %>' +
'<%= (node.disabled ? " jsonform-disabled" : "") %>' +
'">' +
Expand Down Expand Up @@ -213,7 +213,7 @@ var inputFieldTemplate = function (type) {
'<%= (node.disabled? " disabled" : "")%>' +
'<%= (node.formElement && node.formElement.readonly ? " readonly=\'readonly\'" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.maxLength ? " maxlength=\'" + node.schemaElement.maxLength + "\'" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.required ? " required=\'required\'" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.required && (node.schemaElement.type !== "boolean") ? " required=\'required\'" : "") %>' +
'<%= (node.placeholder? "placeholder=" + \'"\' + escape(node.placeholder) + \'"\' : "")%>' +
' />',
'fieldtemplate': true,
Expand Down Expand Up @@ -418,7 +418,7 @@ jsonform.elementTypes = {
'template': '<label class="checkbox"><input type="checkbox" id="<%= id %>" ' +
'name="<%= node.name %>" value="1" <% if (value) {%>checked<% } %>' +
'<%= (node.disabled? " disabled" : "")%>' +
'<%= (node.schemaElement && node.schemaElement.required ? " required=\'required\'" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.required && (node.schemaElement.type !== "boolean") ? " required=\'required\'" : "") %>' +
' /><span><%= node.inlinetitle || "" %></span>' +
'</label>',
'fieldtemplate': true,
Expand Down Expand Up @@ -690,7 +690,6 @@ jsonform.elementTypes = {
var template = '<label class="checkbox">' +
'<input type="checkbox" <% if (value) { %> checked="checked" <% } %> name="<%= name %>" value="1"' +
'<%= (node.disabled? " disabled" : "")%>' +
'<%= (node.schemaElement && node.schemaElement.required ? " required=\'required\'" : "") %>' +
'/><span><%= title %></span></label>';
if (!node || !node.schemaElement || !node.schemaElement.items) return;
choices = node.schemaElement.items['enum'] ||
Expand Down Expand Up @@ -3252,14 +3251,17 @@ formTree.prototype.submit = function(evt) {
* true as soon as it finds a "required" flag even though, in theory, that
* schema key may not appear in the final form.
*
* Note that a "required" constraint on a boolean type is always enforced,
* the code skips such definitions.
*
* @function
* @return {boolean} True when the form has some required field,
* false otherwise.
*/
formTree.prototype.hasRequiredField = function () {
var parseElement = function (element) {
if (!element) return null;
if (element.required) {
if (element.required && (element.type !== 'boolean')) {
return element;
}

Expand Down

0 comments on commit 4a00a47

Please sign in to comment.