场景:用户想要检索电子邮件或重置密码。人们将根据要求选择相应的选项。仅当表单有效时,提交按钮才会启用。
问题:我想根据所选选项动态更新验证。但它不起作用。我肯定错过了一些东西。我使用了clearValidators,但它也不起作用。这是 工作代码。
用于测试使用:电子邮件:test@test.com 密码:Test@1234
我已经知道inputa 中的项目FormControl可以被标记dirt或touched通过调用以下任何方法(也许更多): group.markAsTouched(); form.get('control-name').markAsTouched(); form.markAllAsTouched(); form.controls[someIndex].markAsTouched();
但是,我可以看到该markAsTouched方法似乎在输入focus为 then时被调用blur。
有没有办法通过代码达到相同的结果?比方说,当点击一个按钮时。
在这里,您可以看到当前标准行为的 gif,无需表单,您也可以在以下实时示例中自行测试:
https://stackblitz.com/edit/angular-peq11f
对我来说,很明显这种行为应该可以由代码触发,而不仅仅是在blur事件触发时触发
像这样的东西:
<input #myInput>
<button (click)="myInput.markAsTouched()">click</button>
Run Code Online (Sandbox Code Playgroud) angular2-forms angular2-formbuilder angular angular2-form-validation ignite-ui-angular
我看到有一个主题看起来像我的,但它并没有真正回答我的问题,因为我们没有以相同的方式管理错误。不推荐使用 FormBuilder 组
首先,我刚刚迁移到 Angular 11,现在遇到了这个问题:
group is deprecated: This api is not typesafe and can result in issues with Closure Compiler renaming.
Use the `FormBuilder#group` overload with `AbstractControlOptions` instead.
Run Code Online (Sandbox Code Playgroud)
在此页面中,我会在页面上自动生成许多表单,在日期范围的情况下,我使用两个日期选择器。我创建了一个函数来检查 2 个日期选择器中的值。
成分:
newGroup = this.fb.group(
{
[node.type + '_' + node.objectId + '_dateFrom']: [
'',
[Validators.required]
],
[node.type + '_' + node.objectId + '_dateTo']: [
'',
[Validators.required]
]
},
{
validator: CheckFromToDate(
node.type + '_' + node.objectId + '_dateFrom',
node.type + '_' + node.objectId + '_dateTo'
) …Run Code Online (Sandbox Code Playgroud) angular2-forms angular2-formbuilder angular-validation angular angular2-form-validation
我有一个表单,仅当值不等于 0 时,我才想实现 MAXLENGTH 验证。
那么有parameter.valueMaxlength === 0 { then dont execute maxlength validation }
没有办法把这个逻辑写在html文件中呢。
<mat-form-field *ngSwitchCase="'TEXTBOX'" class="example-full-width">
<input
matInput
[placeholder]="parameter.displayName"
[formControlName]="parameter.id"
[id]="parameter.id"
[type]="parameter.dataType"
[maxlength] = "parameter.valueMaxlength"
/>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud) forms maxlength angular angular-reactive-forms angular2-form-validation