anakin59490
anakin59490

Reputation: 672

Angular 7 - enable submit form with FormControl field

My FormGroup has 3 fields: 2 text field et 1 FormControl.

All fields are required and FormControl has one specific Validator :

generateForm(scanfolder) {
console.log('generateForm');
this.origineControl.setValidators(this.forbiddenNamesValidator(this.options))
const fb: FormBuilder = new FormBuilder();
this.scanfolderForm = fb.group({
  'fullpath': [scanfolder.fullpath, [Validators.required]],
  'name': [scanfolder.name, [Validators.required]],
  'origin': [this.origineControl, [Validators.required]],
});
}

Specific Control :

forbiddenNamesValidator(names: string[]): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } | null => {
  console.log(typeof control );
  console.log(control.value );
  // below findIndex will check if control.value is equal to one of our options or not
  const index = names.findIndex(name => {
    return (new RegExp('\^' + name + '\$')).test(control.value);
  });
  return index < 0 ? { 'forbiddenNames': { value: control.value } } : null;
};
}

When the fields are populated, submit form button must be enable but it doesn't work :

before enter image description here

after: enter image description here

We can see button is available whereas 2nd fiels has error

Here is my HTML :

          <form [formGroup]="scanfolderForm">
        <div class="row">
          <mat-form-field class="col-md-4">
            <input matInput placeholder="Chemin complet"  formControlName="fullpath" name="fullpath">
            <mat-error *ngIf="scanfolderForm.controls['fullpath'].errors?.incorrectName">Saisie incorrecte</mat-error>
          </mat-form-field>
          <mat-form-field class="col-md-4">
            <input type="text" placeholder="Pick one" aria-label="Number" matInput [formControl]="origineControl" [matAutocomplete]="auto" required>
            <mat-autocomplete #auto="matAutocomplete">
              <mat-option *ngFor="let option of options" [value]="option">
                {{option}}
              </mat-option>
            </mat-autocomplete>
            <mat-error *ngIf="origineControl.hasError('forbiddenNames')">
              You should enter value from suggested one only. <strong>'{{origineControl.errors.forbiddenNames.value}}'</strong> is not allowed.
            </mat-error>
            <mat-error *ngIf="origineControl.hasError('required')">
              This is <strong>required</strong>
            </mat-error>
          </mat-form-field>
          <mat-form-field class="col-md-4">
          <input matInput placeholder="Nom" formControlName="name" name="name">
          </mat-form-field>
        </div>
     </form>
    

<mat-dialog-actions *ngIf="!loading">
 <button class="btn btn-primary" *ngIf="action === 2 && !isDisabled" (click)="cuScanfolder()" 
  [disabled]="!scanfolderForm.valid">M.A.J</button>
  <button class="btn btn-warning" *ngIf="data.list.length < 2" (click)="nextOne()" cdkFocusInitial>Retour</button>
</mat-dialog-actions>

Upvotes: 0

Views: 312

Answers (1)

ttquang1063750
ttquang1063750

Reputation: 863

from official

When you add or remove a validator at run time, you must call updateValueAndValidity() for the new validation to take effect.

this.origineControl.setValidators([this.forbiddenNamesValidator(this.options), Validators.required]);
const fb: FormBuilder = new FormBuilder();
this.scanfolderForm = fb.group({
  'fullpath': [scanfolder.fullpath, [Validators.required]],
  'name': [scanfolder.name, [Validators.required]]
});
this.scanfolderForm.addControl('origin', this.origineControl)
this.scanfolderForm.updateValueAndValidity();
<mat-form-field class="col-md-4">
  <input type="text" placeholder="Pick one" aria-label="Number" matInput formControlName="origin" [matAutocomplete]="auto" required>
  <mat-autocomplete #auto="matAutocomplete">
    <mat-option *ngFor="let option of options" [value]="option">
      {{option}}
    </mat-option>
  </mat-autocomplete>
  <mat-error *ngIf="scanfolderForm.get('origin').hasError('forbiddenNames')">
    You should enter value from suggested one only. <strong>'{{scanfolderForm.get('origin').errors.forbiddenNames.value}}'</strong> is not allowed.
  </mat-error>
  <mat-error *ngIf="scanfolderForm.get('origin').hasError('required')">
    This is <strong>required</strong>
  </mat-error>
</mat-form-field>

Upvotes: 0

Related Questions