marioizquierdo / jquery.serializeJSON

Serialize an HTML Form to a JavaScript Object, supporting nested attributes and arrays.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Treat "" as null

JocaPC opened this issue · comments

In some cases it would be good to treat empty values from inputs as null instead of the empty string "".
.serializeArray() always returns "" for the inputs that are not populated. I had a problem with parsing on server-side because some numeric fields that are not populated cannot be converted on the server side from "" to null.

I have changed this in the code:

 $.each(formAsArray, function (i, obj) {
        name = obj.name; // original input name
        value = obj.value; // input value

        /// START
 if (opts.useNullAsEmptyString && value === "")
     value = null;
        /// END

And added new option:

  useNullAsEmptyString: false    // useNullAsEmptyString:true => { foo: null }, instead of { foo: "" }

It would be good to introduce this as an option.

I can send PR with the change and the option, but I would need to know are you fine with the option name.

Also, do you think that this should be default option or it would be breaking change?

This would be a breaking change. SerializeJSON defaults are designed to behave as close as possible to regular HTML forms, then the options help customize to your case.

I don't think we need to make a new option, because you can define custom types. For your case, you could solve this by making a new type:

$('form').serializeJSON({
  customTypes: {
    emptyNull: function(str) { return str || null; }
  }
});

Hi,

A custom type is not an option because I cannot change the name (name is generated based on model properties so it can be populated by matching JSON properties and form input names). A custom type in class might be an option but it is not supported.
The problem is that you are relying on JQuery.serializeArray() that ignores HTML5 input type, so when it serializes it returns string if field is not populated. Unfortunately in current code I cannot determine the type of original input.
You made the same change when you parse "true"/"false" as true/false for checkbox type.

My proposal would be to introduce new option that would be disabled by default and allow customization for that kind of input types.

Jovan

Oh, I see. You can not edit the HTML to add a type to the name attribute like name="myfield:emptyNull".

You still have options:

If you can add a new attribute to the HTML, you can use data-value-type to apply your custom type. For example data-value-type="emptyNull".

You can use the option parseWithFunction. For example:

$('form').serializeJSON({
  parseWithFunction: function(str){return str || null; }
});

You could use the option skipFalsyValuesForFields. This will make sure that empty values are not serialized, which your server could interpret as null values. For example:

$('form').serializeJSON({
  skipFalsyValuesForFields: ["myfield"]
});

And I realize one more thing, that you should be able to redefine the :string type to work as you want. In the docs I mention that fields with no type are considered :string by default, therefore this should work:

$('form').serializeJSON({
  customTypes: {
    string: function(str) { return str || null; }
  }
});

This should work for fields that don't specify a type. But I tested it locally and it doesn't work in this current version. But I think I will release a new version with this feature.