Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form: Fix form validation error when items are cleared (T1233487) #27435

Merged
merged 17 commits into from
Jun 24, 2024
Merged
4 changes: 2 additions & 2 deletions packages/devextreme/js/__internal/ui/form/m_form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ const Form = Widget.inherit({

_initMarkup() {
// @ts-expect-error
ValidationEngine.addGroup(this._getValidationGroup());
ValidationEngine.addGroup(this._getValidationGroup(), false);
this._clearCachedInstances();
this._prepareFormData();
this.$element().addClass(FORM_CLASS);
Expand Down Expand Up @@ -879,7 +879,7 @@ const Form = Widget.inherit({
const optionName = getOptionNameFromFullName(fullName);
if (ITEM_OPTIONS_FOR_VALIDATION_UPDATING.includes(optionName)) {
// @ts-expect-error
ValidationEngine.addGroup(this._getValidationGroup());
ValidationEngine.addGroup(this._getValidationGroup(), false);
if (this.option('showValidationSummary')) {
this._validationSummary?.refreshValidationGroup();
}
Expand Down
18 changes: 9 additions & 9 deletions packages/devextreme/js/__internal/ui/m_validation_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,10 @@ const rulesValidators = {
};

const GroupConfig = Class.inherit({
ctor(group) {
ctor(group, isRemovable) {
this.group = group;
this.validators = [];
this._isRemovable = isRemovable;
this._pendingValidators = [];
this._onValidatorStatusChanged = this._onValidatorStatusChanged.bind(this);
this._resetValidationInfo();
Expand Down Expand Up @@ -562,10 +563,10 @@ const ValidationEngine = {
this.addGroup();
ksercs marked this conversation as resolved.
Show resolved Hide resolved
},

addGroup(group) {
addGroup(group, isRemovable) {
let config = this.getGroupConfig(group);
if (!config) {
config = new GroupConfig(group);
config = new GroupConfig(group, isRemovable);
this.groups.push(config);
}
return config;
Expand Down Expand Up @@ -778,22 +779,21 @@ const ValidationEngine = {
},

registerValidatorInGroup(group, validator) {
const groupConfig = ValidationEngine.addGroup(group);
const groupConfig = ValidationEngine.addGroup(group, true);
ksercs marked this conversation as resolved.
Show resolved Hide resolved
groupConfig.registerValidator.call(groupConfig, validator);
},

_shouldRemoveGroup(group, validatorsInGroup) {
const isDefaultGroup = group === undefined;
const isValidationGroupInstance = group && group.NAME === 'dxValidationGroup';
return !isDefaultGroup && !isValidationGroupInstance && !validatorsInGroup.length;
_shouldRemoveGroup(validatorsInGroup, isRemovable) {
jdvictoria marked this conversation as resolved.
Show resolved Hide resolved
return !validatorsInGroup.length && isRemovable;
},

removeRegisteredValidator(group, validator) {
const config = ValidationEngine.getGroupConfig(group);
if (config) {
config.removeRegisteredValidator.call(config, validator);
const validatorsInGroup = config.validators;
if (this._shouldRemoveGroup(group, validatorsInGroup)) {
const isRemovable = config._isRemovable;
if (this._shouldRemoveGroup(validatorsInGroup, isRemovable)) {
this.removeGroup(group);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/devextreme/js/__internal/ui/m_validation_group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ValidationGroup extends DOMComponent {
_init() {
// @ts-expect-error
super._init();
ValidationEngine.addGroup(this);
ValidationEngine.addGroup(this, false);
ksercs marked this conversation as resolved.
Show resolved Hide resolved
ksercs marked this conversation as resolved.
Show resolved Hide resolved
}

_initMarkup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const ValidationSummary = CollectionWidget.inherit({
const $element = this.$element();
const group = this.option('validationGroup')
|| ValidationEngine.findGroup($element, this._modelByElement($element));
const groupConfig = ValidationEngine.addGroup(group);
const groupConfig = ValidationEngine.addGroup(group, false);
ksercs marked this conversation as resolved.
Show resolved Hide resolved

this._unsubscribeGroup();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ QUnit.module('General', {
ValidationEngine.removeRegisteredValidator(group, validator2);
});

QUnit.test('group should be validated positively after all validators remove (T1006667)', function(assert) {
QUnit.test('group should be validated positively and should not be removed after all validators are removed (T1006667)', function(assert) {
const $container = $('#dxValidationGroup');
const group = this.fixture.createGroup($container);
const adapter = sinon.createStubInstance(DefaultAdapter);
Expand All @@ -293,7 +293,6 @@ QUnit.module('General', {
});
const validator2 = $validator2.dxValidator('instance');


$validator1.appendTo($container);
$validator2.appendTo($container);
triggerShownEvent($container);
Expand All @@ -305,6 +304,8 @@ QUnit.module('General', {
assert.ok(isValid, 'validation is correct');
});
ValidationEngine.removeRegisteredValidator(group, validator2);

assert.ok(ValidationEngine.getGroupConfig(group), 'group is not removed');
jdvictoria marked this conversation as resolved.
Show resolved Hide resolved
});

QUnit.test('group should be validated positively with a new validator (async)', function(assert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,33 @@ QUnit.test('Changing an validationRules options of an any item does not invalida
assert.strictEqual(renderComponentSpy.callCount, 0, 'renderComponentSpy.callCount');
});

[false, true].forEach(hasValidationGroup => {
QUnit.test(`form ${hasValidationGroup ? 'with' : 'without'} validation group should validate without errors (T1233487)`, function(assert) {
jdvictoria marked this conversation as resolved.
Show resolved Hide resolved
const form = $('#form').dxForm({
validationGroup: hasValidationGroup ? 'test' : undefined,
formData: {
firstName: 'Kyle',
},
items: [{
itemType: 'group',
items: [{
dataField: 'firstName',
validationRules: [{ type: 'required' }]
}],
}],
}).dxForm('instance');

form.option('items[0].items', []);

try {
form.validate();
assert.ok(true);
} catch(e) {
assert.ok(false, 'error is thrown');
}
});
});

QUnit.test('Validate the form without validation rules for an any simple items', function(assert) {
const errorStub = sinon.stub();
logger.error = errorStub;
Expand Down
Loading