Skip to content

Commit 38bcce2

Browse files
committed
mgr/dashboard: fix clone async validators with different groups
Providing a way to dynamically update the async validator based on the selector field so that when the selected value changes, the depended field like the clone name gets validated again against the new value Fixes: https://tracker.ceph.com/issues/66703 Signed-off-by: Nizamudeen A <[email protected]>
1 parent f0b9411 commit 38bcce2

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-subvolume-snapshots-list/cephfs-subvolume-snapshots-list.component.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ export class CephfsSubvolumeSnapshotsListComponent implements OnInit, OnChanges
270270
this.cephfsSubvolumeService,
271271
null,
272272
null,
273-
this.fsName
273+
this.fsName,
274+
this.activeGroupName
274275
)
275276
],
276277
required: true,
@@ -284,12 +285,23 @@ export class CephfsSubvolumeSnapshotsListComponent implements OnInit, OnChanges
284285
name: 'groupName',
285286
value: this.activeGroupName,
286287
label: $localize`Group name`,
288+
valueChangeListener: true,
289+
dependsOn: 'cloneName',
287290
typeConfig: {
288291
options: allGroups
289292
}
290293
}
291294
],
292295
submitButtonText: $localize`Create Clone`,
296+
updateAsyncValidators: (value: any) =>
297+
CdValidators.unique(
298+
this.cephfsSubvolumeService.exists,
299+
this.cephfsSubvolumeService,
300+
null,
301+
null,
302+
this.fsName,
303+
value
304+
),
293305
onSubmit: (value: any) => {
294306
this.cephfsSubvolumeService
295307
.createSnapshotClone(

src/pybind/mgr/dashboard/frontend/src/app/shared/components/form-modal/form-modal.component.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export class FormModalComponent implements OnInit {
2323
submitButtonText: string;
2424
onSubmit: Function;
2525

26+
updateAsyncValidators?: Function;
27+
2628
// Internal
2729
formGroup: CdFormGroup;
2830

@@ -57,13 +59,22 @@ export class FormModalComponent implements OnInit {
5759
if (field.asyncValidators) {
5860
asyncValidators = asyncValidators.concat(field.asyncValidators);
5961
}
60-
return new UntypedFormControl(
62+
63+
const control = new UntypedFormControl(
6164
_.defaultTo(
6265
field.type === 'binary' ? this.dimlessBinaryPipe.transform(field.value) : field.value,
6366
null
6467
),
6568
{ validators, asyncValidators }
6669
);
70+
71+
if (field.valueChangeListener) {
72+
control.valueChanges.subscribe((value) => {
73+
const validatorToUpdate = this.updateAsyncValidators(value);
74+
this.updateValidation(field.dependsOn, validatorToUpdate);
75+
});
76+
}
77+
return control;
6778
}
6879

6980
getError(field: CdFormModalFieldConfig): string {
@@ -114,4 +125,10 @@ export class FormModalComponent implements OnInit {
114125
this.onSubmit(values);
115126
}
116127
}
128+
129+
updateValidation(name?: string, validator?: AsyncValidatorFn[]) {
130+
const field = this.formGroup.get(name);
131+
field.setAsyncValidators(validator);
132+
field.updateValueAndValidity();
133+
}
117134
}

src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-form-modal-field-config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export class CdFormModalFieldConfig {
1313
validators: ValidatorFn[];
1414
asyncValidators?: AsyncValidatorFn[];
1515

16+
// Used when you want to dynamically update the
17+
// async validators based on the field value
18+
valueChangeListener?: boolean;
19+
dependsOn?: string;
20+
1621
// --- Specific field properties ---
1722
typeConfig?: {
1823
[prop: string]: any;

0 commit comments

Comments
 (0)