【问题标题】:Angular dropdown validation when the default value is set to '0'默认值设置为“0”时的角度下拉验证
【发布时间】:2021-08-05 20:37:42
【问题描述】:

我已将“选择”的默认值设置为下拉菜单的“0”。

验证规则

1.根据需要选择下拉菜单。(至少从下拉菜单中选择一个选项)。

2.如果没有选择某个值,则显示错误提示“您需要选择一个选项”

我尝试过的事情:

案例一:

HTML:

<div class="col-12 col-md-6 col-lg-4">
            <div class="form-group">
              <span class="plain-select">
                <select class="form-control" id="select101" formControlName="UserId"
                  [(ngModel)]="editFormData.userId">
                  *****<option value='0' selected>Select</option>*****
                  <option *ngFor="let user of usernames" value={{user.userId}}>
                    {{user.usernames}}
                  </option>
                </select>
              </span>
              <div
                *ngIf="addUserForm.controls['UserId'].invalid && (submited || ` addUserForm.controls['UserId'].dirty || addUserForm.controls['UserId'].touched)"`
                class="error-msg">
                <div *ngIf="addUserForm.controls['UserId'].errors.required">
                  UserId is required.
                </div>
              </div>
            </div>
          </div>
      
      
      TypeScript:
      
        UserId:  ['', [Validators.required, Validators.min(1)]],
        
        

**以上情况:

        I am able to see the default as "Select" but the validation doesn't work.

当没有从下拉列表中选择任何选项时,它不会给出任何验证错误。

案例 2:

 <div class="col-12 col-md-6 col-lg-4">
            <div class="form-group">
              <span class="plain-select">
                <select class="form-control" id="select101" formControlName="UserId"
                  [(ngModel)]="editFormData.UserId">
                  <option value='null' selected>Select</option>
                  <option *ngFor="let user of usernames" value={{user.userId}}>
                    {{user.usernames}}
                  </option>
                </select>
              </span>
              <div
                *ngIf="addUserForm.controls['UserId'].invalid && (submited || addUserForm.controls['UserId'].dirty || addUserForm.controls['UserId'].touched)"
                class="error-msg">
                <div *ngIf="addUserForm.controls['UserId'].errors.required">
                  UserId is required.
                </div>
              </div>
            </div>
          </div>
          
      
        TypeScript:
      
      

  UserId:  ['', [Validators.required, Validators.min(1)]],
        
        In the above case, the validation is working as expected but however the default value is not set to "Select" and also the validation works only if "Select" option is selected from the dropdown
        
        Case-3:
        
        Same happens when i select the value to "undefined".

【问题讨论】:

    标签: html angular validation dropdown angular-ng-if


    【解决方案1】:

    使其工作的一种可能方法是使用ngValue 进行数据绑定

      <option [ngValue]="null" selected>Select</option>
      <option *ngFor="let user of usernames" [ngValue]="user.userId">
        {{user.username}} - {{user.userId}}
      </option>
    

    显示验证错误(注意?.optional chaining):

      <div *ngIf="frm.controls['UserId'].dirty && frm.controls['UserId']?.errors?.required">
        UserId is required.
      </div>
    

    Demo

    【讨论】:

    • 您好,感谢您的回复。然而,就我而言,我不能使用 [ngValue] = "null" 因为我的 userId 不接受空值。如果在我的情况下使用 [ngValue] = "null",则不会呈现“选择”值。下拉菜单默认值为空。但是,当我从下拉列表中选择“选择”选项时,验证有效。 2. 如果绑定 [ngValue] = 0,则加载默认选项,但验证不起作用。如果我们把上面Demo中的[ngValue] = "null"替换成[ngValue]=0。当我将 [ngValue] 与“null”绑定时,确切的情况正在发生。
    • 试图实现这样的东西,但在我的情况下它不起作用stackoverflow.com/questions/56037515/…
    • @rutuja 请创建一个 Stackblitz
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多