angular和react

by Luuk Gruijs

Luuk Gruijs着

如何在Angular中验证默认和自定义React形式 (How to validate default and custom reactive forms in Angular)

When presenting forms to your users, it’s considered very user-friendly to give them immediate feedback on whether what they type is actually correct. Besides that, it could also limit the number of requests to the server. You would be able to catch 99% of the errors before submitting your form to the server.

向您的用户展示表单时,认为他们非常友好,可以立即向他们反馈输入的内容是否正确。 除此之外,它还可能限制对服务器的请求数量。 在将表单提交到服务器之前,您将能够捕获99%的错误。

When using reactive forms, Angular offers only a hand full of very generic validators. The default form field validators are:

当使用React形式时,Angular仅提供一堆非常通用的验证器。 默认的表单字段验证器为:

  • min: the value should be higher than a certain number.

    min:该值应大于一定数值。

  • max: the value should be lower than a certain number.

    max:该值应小于一定数值。

  • required: the value cannot be empty

    必填:值不能为空

  • requiredTrue: the value must be true

    requiredTrue:该值必须为true

  • email: the value should be an email

    电子邮件:值应为电子邮件

  • minLength: The value should contain a minimum amount of characters

    minLength:该值应包含最少数量的字符

  • maxLength: The value should contain a maximum amount of characters

    maxLength:该值应包含最大字符数

Chances are the above validators will not be able to match the requirements of your server. Therefore you cannot give the user the feedback they would like to get and help them submit a correct form. For this, you are going to need custom form field validators.

上述验证器很可能无法满足您服务器的要求。 因此,您无法向用户提供他们希望获得的反馈并无法帮助他们提交正确的表单。 为此,您将需要自定义表单字段验证器。

创建自定义表单字段验证器 (Creating a custom form field validator)

A form field validator is a function that takes your form control — the input field — and validates the value of that form control against a certain condition. This function either returns nothing when everything is ok or an object stating what went wrong.

表单字段验证器是一种功能,它接受您的表单控件(即输入字段),并针对特定条件验证该表单控件的值。 一切正常时,此函数不返回任何内容,或者一个对象指出发生了什么问题。

A very common use case of a custom validator is to check whether the form matches the sanitization rules of the server. This means the validator checks if the characters your user puts into your form are allowed. Let’s create this form validator now.

定制验证器的一个非常常见的用例是检查表单是否与服务器的清理规则匹配。 这意味着验证器检查是否允许用户输入您的表单中的字符。 让我们现在创建此表单验证器。

建立表格 (Building the form)

To use this validator we need to create a form and define it there. For this purpose, we’re going to create a simple user signup form. We use the reactive way to create the form. It can be done like this:

要使用此验证器,我们需要创建一个表单并在其中定义它。 为此,我们将创建一个简单的用户注册表单。 我们使用React方式来创建表单。 可以这样完成:

The template can then look like this:

模板可以如下所示:

We now have a working reactive form. We, however, did not apply any form validation. To add form validation, we simply pass our validators into the form creation function like this:

现在,我们有了一个有效的React形式。 但是,我们没有应用任何表单验证。 要添加表单验证,我们只需将验证器传递给表单创建函数,如下所示:

We used the required and email validators from Angular. We also imported our custom created validateCharacters validator. The last thing we still have to do is present potential errors to our users.

我们使用了来自Angular的必需验证器和电子邮件验证器。 我们还导入了自定义创建的validateCharacters验证器。 我们仍然要做的最后一件事是向我们的用户展示潜在的错误。

向用户呈现错误 (Presenting errors to the user)

There are two moments when we want to present errors to our users: when the focus moves from one field to the other and right before the final form submission.

我们有两个时刻要向用户展示错误:焦点从一个字段移到另一个字段,并且恰好在提交最终表单之前。

Let’s create a form service for this. This service could potentially look like this:

让我们为此创建一个表单服务。 该服务可能看起来像这样:

The validateForm method accepts the form to validate, a form errors object and a boolean on whether to check dirty fields or not. The function then loops over all the form controls and checks if there are errors on that control. If there are any, we find the correct error message that came from the validationMessages method and pass back the form errors object.

validateForm方法接受要验证的表单,一个表单错误对象以及一个是否检查脏字段的布尔值。 然后,该函数循环遍历所有窗体控件,并检查该控件上是否有错误。 如果有任何错误消息,我们将找到来自validationMessages方法的正确错误消息,并将其传递给表单错误对象。

To use this error service in our components, we have to do the following:

要在我们的组件中使用此错误服务,我们必须执行以下操作:

Now the final step is to show the error messages in our template. We can do that like this:

现在,最后一步是在模板中显示错误消息。 我们可以这样做:

If there any errors on one particular field, we show the message that’s in the formErrors object. Below is a full demo. Play around with the fields. Try to fill in characters like !#$^ in the name field and see if our custom form validator works. If no errors appear, hit the signup button and see the success message.

如果在一个特定字段上有任何错误,我们将显示formErrors对象中的消息。 以下是完整的演示。 在田野里玩。 尝试在名称字段中填写!#$^类的字符,并查看我们的自定义表单验证程序是否有效。 如果没有错误出现,请点击注册按钮,查看成功消息。

结论 (Conclusion)

I hope this article helps you validate your forms and give your users a better experience when filling in the forms.

我希望本文能帮助您验证表单并在填写表单时为用户提供更好的体验。

在阿姆斯特丹找工作? (Looking for a job in Amsterdam?)

