MOBILE-4234 mod_data: Correctly handle 0 in number fields

main
Dani Palou 2023-01-20 08:10:03 +01:00
parent a7b5091baa
commit 18f9e90fbc
5 changed files with 40 additions and 11 deletions

View File

@ -45,17 +45,17 @@ export class AddonModDataFieldNumberHandlerService extends AddonModDataFieldText
originalFieldData: AddonModDataEntryField, originalFieldData: AddonModDataEntryField,
): boolean { ): boolean {
const fieldName = 'f_' + field.id; const fieldName = 'f_' + field.id;
const input = inputData[fieldName] || ''; const input = inputData[fieldName] ?? '';
const content = originalFieldData?.content || ''; const content = originalFieldData?.content ?? '';
return input != content; return input !== content;
} }
/** /**
* @inheritdoc * @inheritdoc
*/ */
getFieldsNotifications(field: AddonModDataField, inputData: AddonModDataSubfieldData[]): string | undefined { 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'); return Translate.instant('addon.mod_data.errormustsupplyvalue');
} }
} }

View File

@ -70,7 +70,7 @@ export class AddonModDataFieldTextHandlerService implements AddonModDataFieldHan
return [{ return [{
fieldid: field.id, fieldid: field.id,
value: inputData[fieldName] || '', value: inputData[fieldName] ?? '',
}]; }];
} }
@ -83,10 +83,10 @@ export class AddonModDataFieldTextHandlerService implements AddonModDataFieldHan
originalFieldData: AddonModDataEntryField, originalFieldData: AddonModDataEntryField,
): boolean { ): boolean {
const fieldName = 'f_' + field.id; const fieldName = 'f_' + field.id;
const input = inputData[fieldName] || ''; const input = inputData[fieldName] ?? '';
const content = originalFieldData?.content || ''; const content = originalFieldData?.content ?? '';
return input != content; return input !== content;
} }
/** /**
@ -102,7 +102,7 @@ export class AddonModDataFieldTextHandlerService implements AddonModDataFieldHan
* @inheritdoc * @inheritdoc
*/ */
overrideData(originalContent: AddonModDataEntryField, offlineContent: CoreFormFields<string>): AddonModDataEntryField { overrideData(originalContent: AddonModDataEntryField, offlineContent: CoreFormFields<string>): AddonModDataEntryField {
originalContent.content = offlineContent[''] || ''; originalContent.content = offlineContent[''] ?? '';
return originalContent; return originalContent;
} }

View File

@ -42,6 +42,7 @@ import {
import { AddonModDataHelper } from '../../services/data-helper'; import { AddonModDataHelper } from '../../services/data-helper';
import { CoreDom } from '@singletons/dom'; import { CoreDom } from '@singletons/dom';
import { AddonModDataEntryFieldInitialized } from '../../classes/base-field-plugin-component'; import { AddonModDataEntryFieldInitialized } from '../../classes/base-field-plugin-component';
import { CoreTextUtils } from '@services/utils/text';
/** /**
* Page that displays the view edit page. * Page that displays the view edit page.
@ -368,9 +369,18 @@ export class AddonModDataEditPage implements OnInit {
} }
}); });
} }
this.jsData!.errors = this.errors; this.jsData!.errors = this.errors;
this.scrollToFirstError(); this.scrollToFirstError();
if (updateEntryResult.generalnotifications?.length) {
CoreDomUtils.showAlertWithOptions({
header: Translate.instant('core.notice'),
message: CoreTextUtils.buildMessage(updateEntryResult.generalnotifications),
buttons: [Translate.instant('core.ok')],
});
}
} }
} finally { } finally {
modal.dismiss(); modal.dismiss();

View File

@ -590,8 +590,8 @@ export class AddonModDataHelperProvider {
// WS wants values in JSON format. // WS wants values in JSON format.
entryFieldDataToSend.push({ entryFieldDataToSend.push({
fieldid: fieldSubdata.fieldid, fieldid: fieldSubdata.fieldid,
subfield: fieldSubdata.subfield || '', subfield: fieldSubdata.subfield ?? '',
value: value ? JSON.stringify(value) : '', value: (value || value === 0) ? JSON.stringify(value) : '',
}); });
return; return;

View File

@ -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 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 press "Delete" in the app
And I should not find "Moodle Cloud" 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