diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a40b54b..514660fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * The `Alchemy.ClientSession` class should now be used as a map to store session data * Fix `Field.Schema` contents not being converted from database representation when the schema also has a relation * Make `Criteria#contains(value)` work as expected on array fields +* Don't cast empty strings to `0` values in numeric `Field` types ## 1.3.22 (2023-12-21) diff --git a/lib/app/helper_field/10-number_field.js b/lib/app/helper_field/10-number_field.js index 1384f9fc..2aa21fcd 100644 --- a/lib/app/helper_field/10-number_field.js +++ b/lib/app/helper_field/10-number_field.js @@ -7,9 +7,7 @@ * @since 0.2.0 * @version 1.1.0 */ -var NumberField = Function.inherits('Alchemy.Field', function Number(schema, name, options) { - Number.super.call(this, schema, name, options); -}); +const NumberField = Function.inherits('Alchemy.Field', 'Number'); /** * Set the datatype name @@ -25,14 +23,18 @@ NumberField.setDatatype('number'); * * @author Jelle De Loecker * @since 0.2.0 - * @version 0.4.1 + * @version 1.4.0 * - * @param {Mixed} value + * @param {*} value * * @return {number} */ NumberField.setMethod(function cast(value) { + if (value === '') { + value = null; + } + // Allow null if (value == null) { return value; diff --git a/lib/app/helper_field/20-decimal_field.js b/lib/app/helper_field/20-decimal_field.js index 2e9b70cb..fe7b5266 100644 --- a/lib/app/helper_field/20-decimal_field.js +++ b/lib/app/helper_field/20-decimal_field.js @@ -14,7 +14,7 @@ const DecimalField = Function.inherits('Alchemy.Field.Number', 'Decimal'); * * @author Jelle De Loecker * @since 1.3.20 - * @version 1.3.20 + * @version 1.4.0 * * @param {Mixed} value * @@ -22,6 +22,10 @@ const DecimalField = Function.inherits('Alchemy.Field.Number', 'Decimal'); */ DecimalField.setMethod(function cast(value) { + if (value === '') { + value = null; + } + // Allow null if (value == null) { return value; diff --git a/lib/app/helper_field/big_int_field.js b/lib/app/helper_field/big_int_field.js index e8fb8a92..a07c7dc0 100644 --- a/lib/app/helper_field/big_int_field.js +++ b/lib/app/helper_field/big_int_field.js @@ -29,7 +29,7 @@ BigIntField.setDatatype('bigint'); * * @author Jelle De Loecker * @since 1.3.6 - * @version 1.3.6 + * @version 1.4.0 * * @param {Mixed} value * @@ -37,6 +37,10 @@ BigIntField.setDatatype('bigint'); */ BigIntField.setMethod(function cast(value) { + if (value === '') { + value = null; + } + // Allow null if (value == null) { return value; diff --git a/lib/app/helper_field/fixed_decimal_field.js b/lib/app/helper_field/fixed_decimal_field.js index 36e68941..87620d6f 100644 --- a/lib/app/helper_field/fixed_decimal_field.js +++ b/lib/app/helper_field/fixed_decimal_field.js @@ -21,7 +21,7 @@ const FixedDecimalField = Function.inherits('Alchemy.Field.Decimal', function Fi * * @author Jelle De Loecker * @since 1.3.20 - * @version 1.3.20 + * @version 1.4.0 * * @param {Mixed} value * @@ -29,6 +29,10 @@ const FixedDecimalField = Function.inherits('Alchemy.Field.Decimal', function Fi */ FixedDecimalField.setMethod(function cast(value) { + if (value === '') { + value = null; + } + // Allow null if (value == null) { return value; diff --git a/lib/app/helper_field/integer_field.js b/lib/app/helper_field/integer_field.js index 4c482834..1d446187 100644 --- a/lib/app/helper_field/integer_field.js +++ b/lib/app/helper_field/integer_field.js @@ -7,23 +7,25 @@ * @since 1.1.0 * @version 1.1.0 */ -var IntegerField = Function.inherits('Alchemy.Field.Number', function Integer(schema, name, options) { - Integer.super.call(this, schema, name, options); -}); +const IntegerField = Function.inherits('Alchemy.Field.Number', 'Integer'); /** * Cast the given value to this field's type * * @author Jelle De Loecker * @since 1.1.0 - * @version 1.1.0 + * @version 1.4.0 * - * @param {Mixed} value + * @param {*} value * * @return {number} */ IntegerField.setMethod(function cast(value) { + if (value === '') { + value = null; + } + // Allow null if (value == null) { return value;