I work for Sytac as a Senior front-end developer. We are looking for mid/senior developers that specialize in Angular, React, Java or Scala. Sytac is a very ambitious consultancy company in the Netherlands.

我在Sytac担任高级前端开发人员。 我们正在寻找专门从事Angular,React,Java或Scala的中/高级开发人员。 Sytac是荷兰一家雄心勃勃的咨询公司。

If you think you have what it takes to work with the best, send me an email on luuk.gruijs@sytac.io and I’m happy to tell you more.

如果您认为自己有最佳的工作条件,请给我发送电子邮件至luuk.gruijs@sytac.io ,我很乐意告诉您更多信息。

翻译自: https://www.freecodecamp.org/news/validating-reactive-forms-with-default-and-custom-form-field-validators-in-angular-5586dc51c4ae/

angular和react

angular和react_如何在Angular中验证默认和自定义React形式相关推荐

  1. 如何在JavaScript中验证电子邮件地址

    如何在JavaScript中验证电子邮件地址? #1楼 与squirtle相比 ,这是一个复杂的解决方案,但是在正确验证电子邮件方面做得非常出色: function isEmail(email) { ...

  2. 如何在Java中验证电话号码(正则表达式+ Google libphonenumber)

    关于如何在不同国家(例如美国,美国)使用Java验证电话号码的快速指南. 带有正则表达式和Google libphonenumber API的示例程序. 1.简介 在本教程中,我们将学习如何在java ...

  3. linux ios文件是否存在,技术|如何在 Linux 中验证 ISO 镜像

    你从喜爱的 Linux 发行版的官方网站或第三方网站下载了它的 ISO 镜像之后,接下来要做什么呢?是创建可启动介质并开始安装系统吗?并不是,请稍等一下.在开始使用它之前,强烈建议你检查一下你刚下载到 ...

  4. 如何在 Flutter 中禁用默认的 Widget 飞溅效果

    如何在 Flutter 中禁用默认的 Widget 飞溅效果 默认情况下,许多 Flutter Material Design 小部件在被选中时会显示飞溅效果. 这适用于IconButton,InkW ...

  5. python 指定证书验证_如何在python中验证SSL证书?

    我需要验证我的自定义CA签署了证书.使用OpenSSL命令行实用程序很容易做到: # Custom CA file: ca-cert.pem # Cert signed by above CA: bo ...

  6. 如何在 Python 中验证用户输入

    要验证用户输入: 使用 while 循环进行迭代,直到提供的输入值有效. 检查输入值在每次迭代中是否有效. 如果该值有效,则跳出 while 循环. # ✅ 验证用户输入的是否是整数num = 0wh ...

  7. linux 修改java版本_Linux 有问必答:如何在 Linux 中改变默认的 Java 版本

    提问:当我尝试在Linux中运行一个Java程序时,我遇到了一个错误.看上去像程序编译所使用的Java版本与我本地的不同.我该如何在Linux上切换默认的Java版本? 当Java程序编译时,编译环境 ...

  8. 怎么在linux上修改mysql端口映射_如何在Linux中更改默认的MySQL / MariaDB端口

    在本指南中,我们将学习如何更改MySQL / MariaDB数据库在CentOS 7和基于Debian的Linux发行版中绑定的默认端口. MySQL数据库服务器在Linux和Unix下运行的默认端口 ...

  9. 如何在create-react-app中使用Workbox构建自定义PWA

    Note: This is the third in a series of posts about PWAs inside of React. For a quick primer, see the ...

最新文章

  1. 视觉人工智能市场格局初成型,国内企业占半壁江山
  2. 安装程序检测到无法验证文件的发行者_InstallShield Installation 文件
  3. 【项目】itdage-java获取天气和发短信
  4. 32位hex转浮点 python_python——int()、hex()、oct()、bin()、float()数值类型转换函数
  5. Leetcode--870. 优势洗牌
  6. PAT (Basic Level) Practise (中文)- 1002. 写出这个数 (20)
  7. 数据结构笔记-----链表
  8. Windows10安装sql2016配置iis问题
  9. 没有IDEA与Eclipse,该怎么学Java?
  10. 重装系统大师计算机硬件不兼容,360重装系统教你如何处理提示不兼容的软件
  11. distpicker动态赋值问题
  12. CSS 加粗(css font-weight)
  13. linux swap shayisi,临时邮箱,10分钟邮箱域名收集(持续更新)
  14. yum 碰到的问题与解决办法
  15. 运筹优化学习07:Lingo的 @if 函数的使用方法
  16. 如何计算机械能增加量,探讨优化验证机械能守恒定律实验中动能增加量的计算方法...
  17. 派森诺细菌完成图标准分析轻松发文
  18. autojs之大柒侧滑栏详解
  19. java文件实现文件的上传和下载
  20. 视频分析代码douyin

热门文章

  1. 阿里P8成长路线!我的头条面试经历分享,吊打面试官系列!
  2. 2019牛客多校第三场 F.Planting Trees
  3. 遇到Visual Studio 当前不会命中断点.还没有为该文档加载任何符号的情况
  4. Oracle VM Virtual Box的安装
  5. 山东理工大学第七届ACM校赛-G 飞花的传送门
  6. windows 删除删除不掉的文件
  7. 网速正常,一打开IE总是卡很久
  8. android模拟器安装及优化(集锦)
  9. 中国直销未来三五年的发展趋势[转]
  10. pip国内加载速度慢解决方法