简体   繁体   English

如果该模式已经实现了这方面,是否需要?

[英]Do I need required if the pattern already implement this aspect?

I want some fields in a form to be required and to respect some pattern. 我希望表单中的某些字段是必需的,并且要尊重某些模式。

Currently I have this : Validators.required, Validators.pattern('^[0-9-_\\/]+$') 当前我有这个: Validators.required, Validators.pattern('^[0-9-_\\/]+$')

but I was curious, is the .required is needed since the + in the regex already specify I need more than 0 element ? 但我很好奇,因为正则表达式中的+已经指定我需要大于0的元素,所以需要.required吗? Is there anything the required validators does that a '.+' regex wouldn't already have ? 是否有需要的验证程序执行“。+”正则表达式所没有的任何内容?

This is only for UI purposes. 这仅用于UI。

One will display the pattern error, the other one the required error. 一个将显示模式错误,另一个将显示所需的错误。

If you wish to display distinct errors, then you should keep it. 如果您希望显示明显的错误,则应保留该错误。

Otherwise, if you simply display "Error on the form" (or something alike), then no, you don't need to use the required validator. 否则,如果仅显示“表单上的错误”(或类似内容),则否,则无需使用所需的验证器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我已经有了 expectOne() 还需要 verify()? - Why do I need verify() when I already have expectOne()? 如果我已经添加了 formControlName,是否需要在输入中添加名称? - Do I need to add a name to the input if I am already adding a formControlName? Angular 8+ - 如果我不打算覆盖它,是否需要在我的组件中实现 OnInit function? - Angular 8+ - do I need to implement the OnInit function in my components if I am not planning to override it? 为什么需要在 app.module.ts 中导入 HttpClientModule 当我已经导入到使用它的单独模块中时 - Why do need to import HttpClientModule in app.module.ts when I've already imported in a separate module where it is used 需要帮助来完成我的正则表达式以在字符串长度小于 6 时找到所需的 * 并在长度为 6 时找到模式匹配 - Need help to complete my regexp to find a required * when string length is less than 6 and pattern matching when length is 6 需要正则表达式验证模式 - Regex Valdation Pattern Required 如何在 Typescript 中进行输入? - How do I make an Input required in Typescript? 我需要使用 angular 材料检查一个复选框 - I need to get a checkbox already checked with angular material 我需要可可豆吗? - Do I need Cocoa Pods? 需要帮助来了解如何正确实现订阅 - Need help in understanding how I can implement subscribe properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM