Skip to content

Commit 6373010

Browse files
authored
Merge pull request #3527 from dpalou/MOBILE-4234
MOBILE-4234 mod_data: Correctly handle 0 in number fields
2 parents 6cc23c0 + 18f9e90 commit 6373010

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

src/addons/mod/data/fields/number/services/handler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ export class AddonModDataFieldNumberHandlerService extends AddonModDataFieldText
4545
originalFieldData: AddonModDataEntryField,
4646
): boolean {
4747
const fieldName = 'f_' + field.id;
48-
const input = inputData[fieldName] || '';
49-
const content = originalFieldData?.content || '';
48+
const input = inputData[fieldName] ?? '';
49+
const content = originalFieldData?.content ?? '';
5050

51-
return input != content;
51+
return input !== content;
5252
}
5353

5454
/**
5555
* @inheritdoc
5656
*/
5757
getFieldsNotifications(field: AddonModDataField, inputData: AddonModDataSubfieldData[]): string | undefined {
58-
if (field.required && (!inputData || !inputData.length || inputData[0].value == '')) {
58+
if (field.required && (!inputData || !inputData.length || inputData[0].value === '')) {
5959
return Translate.instant('addon.mod_data.errormustsupplyvalue');
6060
}
6161
}

src/addons/mod/data/fields/text/services/handler.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class AddonModDataFieldTextHandlerService implements AddonModDataFieldHan
7070

7171
return [{
7272
fieldid: field.id,
73-
value: inputData[fieldName] || '',
73+
value: inputData[fieldName] ?? '',
7474
}];
7575
}
7676

@@ -83,10 +83,10 @@ export class AddonModDataFieldTextHandlerService implements AddonModDataFieldHan
8383
originalFieldData: AddonModDataEntryField,
8484
): boolean {
8585
const fieldName = 'f_' + field.id;
86-
const input = inputData[fieldName] || '';
87-
const content = originalFieldData?.content || '';
86+
const input = inputData[fieldName] ?? '';
87+
const content = originalFieldData?.content ?? '';
8888

89-
return input != content;
89+
return input !== content;
9090
}
9191

9292
/**
@@ -102,7 +102,7 @@ export class AddonModDataFieldTextHandlerService implements AddonModDataFieldHan
102102
* @inheritdoc
103103
*/
104104
overrideData(originalContent: AddonModDataEntryField, offlineContent: CoreFormFields<string>): AddonModDataEntryField {
105-
originalContent.content = offlineContent[''] || '';
105+
originalContent.content = offlineContent[''] ?? '';
106106

107107
return originalContent;
108108
}

src/addons/mod/data/pages/edit/edit.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
import { AddonModDataHelper } from '../../services/data-helper';
4343
import { CoreDom } from '@singletons/dom';
4444
import { AddonModDataEntryFieldInitialized } from '../../classes/base-field-plugin-component';
45+
import { CoreTextUtils } from '@services/utils/text';
4546

4647
/**
4748
* Page that displays the view edit page.
@@ -368,9 +369,18 @@ export class AddonModDataEditPage implements OnInit {
368369
}
369370
});
370371
}
372+
371373
this.jsData!.errors = this.errors;
372374

373375
this.scrollToFirstError();
376+
377+
if (updateEntryResult.generalnotifications?.length) {
378+
CoreDomUtils.showAlertWithOptions({
379+
header: Translate.instant('core.notice'),
380+
message: CoreTextUtils.buildMessage(updateEntryResult.generalnotifications),
381+
buttons: [Translate.instant('core.ok')],
382+
});
383+
}
374384
}
375385
} finally {
376386
modal.dismiss();

src/addons/mod/data/services/data-helper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,8 @@ export class AddonModDataHelperProvider {
590590
// WS wants values in JSON format.
591591
entryFieldDataToSend.push({
592592
fieldid: fieldSubdata.fieldid,
593-
subfield: fieldSubdata.subfield || '',
594-
value: value ? JSON.stringify(value) : '',
593+
subfield: fieldSubdata.subfield ?? '',
594+
value: (value || value === 0) ? JSON.stringify(value) : '',
595595
});
596596

597597
return;

src/addons/mod/data/tests/behat/entries.feature

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,22 @@ Feature: Users can manage entries in database activities
206206
Then I should find "Are you sure you want to delete this entry?" in the app
207207
And I press "Delete" in the app
208208
And I should not find "Moodle Cloud" in the app
209+
210+
Scenario: Handle number 0 correctly when creating entries
211+
Given the following "activities" exist:
212+
| activity | name | intro | course | idnumber |
213+
| data | Number DB | Number DB | C1 | data2 |
214+
And the following "mod_data > fields" exist:
215+
| database | type | name | description |
216+
| data2 | number | Number | Number value |
217+
And I entered the data activity "Number DB" on course "Course 1" as "student1" in the app
218+
When I press "Add entries" in the app
219+
And I press "Save" near "Number DB" in the app
220+
Then I should find "You did not fill out any fields!" in the app
221+
222+
When I press "OK" in the app
223+
And I set the following fields to these values in the app:
224+
| Number | 0 |
225+
And I press "Save" near "Number DB" in the app
226+
Then I should find "0" near "Number:" in the app
227+
But I should not find "Save" in the app

0 commit comments

Comments
 (0)