Angular - Uniqueness Validator in FormArray

I created a special validator to verify uniqueness in FormArray. I want to show an error when specific values ​​are already in the array.

The problem is that it does not work properly.

Actual behavior:

Steps to play:

  • Add 3 "inputs" - address;
  • Fill in the input 1;
  • Fill in input 2 with a different value;
  • Fill input 3 with the same input 1 value; (errors do not appear at input 1 or input 3)

Expected Behavior :

If the same values ​​are displayed in "X-groups", their specific inputs should indicate an error.

In the case described above, errors should appear on inputs 1 and 3.

Suppose I have 4 inputs:

  • value: stack
  • value: overflow
  • value: stack
  • value: overflow

4 , .


static uniqueBy = (field: string, caseSensitive = true): ValidatorFn => {
  return (formArray: FormArray): { [key: string]: boolean } => {
    const controls = formArray.controls.filter(formGroup => {
      return isPresent(formGroup.get(field).value);
    });
    const uniqueObj = { uniqueBy: true };
    let found = false;

    if (controls.length > 1) {
      for (let i = 0; i < controls.length; i++) {
        const formGroup = controls[i];
        const mainControl = formGroup.get(field);
        const val = mainControl.value;    
        const mainValue = caseSensitive ? val.toLowerCase() :  val;

        controls.forEach((group, index) => {
          if (i === index) {
            // Same group
            return;
          }

          const currControl = group.get(field);
          const tempValue = currControl.value;
          const currValue = caseSensitive ? tempValue.toLowerCase() : tempValue;
          let newErrors;

          if ( mainValue === currValue) {
            if (isBlank(currControl.errors)) {
              newErrors = uniqueObj;
            } else {
              newErrors = Object.assign(currControl.errors, uniqueObj);
            }

            found = true;
          } else {
            newErrors = currControl.errors;

            if (isPresent(newErrors)) {
              // delete uniqueBy error
              delete newErrors['uniqueBy'];

              if (isBlank(newErrors)) {
                // {} to undefined/null
                newErrors = null;
              }
            }
          }

          // Add specific errors based on condition
          currControl.setErrors(newErrors);
        });
      }

      if (found) {
        // Set errors to whole formArray
        return uniqueObj;
      }
    }

    // Clean errors
    return null;
  };
}

DEMO

? , .

.

+4
2

, .

:

  0      [null, "{"uniqueBy":true}", null]

  1      ["{"uniqueBy":true}", "{"uniqueBy":true}", null]

  2      [null, "{}", null]

http://plnkr.co/edit/MTjzQ9KiJHJ56DVAZ155?p=preview ( )

.

controls.map(formGroup => formGroup.get(field)).forEach(x => x.errors && delete x.errors['uniqueBy']);
for (let i: number = 0; i < controls.length; i++) {
    const formGroup: FormGroup = controls[i] as FormGroup;
    const mainControl: AbstractControl = formGroup.get(field);
    const val: string = mainControl.value;

    const mainValue: string = caseSensitive ? val.toLowerCase() :  val;
    controls.forEach((group: FormGroup, index: number) => {
        if (i === index) {
            return;
        }

        const currControl: any = group.get(field);
        const tempValue: string = currControl.value;
        const currValue: string = caseSensitive ? tempValue.toLowerCase() : tempValue;
        let newErrors: any;

        if ( mainValue === currValue) {
            if (isBlank(currControl.errors)) {
                newErrors = uniqueObj;
            } else {
                newErrors = Object.assign(currControl.errors, uniqueObj);
            }
            currControl.setErrors(newErrors);
            find = true;
        }
    });
}

+7

, . yurzui plunker, . yurzui: 1 ° 2 ° 'x' 3 ° 'x' 4 ° , 'x'

- , "" FormControl , "{}" . , dev_054 , , : http://plnkr.co/edit/ejx8R3HKbu2zXRw41oKc?p=preview

:

controls.map(formGroup => formGroup.get(field)).forEach(x => x.errors && delete x.errors['uniqueBy']);

:

    controls.map(formGroup => formGroup.get(field)).forEach(x => {
      if(x.errors){
        delete x.errors['unique-by'];
        if(isBlank(x.errors)){
          x.setErrors(null);
        }
      }
    });

caseSensitive. caseSentive , 'street' 'STREET', . , caseSentive , . , .

dev_054 yurzui, , , .

+1

Source: https://habr.com/ru/post/1675474/


All Articles