javascript - Why is my Angular FormGroup Valid when a control that is marked as required is empty? - Stack Overflow

I am trying to setup a basic form validation for a select.Inmy constructor I have the following Form

I am trying to setup a basic form validation for a select.

In my constructor I have the following FormGroup

this.formSubmission = new FormGroup(
  {
    triggers: new FormControl(['', [ Validators.required]])
  }
)

I have a button on the screen to test the validity of the FormGroup and it always es back as valid even when there is nothing selected for "triggers".

When clicked the following code executes:

console.log('FormGroup Status: ' + this.formSubmission.status)

This will return VALID.

The HTML portion of this can be found here:

<div [formGroup]="formSubmission">
  <mat-form-field appearance="fill">
    <mat-label>Triggers</mat-label>
    <mat-select 
    formControlName="triggers" multiple
    >
      <mat-option *ngFor="let trigger of triggersList" [value]="trigger.TRIGGER_NAME">{{trigger.TRIGGER_NAME}}</mat-option>
    </mat-select>
  </mat-form-field>
</div>

I am trying to setup a basic form validation for a select.

In my constructor I have the following FormGroup

this.formSubmission = new FormGroup(
  {
    triggers: new FormControl(['', [ Validators.required]])
  }
)

I have a button on the screen to test the validity of the FormGroup and it always es back as valid even when there is nothing selected for "triggers".

When clicked the following code executes:

console.log('FormGroup Status: ' + this.formSubmission.status)

This will return VALID.

The HTML portion of this can be found here:

<div [formGroup]="formSubmission">
  <mat-form-field appearance="fill">
    <mat-label>Triggers</mat-label>
    <mat-select 
    formControlName="triggers" multiple
    >
      <mat-option *ngFor="let trigger of triggersList" [value]="trigger.TRIGGER_NAME">{{trigger.TRIGGER_NAME}}</mat-option>
    </mat-select>
  </mat-form-field>
</div>
Share Improve this question edited Apr 29, 2022 at 15:29 Anurag Srivastava 14.5k4 gold badges37 silver badges46 bronze badges asked Apr 29, 2022 at 15:09 user68288user68288 7842 gold badges16 silver badges41 bronze badges 1
  • what is the result of console.log('FormGroup value: ' + this.formSubmission.value) ? – Seega Commented Apr 29, 2022 at 15:18
Add a ment  | 

1 Answer 1

Reset to default 7

You defined triggers wrong.

  triggers: new FormControl(['', [ Validators.required]])

will result in triggers: array. First argument is the value, second is the validators, asyncValidators or options

what you probably meant to do is this:

triggers: new FormControl('', [ Validators.required])

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745438285a4627711.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信