mn-angular-lib 1.0.25 → 1.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4393,6 +4393,8 @@ class MnFormBodyComponent {
4393
4393
  formErrors = {};
4394
4394
  /** Track which fields are currently visible (for conditional fields) */
4395
4395
  fieldVisibility = {};
4396
+ /** Track which fields are currently conditionally required */
4397
+ fieldConditionallyRequired = {};
4396
4398
  /** Track loading state per field for async data sources */
4397
4399
  fieldLoading = {};
4398
4400
  /** Dynamic options loaded from data sources */
@@ -4411,6 +4413,12 @@ class MnFormBodyComponent {
4411
4413
  return val;
4412
4414
  }
4413
4415
  hasRequiredValidator(field) {
4416
+ // Check if conditionallyRequired is currently active
4417
+ if (field.conditionallyRequired) {
4418
+ const formValue = this.form?.value ?? {};
4419
+ if (field.conditionallyRequired(formValue))
4420
+ return true;
4421
+ }
4414
4422
  const validators = field.validators;
4415
4423
  if (!validators)
4416
4424
  return false;
@@ -4635,10 +4643,11 @@ class MnFormBodyComponent {
4635
4643
  // Feature 1: Conditional/Dynamic Fields
4636
4644
  // =========================
4637
4645
  initializeVisibility() {
4646
+ const formValue = this.form.value;
4638
4647
  this.config.fields.forEach(field => {
4639
4648
  const key = field.key;
4640
4649
  const fieldAny = field;
4641
- const isVisible = fieldAny.visible ? fieldAny.visible(this.form.value) : true;
4650
+ const isVisible = fieldAny.visible ? fieldAny.visible(formValue) : true;
4642
4651
  this.fieldVisibility[key] = isVisible;
4643
4652
  // If initially hidden, clear validators so they don't block submit
4644
4653
  if (!isVisible) {
@@ -4648,6 +4657,19 @@ class MnFormBodyComponent {
4648
4657
  control.updateValueAndValidity({ emitEvent: false });
4649
4658
  }
4650
4659
  }
4660
+ else if (fieldAny.conditionallyRequired) {
4661
+ // Initialize conditionallyRequired state for visible fields
4662
+ const isRequired = fieldAny.conditionallyRequired(formValue);
4663
+ this.fieldConditionallyRequired[key] = isRequired;
4664
+ if (isRequired) {
4665
+ const control = this.form.get(key);
4666
+ if (control) {
4667
+ const validators = this.buildValidators(fieldAny, formValue);
4668
+ control.setValidators(validators);
4669
+ control.updateValueAndValidity({ emitEvent: false });
4670
+ }
4671
+ }
4672
+ }
4651
4673
  });
4652
4674
  }
4653
4675
  updateVisibility() {
@@ -4666,14 +4688,54 @@ class MnFormBodyComponent {
4666
4688
  control.updateValueAndValidity({ emitEvent: false });
4667
4689
  }
4668
4690
  else if (isVisible && !wasVisible) {
4669
- // Restore validators
4670
- const validators = fieldAny.validators || [];
4691
+ // Restore validators (including conditionallyRequired if active)
4692
+ const validators = this.buildValidators(fieldAny, formValue);
4671
4693
  control.setValidators(validators);
4672
4694
  control.updateValueAndValidity({ emitEvent: false });
4673
4695
  }
4696
+ else if (isVisible) {
4697
+ // Update conditionallyRequired for visible fields
4698
+ this.updateConditionallyRequired(fieldAny, formValue);
4699
+ }
4674
4700
  }
4675
4701
  });
4676
4702
  }
4703
+ /**
4704
+ * Builds the full validator array for a field, including conditionallyRequired.
4705
+ * @param fieldAny The field configuration.
4706
+ * @param formValue The current form values.
4707
+ * @returns Array of validators to apply.
4708
+ */
4709
+ buildValidators(fieldAny, formValue) {
4710
+ const baseValidators = fieldAny.validators ? [...fieldAny.validators] : [];
4711
+ if (fieldAny.conditionallyRequired && fieldAny.conditionallyRequired(formValue)) {
4712
+ if (!baseValidators.includes(Validators.required)) {
4713
+ baseValidators.push(Validators.required);
4714
+ }
4715
+ }
4716
+ return baseValidators;
4717
+ }
4718
+ /**
4719
+ * Updates the conditionallyRequired state for a single field and adjusts validators.
4720
+ * @param fieldAny The field configuration.
4721
+ * @param formValue The current form values.
4722
+ */
4723
+ updateConditionallyRequired(fieldAny, formValue) {
4724
+ if (!fieldAny.conditionallyRequired)
4725
+ return;
4726
+ const key = fieldAny.key;
4727
+ const wasRequired = this.fieldConditionallyRequired[key] ?? false;
4728
+ const isRequired = fieldAny.conditionallyRequired(formValue);
4729
+ this.fieldConditionallyRequired[key] = isRequired;
4730
+ if (isRequired !== wasRequired) {
4731
+ const control = this.form.get(key);
4732
+ if (control) {
4733
+ const validators = this.buildValidators(fieldAny, formValue);
4734
+ control.setValidators(validators);
4735
+ control.updateValueAndValidity({ emitEvent: false });
4736
+ }
4737
+ }
4738
+ }
4677
4739
  isFieldVisible(field) {
4678
4740
  return this.fieldVisibility[field.key];
4679
4741
  }