diff --git a/src/addons/mod/data/fields/number/services/handler.ts b/src/addons/mod/data/fields/number/services/handler.ts index 05ea5dd74..5846662c4 100644 --- a/src/addons/mod/data/fields/number/services/handler.ts +++ b/src/addons/mod/data/fields/number/services/handler.ts @@ -45,17 +45,17 @@ export class AddonModDataFieldNumberHandlerService extends AddonModDataFieldText originalFieldData: AddonModDataEntryField, ): boolean { const fieldName = 'f_' + field.id; - const input = inputData[fieldName] || ''; - const content = originalFieldData?.content || ''; + const input = inputData[fieldName] ?? ''; + const content = originalFieldData?.content ?? ''; - return input != content; + return input !== content; } /** * @inheritdoc */ getFieldsNotifications(field: AddonModDataField, inputData: AddonModDataSubfieldData[]): string | undefined { - if (field.required && (!inputData || !inputData.length || inputData[0].value == '')) { + if (field.required && (!inputData || !inputData.length || inputData[0].value === '')) { return Translate.instant('addon.mod_data.errormustsupplyvalue'); } } diff --git a/src/addons/mod/data/fields/text/services/handler.ts b/src/addons/mod/data/fields/text/services/handler.ts index 8083b72b8..40088d230 100644 --- a/src/addons/mod/data/fields/text/services/handler.ts +++ b/src/addons/mod/data/fields/text/services/handler.ts @@ -70,7 +70,7 @@ export class AddonModDataFieldTextHandlerService implements AddonModDataFieldHan return [{ fieldid: field.id, - value: inputData[fieldName] || '', + value: inputData[fieldName] ?? '', }]; } @@ -83,10 +83,10 @@ export class AddonModDataFieldTextHandlerService implements AddonModDataFieldHan originalFieldData: AddonModDataEntryField, ): boolean { const fieldName = 'f_' + field.id; - const input = inputData[fieldName] || ''; - const content = originalFieldData?.content || ''; + const input = inputData[fieldName] ?? ''; + const content = originalFieldData?.content ?? ''; - return input != content; + return input !== content; } /** @@ -102,7 +102,7 @@ export class AddonModDataFieldTextHandlerService implements AddonModDataFieldHan * @inheritdoc */ overrideData(originalContent: AddonModDataEntryField, offlineContent: CoreFormFields): AddonModDataEntryField { - originalContent.content = offlineContent[''] || ''; + originalContent.content = offlineContent[''] ?? ''; return originalContent; } diff --git a/src/addons/mod/data/pages/edit/edit.ts b/src/addons/mod/data/pages/edit/edit.ts index e95ec4a5e..6509bc4a4 100644 --- a/src/addons/mod/data/pages/edit/edit.ts +++ b/src/addons/mod/data/pages/edit/edit.ts @@ -42,6 +42,7 @@ import { import { AddonModDataHelper } from '../../services/data-helper'; import { CoreDom } from '@singletons/dom'; import { AddonModDataEntryFieldInitialized } from '../../classes/base-field-plugin-component'; +import { CoreTextUtils } from '@services/utils/text'; /** * Page that displays the view edit page. @@ -368,9 +369,18 @@ export class AddonModDataEditPage implements OnInit { } }); } + this.jsData!.errors = this.errors; this.scrollToFirstError(); + + if (updateEntryResult.generalnotifications?.length) { + CoreDomUtils.showAlertWithOptions({ + header: Translate.instant('core.notice'), + message: CoreTextUtils.buildMessage(updateEntryResult.generalnotifications), + buttons: [Translate.instant('core.ok')], + }); + } } } finally { modal.dismiss(); diff --git a/src/addons/mod/data/services/data-helper.ts b/src/addons/mod/data/services/data-helper.ts index c83abf47a..52b822c95 100644 --- a/src/addons/mod/data/services/data-helper.ts +++ b/src/addons/mod/data/services/data-helper.ts @@ -590,8 +590,8 @@ export class AddonModDataHelperProvider { // WS wants values in JSON format. entryFieldDataToSend.push({ fieldid: fieldSubdata.fieldid, - subfield: fieldSubdata.subfield || '', - value: value ? JSON.stringify(value) : '', + subfield: fieldSubdata.subfield ?? '', + value: (value || value === 0) ? JSON.stringify(value) : '', }); return; diff --git a/src/addons/mod/data/tests/behat/entries.feature b/src/addons/mod/data/tests/behat/entries.feature index 59320cca3..bd6707cdf 100644 --- a/src/addons/mod/data/tests/behat/entries.feature +++ b/src/addons/mod/data/tests/behat/entries.feature @@ -206,3 +206,22 @@ Feature: Users can manage entries in database activities Then I should find "Are you sure you want to delete this entry?" in the app And I press "Delete" in the app And I should not find "Moodle Cloud" in the app + + Scenario: Handle number 0 correctly when creating entries + Given the following "activities" exist: + | activity | name | intro | course | idnumber | + | data | Number DB | Number DB | C1 | data2 | + And the following "mod_data > fields" exist: + | database | type | name | description | + | data2 | number | Number | Number value | + And I entered the data activity "Number DB" on course "Course 1" as "student1" in the app + When I press "Add entries" in the app + And I press "Save" near "Number DB" in the app + Then I should find "You did not fill out any fields!" in the app + + When I press "OK" in the app + And I set the following fields to these values in the app: + | Number | 0 | + And I press "Save" near "Number DB" in the app + Then I should find "0" near "Number:" in the app + But I should not find "Save" in the app