Skip to content

Add a new option to handle differently the checkboxes with true/false values #31

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.hg
.hgignore
*.iml
42 changes: 25 additions & 17 deletions src/form2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ var form2js = (function()
* @param skipEmpty {Boolean} should skip empty text values, defaults to true
* @param nodeCallback {Function} custom function to get node value
* @param useIdIfEmptyName {Boolean} if true value of id attribute of field will be used if name of field is empty
* @param forceCheckBoxValue {Boolean} if true the real value of checkbox is used in case of true|false checkboxes
*/
function form2js(rootNode, delimiter, skipEmpty, nodeCallback, useIdIfEmptyName)
function form2js(rootNode, delimiter, skipEmpty, nodeCallback, useIdIfEmptyName, forceCheckBoxValue)
{
if (typeof skipEmpty == 'undefined' || skipEmpty == null) skipEmpty = true;
if (typeof delimiter == 'undefined' || delimiter == null) delimiter = '.';
Expand All @@ -56,12 +57,12 @@ var form2js = (function()
{
while(currNode = rootNode[i++])
{
formValues = formValues.concat(getFormValues(currNode, nodeCallback, useIdIfEmptyName));
formValues = formValues.concat(getFormValues(currNode, nodeCallback, useIdIfEmptyName, forceCheckBoxValue));
}
}
else
{
formValues = getFormValues(rootNode, nodeCallback, useIdIfEmptyName);
formValues = getFormValues(rootNode, nodeCallback, useIdIfEmptyName, forceCheckBoxValue);
}

return processNameValues(formValues, skipEmpty, delimiter);
Expand Down Expand Up @@ -213,44 +214,44 @@ var form2js = (function()
return result;
}

function getFormValues(rootNode, nodeCallback, useIdIfEmptyName)
function getFormValues(rootNode, nodeCallback, useIdIfEmptyName, forceCheckBoxValue)
{
var result = extractNodeValues(rootNode, nodeCallback, useIdIfEmptyName);
return result.length > 0 ? result : getSubFormValues(rootNode, nodeCallback, useIdIfEmptyName);
var result = extractNodeValues(rootNode, nodeCallback, useIdIfEmptyName, forceCheckBoxValue);
return result.length > 0 ? result : getSubFormValues(rootNode, nodeCallback, useIdIfEmptyName, forceCheckBoxValue);
}

function getSubFormValues(rootNode, nodeCallback, useIdIfEmptyName)
function getSubFormValues(rootNode, nodeCallback, useIdIfEmptyName, forceCheckBoxValue)
{
var result = [],
currentNode = rootNode.firstChild;

while (currentNode)
{
result = result.concat(extractNodeValues(currentNode, nodeCallback, useIdIfEmptyName));
result = result.concat(extractNodeValues(currentNode, nodeCallback, useIdIfEmptyName, forceCheckBoxValue));
currentNode = currentNode.nextSibling;
}

return result;
}

function extractNodeValues(node, nodeCallback, useIdIfEmptyName) {
var callbackResult, fieldValue, result, fieldName = getFieldName(node, useIdIfEmptyName);
function extractNodeValues(node, nodeCallback, useIdIfEmptyName, forceCheckBoxValue) {
var callbackResult, fieldValue, result, fieldName = getFieldName(node, useIdIfEmptyName, forceCheckBoxValue);

callbackResult = nodeCallback && nodeCallback(node);

if (callbackResult && callbackResult.name) {
result = [callbackResult];
}
else if (fieldName != '' && node.nodeName.match(/INPUT|TEXTAREA/i)) {
fieldValue = getFieldValue(node);
fieldValue = getFieldValue(node, forceCheckBoxValue);
result = [ { name: fieldName, value: fieldValue} ];
}
else if (fieldName != '' && node.nodeName.match(/SELECT/i)) {
fieldValue = getFieldValue(node);
fieldValue = getFieldValue(node, forceCheckBoxValue);
result = [ { name: fieldName.replace(/\[\]$/, ''), value: fieldValue } ];
}
else {
result = getSubFormValues(node, nodeCallback, useIdIfEmptyName);
result = getSubFormValues(node, nodeCallback, useIdIfEmptyName, forceCheckBoxValue);
}

return result;
Expand All @@ -264,18 +265,25 @@ var form2js = (function()
}


function getFieldValue(fieldNode)
function getFieldValue(fieldNode, forceCheckBoxValue)
{
if (fieldNode.disabled) return null;

switch (fieldNode.nodeName) {
case 'INPUT':
case 'TEXTAREA':
switch (fieldNode.type.toLowerCase()) {
case 'radio':
case 'checkbox':
if (fieldNode.checked && fieldNode.value === "true") return true;
if (!fieldNode.checked && fieldNode.value === "true") return false;

if (forceCheckBoxValue) {
if (fieldNode.checked && fieldNode.value === "false") return false;
if (!fieldNode.checked && fieldNode.value === "true") return '';
} else {
if (!fieldNode.checked && fieldNode.value === "true") return false;
}

if (fieldNode.checked) return fieldNode.value;
break;

Expand Down
5 changes: 3 additions & 2 deletions src/jquery.toObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
delimiter: ".",
skipEmpty: true,
nodeCallback: null,
useIdIfEmptyName: false
useIdIfEmptyName: false,
forceCheckBoxValue: false
};

if (options)
Expand All @@ -49,7 +50,7 @@
switch(settings.mode)
{
case 'first':
return form2js(this.get(0), settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName);
return form2js(this.get(0), settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName, settings.forceCheckBoxValue);
break;
case 'all':
this.each(function(){
Expand Down