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(), true);
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(), true);
if (this.option('showValidationSummary')) {
this._validationSummary?.refreshValidationGroup();
}
Expand Down
15 changes: 8 additions & 7 deletions packages/devextreme/js/__internal/ui/m_validation_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ const GroupConfig = Class.inherit({
ctor(group) {
this.group = group;
this.validators = [];
this.isRemovable = false;
this._pendingValidators = [];
this._onValidatorStatusChanged = this._onValidatorStatusChanged.bind(this);
this._resetValidationInfo();
Expand Down Expand Up @@ -562,10 +563,11 @@ 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.isRemovable = isRemovable;
ksercs marked this conversation as resolved.
Show resolved Hide resolved
ksercs marked this conversation as resolved.
Show resolved Hide resolved
this.groups.push(config);
}
return config;
Expand Down Expand Up @@ -778,22 +780,21 @@ const ValidationEngine = {
},

registerValidatorInGroup(group, validator) {
const groupConfig = ValidationEngine.addGroup(group);
const groupConfig = ValidationEngine.addGroup(group, false);
groupConfig.registerValidator.call(groupConfig, validator);
},

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

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 { validators, isRemovable } = config;
if (this._shouldRemoveGroup(group, validators, 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 @@ -475,6 +475,62 @@ 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('validation group should not be removed after its validators are removed (T1233487)', function(assert) {
ksercs marked this conversation as resolved.
Show resolved Hide resolved
const $formContainer = $('#form').dxForm({
validationGroup: 'Test',
formData: {
firstName: 'Kyle',
},
items: [{
itemType: 'group',
items: [{
dataField: 'firstName',
validationRules: [{ type: 'required' }]
}],
}],
});
const form = $formContainer.dxForm('instance');

let $validator = $formContainer.find(`.${VALIDATOR_CLASS}`);
const validator = $validator.dxValidator('instance');

assert.equal(validator.option('validationGroup'), 'Test', 'validation group of the validator');
assert.equal($validator.length, 1, 'validators count');

form.option('items[0].items[0]', 'validationRules', []);
$validator = $formContainer.find(`.${VALIDATOR_CLASS}`);

assert.equal(validator.option('validationGroup'), 'Test', 'validation group should not be removed');
assert.equal($validator.length, 0, 'validators count');
});

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