Eur*_*e01 7 html typescript angular2-forms angular
我正在尝试检查电子邮件字段和确认电子邮件字段是否相互匹配.也就是说,用户输入他们的电子邮件然后他们必须再次确认.我希望匹配/验证在模糊时发生(当用户按下回车或文本字段丢失焦点时).
这是我的ts文件:
import {Component, OnInit} from '@angular/core';
import {User} from './user.interface';
import {FormBuilder, FormGroup, ValidatorFn} from '@angular/forms';
@Component({
selector: 'my-email',
templateUrl: '/app/components/profile/email.component.html',
styleUrls:['styles.css'],
})
export class EmailComponent implements OnInit {
public user : User;
Form : FormGroup;
ngOnInit() {
// initialize model here
this.user = {
Email: '',
confirmEmail: ''
}
if(this.Form.valid) {
this.displayErrors = false;
}
}
constructor(fb: FormBuilder, private cookieService: CookieService, private router: Router) {
this.Form = fb.group({
email: [''],
confirmEmail: ['']
},
{
validator: this.matchingEmailsValidator('email', 'confirmEmail')
});
}
save(model: User, isValid: boolean) {
// call API to save customer
//save email
}
matchingEmailsValidator(emailKey: string, confirmEmailKey: string): ValidatorFn {
return (group: FormGroup): {[key: string]: any} => {
let email = group.controls[emailKey];
let confirmEmail = group.controls[confirmEmailKey];
if (email.value !== confirmEmail.value) {
return {
mismatch: true
};
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的观点:
<form [formGroup]="Form" novalidate (ngSubmit)="Form.valid && save(Form.value, Form.valid)">
<div class="container-fluid">
<div id = "container" class="contain" style="text-align: center">
<div>
<fieldset class="form-group">
<label id = "rounded" class="item item-input .col-md-6 .col-md-offset-3">
<input class="email-address-entry form-control" name="email" type="email" placeholder="name@domain.com" formControlName="email" pattern="^(\\w|[0-9.!#$%&’*+/=?^_\`{|}~-])+@(\\w|[0-9-])+(?:??[.](\\w|[0-9-])+)*$"/>
</label>
<p class="Reenter-your-email">Reenter your email to confirm</p>
<label id = "rounded" class="item item-input">
<input class="email-address-entry form-control" (blur)="displayErrors=true" name="confirmEmail" type="email" placeholder="name@domain.com" formControlName="confirmEmail" validateEqual="email"/>
</label>
</fieldset>
</div>
<div>
<label class="entry-invalid">
<p *ngIf="displayErrors && !Form.get('email').valid">The email you entered does not match.</p>
</label>
</div>
<div (click)="Form.get('email').length > 0 ? save(Form.value, Form.valid) : NaN" class="{{ Form.get('email').length > 0 ? 'container-fluid anchorBottomHighlight' : 'container-fluid anchorBottom'}}">
<label class="footerLabel">Confirm</label>
</div>
</div>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
目前,通过它的设置方式,验证发生但在输入正确的匹配时不会被清除.我想知道如何正确设置我的视图?因此,如果设置了正确的匹配,则显示/隐藏验证消息.
它似乎Form.get('email').length > 0 永远不会大于0 /永不击中,所以我的标签不会切换为可点击.
我正在使用Angular 2和反应形式.
看起来您正在混合两种表单语法:template-driven forms和model-driven forms。
由于您在类中声明了一个表单模型FormBuilder,我假设您想要一个模型驱动的表单。
这意味着您的字段不需要像[(ngModel)]或 之类的属性#EmailAddress。
相反:
<input type="email" [(ngModel)]="user.EmailAddress" required #EmailAddress="ngModel">
Run Code Online (Sandbox Code Playgroud)
写下这个:
<!-- Now I'm using `formControlName` to bind the field to the model -->
<!-- Its value must match one of the names you used in the FormBuilder -->
<input type="email" formControlName="email">
Run Code Online (Sandbox Code Playgroud)
所有验证器都必须在 FormBuilder 中声明。不仅如此matchingEmailsValidator,而且required:
this.Form = fb.group({
email: ['', Validators.required],
confirmEmail: ['', Validators.required]
},
{
validator: this.matchingEmailsValidator('email', 'confirmEmail')
});
Run Code Online (Sandbox Code Playgroud)
现在您可以使用以下语法访问字段:
// In the class
this.Form.get('email').value
this.Form.get('email').errors
Run Code Online (Sandbox Code Playgroud)
<!-- In the template -->
{{ Form.get('email').value }}
{{ Form.get('email').errors }}
Run Code Online (Sandbox Code Playgroud)
您可以使用这些语法来显示错误。例如:
<input type="email" formControlName="email">
<p *ngIf="Form.get('email').dirty && Form.get('email').errors.required">
This field is required.
</p>
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,如果该email字段已被触摸(即用户尝试输入某些内容)并且required存在错误,我将显示一条错误消息。
您还可以通过使用浏览器的开发工具检查表单的标记来验证您的验证规则是否得到执行。Angular 应该向具有验证规则的标签添加类似.ng-invalid或 的类。.ng-valid<input>
最后,关于您在模糊上检查电子邮件匹配的问题。你不能推迟 Angular 的验证,它会实时发生(当用户输入时)。但您可以等待模糊事件显示错误。
将最后的建议与我之前的示例相结合,如果电子邮件字段为空并且失去焦点(模糊事件),您可能会出现错误:
<input type="email" formControlName="email" (blur)="displayErrors=true">
<p *ngIf="displayErrors && Form.get('email').dirty && Form.get('email').errors.required">
This field is required.
</p>
Run Code Online (Sandbox Code Playgroud)
Euridice 发布此 Plunkr后更新(2017 年 2 月 1 日):
FormBuilder)。进一步来说:
pattern="..."字段中的属性应email替换为Validators.pattern()表单模型中的属性。validateEqual="email"字段中的属性是什么confirmEmail?你没有在任何地方使用它。*ngIf="displayErrors && !Form.get('email').valid && Form.get('email').error.mismatch"。
errors带有“s”,而不是error。mismatch从 检索自定义错误Form.errors.mismatch,而不是Form.get('email').errors.mismatch。这是更新后的工作 Plunkr:https://plnkr.co/edit/dTjcqlm6rZQxA7E0yZLa ?p=preview
| 归档时间: |
|
| 查看次数: |
631 次 |
| 最近记录: |