From fcc07d56e215a3672e891ea476c1ce6bae3864de Mon Sep 17 00:00:00 2001 From: robinknaapen Date: Thu, 3 May 2018 16:03:34 +0200 Subject: [PATCH] Add radio support --- serializer.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/serializer.js b/serializer.js index 203bdae..f7f44d5 100644 --- a/serializer.js +++ b/serializer.js @@ -48,6 +48,12 @@ class SerializerField { return new Date(this.field.value); } + if (this.type == "radio") { + if (!this.field.checked) { + return null + } + } + if (this.type == "checkbox") { let checked = this.field.checked; if (this.field.hasAttribute("string")) { @@ -103,6 +109,7 @@ class Serializer { } let isArray = fieldName.match(/^\[(\d*)\]/); + let isRadio = f.type == "radio"; fieldName = fieldName.replace(/^\[\d*\]/, ""); @@ -131,6 +138,12 @@ class Serializer { } else { this._fields.get(fieldName).push(field); } + } else if (isRadio) { + if (!this._fields.has(fieldName)) { + this._fields.set(fieldName, []); + } + + this._fields.get(fieldName).push(field); } else { this._fields.set(fieldName, field); } @@ -168,6 +181,21 @@ class Serializer { json = []; } for (let key in f) { + if (f[key].type == "radio") { + json = {} + + let d = f[key].serialize(); + if (d !== null) { + if (k == "") { + json = d; + break; + } + json[k] = d; + break; + } + + continue + } if (f[key]) { let d = f[key].serialize(); if (d == null) {