简体   繁体   English

角度材料自定义控件,在提交表单时也会显示错误

[英]angular material custom control that displays errors also when the form is submitted

I have an issue with a custom material input I created.我创建的自定义材料输入有问题。 It's a country-autocomplete input which implements MatFormFieldControl and ControlValueAccessor.它是一个国家自动完成输入,它实现了 MatFormFieldControl 和 ControlValueAccessor。 The value it should reflect to the outer form is the ID of the country.它应该反映到外部表单的值是国家的 ID。

The problem is that when I place it inside a , if there are errors, they are not displayed when the user submits the form.问题是当我把它放在 a 中时,如果有错误,当用户提交表单时它们不会显示。

https://stackblitz.com/edit/angular-fxu8rz?file=src/app/form-field-custom-control-example.html https://stackblitz.com/edit/angular-fxu8rz?file=src/app/form-field-custom-control-example.html

Did anybody have this kind of scenario?有人遇到过这种场景吗?

This is how I solved it (may be a bit hacky and i'm opened for better solutions):这就是我解决它的方式(可能有点hacky,我愿意寻求更好的解决方案):

I Injected the formGroupDirective to the component, and I am now checking if the form is submitted also.我将 formGroupDirective 注入到组件中,现在我正在检查表单是否也已提交。 That made the red border appear, but still no error messages for some reason.这使红色边框出现,但由于某种原因仍然没有错误消息。 The second thing i did (and that's what I don't like this solution sdo much) is to add an updateValueAndValidity() whenever in side the errorState function.我做的第二件事(这就是我不喜欢这个解决方案的原因)是在 errorState 函数旁边添加一个 updateValueAndValidity() 。

That's the updated stackblitz:这是更新的堆栈闪电战:

https://stackblitz.com/edit/angular-fxu8rz-vcqtsd?fbclid=IwAR0yJtsxcHZal7_ZHPERQr8Prcxz5QqoEh78qEEIXRXtwPyNFec3qiKrZxk&file=src%2Fapp%2Fcountry-input%2Fcountry-input.component.ts https://stackblitz.com/edit/angular-fxu8rz-vcqtsd?fbclid=IwAR0yJtsxcHZal7_ZHPERQr8Prcxz5QqoEh78qEEIXRXtwPyNFec3qiKrZxk&file=src%2Fapp%2Fcountry-inputst%2Fcountry-inputs%2F

暂无
暂无

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

相关问题 带有验证错误的 Angular6 Material 自定义表单字段控件(mat-error) - Angular6 Material custom form field control with validation errors (mat-error) 打开日期选择器角度材料时提交表格 - form is submitted when I open the datepicker angular material Angular Material ErrorStateMatcher可以检测何时提交父表单吗? - Can the Angular Material ErrorStateMatcher detect when a parent form is submitted? Angular Material:mat-form-field 在创建自定义表单域控件时必须包含一个 MatFormFieldControl - Angular Material: mat-form-field must contain a MatFormFieldControl when creating a custom form field control Angular 10:在 Angular Material 自定义表单字段控件中访问 NgControl - Angular 10: Access NgControl in an Angular Material custom form field control 尝试从 angular 材料制作自定义表单字段控件时,有没有办法消除焦点? - Is there a way to remove focus when trying to make custom form field control from angular material? Angular 自定义校验:确保表单组有校验错误时表单控件无效 - Angular custom validation: ensure form control is invalid when the form group has validation errors Angular Material - 带有错误状态的自定义反应式表单控件 - Angular Material - Custom Reactive Form Control with Error State 角材料:“自定义表单字段控件”-移动浮动标签(向下) - Angular Material: Custom Form Field Control - move floating label (down) angular材料的表格控制问题 - Form control problem with angular Material
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM