-
Notifications
You must be signed in to change notification settings - Fork 432
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
Treat "" as null #83
Comments
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. 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 You still have options: If you can add a new attribute to the HTML, you can use You can use the option $('form').serializeJSON({
parseWithFunction: function(str){return str || null; }
}); You could use the option $('form').serializeJSON({
skipFalsyValuesForFields: ["myfield"]
}); And I realize one more thing, that you should be able to redefine the $('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. |
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:
And added new option:
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?
The text was updated successfully, but these errors were encountered: