Angular 5 ngx-bootstrap 表单验证

2023-12-20

我正在读阿里·勒纳的书ng-book在 Angular 5 上。我正在使用ngx-bootstrap and Bootstrap 4。表单验证似乎并不像勒纳先生实现的那样工作。我不确定这是否是一个限制ngx-bootstrap……有人知道吗?

edit好的,我删除了ngx-bootstrap并刚刚加载了 MaxCDNBootstrap 4我也有同样的问题。错误消息没有出现。

应用程序模块.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 { }

注册表格.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);
  }
}

注册表格.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>

它会通过添加一个来工作class="was-validated"根据文档到最外层的 div:引导表单验证的链接 http://getbootstrap.com/docs/4.0/components/forms/?#validation。这将启动对您的字段的验证。另外,请记住用以下标记标记您的输入required并通过传递表单 a 来关闭默认的 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>

您可以 - 并且可能应该添加class="was-validated"以编程方式,而不是像我那样硬编码。

如果您对此有任何疑问,请随时评论我的帖子 - 我会更新我的答案。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular 5 ngx-bootstrap 表单验证 的相关文章

随机推荐

  • webpack-dev-server 不会在 html 或 sass 更改时重新加载

    我的项目有这样的结构 root webpack config js public index html css directives views dist webpack output app js index html app js ma
  • 如何修改 GitHub 上的拉取请求以更改要合并到的目标分支?

    我有一个请求请求从我的分支合并到 master 但所有者希望我更改请求以合并到与我的分支不同的分支 这可能吗 可能的解决方案有哪些 2016 年 8 月更新 更改拉取请求的基础分支 https github com blog 2224 ch
  • 确定用户是否在线的最简单方法是什么? (PHP/MYSQL)

    有没有办法可以通过会话来了解用户是否在线 即 使用日志 我设置一个 SESSION 变量 用户超时 cookie 垃圾收集器更新数据库以将其状态更新为离线 EDIT 我想要一个不涉及时间或日期的解决方案 我想要一些可以在会议上骑行的东西或类
  • 致命错误 C1083:无法打开包含文件:'iostream':没有这样的文件或目录

    我已重新安装 Visual Studio 2010 Professional 多次以尝试使其正常工作 我不得不卸载 Visual Studio 2012 Professional 因为它无法编译我们在课堂上所做的事情 我完全卸载了包括 SQ
  • xmlns、xmlns:xsi、xsi:schemaLocation 和 targetNamespace?

    对于以下 XML 片段
  • 实体框架和存储过程

    我注意到在实体框架设计器中 您可以映射插入 更新和删除操作的存储过程 有没有什么方法可以对 Select 操作执行此操作 或者数据库访问代码是否有一个新的方向 我们不再为基本的选择操作编写存储过程 我工作的公司非常坚持始终对每个数据库操作使
  • Python 中的双大于号

    我在查看一些Python源代码时 发现了这个 print gt gt sys stderr doc gt gt 是什么意思 我以前从未见过这样的语法 请参阅 打印 V 形 说明Python 2 7 文档 https docs python
  • 客户端Flash(SWF)应该如何与服务器端.NET通信?

    所以我在IIS7的服务器上运行ASP NET 我想我将使用 MVC 来处理一些静态页面和基本动态表单 但大多数客户端都是用 Flash ActionScript 编写的 在客户端和服务器之间构建 生成代理的最简单 最简洁 最DRY的方法是什
  • 如何在不写入文件的情况下将 DataVisualization.Charting.Chart 的图像获取到 iTextSharp.text.Image?

    我正在编写一个用于测量数据可视化的软件 为此 我使用 System Windows Forms DataVisualization Charting Chart 并且我确实知道我可以通过 ChartObj SaveImage 获取显示的图像
  • python dict.add_by_value(dict_2)?

    问题 gt gt gt a dict a 1 b 2 gt gt gt b dict b 3 c 2 gt gt gt c c a 1 b 5 c 2 因此 这个想法是通过 int float 值以最短的形式添加到字典中 这是我设计的一种解
  • SQL Server 如何决定隐式日期时间转换的格式?

    declare str datetime varchar 50 set str datetime 30 04 2012 19 01 45 30th April 2012 declare dt datetime datetime select
  • 不知道为什么 dcast() 这个数据集会导致变量丢失

    我有一个数据框 如下所示 id fromuserid touserid from country to country length 1 1 54525953 47195889 US US 2 2 2 54525953 54361607 U
  • 在日志文件中隐藏敏感/机密信息

    您将如何隐藏敏感信息以免进入日志文件 是的 您可以有意识地选择首先不记录敏感信息 但在一般情况下 您可能会盲目地记录失败时的错误消息或在调查问题等时跟踪消息 最终导致敏感信息登陆您的计算机 日志文件 例如 您可能尝试将包含客户信用卡号的订单
  • 如何忽略脚本内的 stdin 输入

    我有一个程序 myprogram 它在控制台上运行时等待输入键 当我们按下某个键后 它就会退出 我想在后台运行这个 myprogram 并忽略标准输入 我有一个示例脚本 示例 sh 脚本 myprogram exit 0 但当我跑步时 sh
  • 如何在 gdb 中中断 UBSan 报告并继续?

    GCC 和 Clang 的最新版本具有未定义行为清理程序 UBSan 它是一个编译标志 fsanitize undefined 添加运行时检测代码 出现错误时 会显示如下警告 packet ber c 1917 23 运行时错误 54645
  • 如何从日期/时间字段快速选择不同的日期,SQL Server

    我想知道是否有一个性能良好的查询来从 SQL Server 中带有日期时间字段的表中选择不同的日期 忽略时间 我的问题不是让服务器实际执行此操作 我已经看到这个问题 https stackoverflow com questions 887
  • ElasticSearch NEST 搜索

    当我尝试对 ElasticSearch 实例执行搜索时遇到意外错误 我正在关注这里的文档 http nest azurewebsites net nest quick start html http nest azurewebsites n
  • 如何在单击时展开文本区域

    我正在开发一个有文本区域的小项目 我需要帮助来使文本区域在鼠标单击时展开 就像 Twitter 和 Facebook 那样 文本区域首先应该看起来像一个文本字段 然后单击时应该展开 这可以在不使用 JavaScript jQuery 的情况
  • R中的时间序列

    我正在电子表格中跟踪我的体重 但我想通过使用 R 来改善体验 我试图在 R 中找到一些有关时间序列分析的信息 但没有成功 我这里的数据采用以下格式 date gt weight gt body fat percentage gt water
  • Angular 5 ngx-bootstrap 表单验证

    我正在读阿里 勒纳的书ng book在 Angular 5 上 我正在使用ngx bootstrap and Bootstrap 4 表单验证似乎并不像勒纳先生实现的那样工作 我不确定这是否是一个限制ngx bootstrap 有人知道吗