mai*_*aik 6 bootstrap-4 ngx-bootstrap angular
我正在阅读ng-bookAngular 5上的Ari Lerner .我正在使用ngx-bootstrap和Bootstrap 4.表格验证似乎没有像勒纳先生那样实施.我不确定这是否是一个限制ngx-bootstrap...任何人都知道?
编辑
好的,我删除了ngx-bootstrap,只是加载了MaxCDNs Bootstrap 4,我有同样的问题.错误消息未出现.
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { NavbarComponent } from './navbar/navbar.component';
import { RegistrationFormComponent } from './registration-form/registration-form.component';
import {
FormsModule,
ReactiveFormsModule,
FormBuilder,
FormGroup
} from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
NavbarComponent,
RegistrationFormComponent
],
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
注册用form.component.ts
import { Component, OnInit } from '@angular/core';
import {
FormBuilder,
FormGroup,
Validators
} from '@angular/forms';
@Component({
selector: 'app-registration-form',
templateUrl: './registration-form.component.html',
styleUrls: ['./registration-form.component.scss']
})
export class RegistrationFormComponent implements OnInit {
regForm: FormGroup;
// name: AbstractControl;
constructor(fb: FormBuilder) {
this.regForm = fb.group({
'name': ['', Validators.required],
// 'email': ['', Validators.required],
// 'password': ['', Validators.required],
// 'password_confirmation': ['', Validators.required]
});
// this.name = this.regForm.controls['name'];
}
ngOnInit() {
}
onSubmit(value: string): void{
console.log(value);
}
}
Run Code Online (Sandbox Code Playgroud)
注册用form.component.html
<div class="row justify-content-center">
<h1>New User</h1>
</div>
<div class='row justify-content-center'>
<div class='col-6'>
<form [formGroup]='regForm'
(ngSubmit)="onSubmit(regForm.value)"
[class.error]="!regForm.valid && regForm.touched"
>
<div class='form-group'
[class.error]="!regForm.get('name').valid && regForm.get('name').touched">
<label>Name</label>
<input type="text" class='form-control' [formControl]="regForm.controls['name']">
<div *ngIf="regForm.controls['name'].hasError('required')" class="invalid-feedback">Name is required</div>
</div>
<div class='form-group'>
<label>Email</label>
<input type="email" class='form-control'>
</div>
<div class='form-group'>
<label>Password</label>
<input type="password" class='form-control'>
</div>
<div class='form-group'>
<label>Confirmation</label>
<input type="password" class='form-control'>
</div>
<button type="submit" class='btn btn-default'>Submit</button>
</form>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
它将class="was-validated"按照文档添加 a 到最外层的 div 来工作:链接到引导程序表单验证。这将启动对您的字段的验证。另外,请记住通过传递required表单来标记您的输入并关闭默认的 html 验证novalidate
工作代码示例:
<div class="container" class="was-validated">
<form [formGroup]="regForm" novalidate (ngSubmit)="submitForm(regForm.value)">
<div class="row justify-content-center">
<div class="form-group col-6">
<label class="col-12 col-form-label" for="email">Email</label>
<input type="email" placeholder="Email address" class="form-control form-control-lg col-12" id="email" [formControl]="regForm.controls['email']"
required>
<div class="invalid-feedback">
Please provide a valid email.
</div>
</div>
</div>
<div class="row justify-content-center">
<div class="form-group col-6">
<label class="col-12 col-form-label" for="password">Password</label>
<input type="password" placeholder="Password" id="password" class="form-control form-control-lg col-12" [formControl]="regForm.controls['password']" required>
<div class="invalid-feedback">
Please provide a password.
</div>
</div>
</div>
<div class="row justify-content-center">
<div class="form-group col-6">
<button type="submit" class="btn btn-outline-secondary btn-block col-12">Sign in</button>
</div>
</div>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
您可以 - 并且可能应该以class="was-validated"编程方式添加,而不是像我一样对其进行硬编码。
如果您对此有任何疑问,请随时评论我的帖子 - 我会更新我的答案。
| 归档时间: |
|
| 查看次数: |
8037 次 |
| 最近记录: |