我想显示空白字段的验证,为此我使用了以下方法,但它不起作用。任何人都建议我帮忙。谢谢。
我的模板,
<form [formGroup]="form" (ngSubmit)="onSubmit(form.value)" class="nobottommargin adminloginform" novalidate>
<label class="col-sm-4 text-right norightpadding">First Name</label>
<div class="input-group" [ngClass]="{errmsg: (!form.controls['password'].valid ||
(!form.controls['password'].pristine && submitted ))}">
<input type="text" [formControl]="form.controls['lastname']" >
</div>
<div class="col-sm-8">
<button type="submit" >Register now</button>
</div>
我的,
export class SignUp {
submitted: boolean = false;
constructor(public fbld: FormBuilder, http: Http, public config: Config, public router: Router) {
this.http = http;
this.form = fbld.group({
firstname: ['', Validators.required],
});
this.header = this.config.header1;
}
onSubmit(form: ISignup): any {
this.submitted = true;
}
}
答案 0 :(得分:1)
请查看以下解决方案。
DEMO:https://plnkr.co/edit/mJFftirG3ATDpnJRWmKN?p=preview
export class AppComponent {
form: FormGroup;
constructor(private formBuilder: FormBuilder){
this.submitted=false;
this.form = formBuilder.group({
lastname: ['', Validators.required],
});
}
onSubmit(form)
{
this.submitted=true;
console.log(form);
if(form.valid) //<<<### submit form is it is valid only
{
console.log('form submitted');
}
}
}