Skip to content

Commit dde51c7

Browse files
author
Pennati, Lucas
committed
feat: Add the ability to disable validationservice on angular
1 parent ad7adb9 commit dde51c7

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

docs/changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
88

99
### Added
1010

11+
- Added the ability to disable the `ValidationService` on Angular. See
12+
[docs](./guide/angular.md#customisingdisabling-validationservice) for more details.
13+
1114
### Fixed
1215

1316
### Removed

docs/guide/angular.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ applied.
8989

9090
So instead of doing:
9191

92-
```
92+
```angular2html
9393
// my-component.component.html
9494
<six-input [formControl]="formControl" [required]="true"></six-input>
95+
```
9596

97+
```ts
9698
// my-component.component.ts
9799
@Component({
98100
...
@@ -105,19 +107,21 @@ export class MyComponent {
105107

106108
when initialising the library pass the `showAsteriskOnRequiredValidator` property like so:
107109

108-
```
109-
UiLibraryAngularModule.forRoot({
110+
```ts
111+
UiLibraryAngularModule.forRoot(/* ... other options*/,{
110112
showAsteriskOnRequiredValidator: true
111113
})
112114
```
113115

114116
Now your component will apply the required flag automatically, without requiring you to set it
115117
manually:
116118

117-
```
119+
```angular2html
118120
// my-component.component.html
119121
<six-input [formControl]="formControl"></six-input> // <-- [required] prop can be omitted
122+
```
120123

124+
```ts
121125
// my-component.component.ts
122126
@Component({
123127
...
@@ -128,6 +132,26 @@ export class MyComponent {
128132
}
129133
```
130134

135+
### Customising/Disabling `ValidationService`
136+
137+
The Angular library ships with a built-in `ValidationService` that can be used to validate
138+
`FormControl`s.
139+
140+
To override it, create your own implementation that extends `ValidationMessagesService`. You can
141+
then override the `getErrorMessage(language: Language, error: ValidationError)` method to return
142+
custom error messages.
143+
144+
To completely disable the `ValidationService`, pass the `disableValidation` option to the
145+
`forRoot()` method:
146+
147+
```ts
148+
UiLibraryAngularModule.forRoot(/* ... other options*/, {
149+
disableValidationService: true
150+
})
151+
```
152+
153+
WARNING: This will disable any sort of validation service. Even if you pass your .
154+
131155
## Using the Components
132156

133157
The components can be utilized just like any other Angular component. However, there's one caveat:

libraries/ui-library-angular/src/lib/control-value-accessors/value-accessor.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AfterViewInit, Directive, ElementRef, HostListener, inject, Injector, OnDestroy } from '@angular/core';
1+
import { AfterViewInit, Directive, ElementRef, HostListener, inject, Injector, OnDestroy, OnInit } from '@angular/core';
22
import { AbstractControl, ControlValueAccessor, NgControl, Validators } from '@angular/forms';
33
import { Subscription } from 'rxjs';
44
import { getLanguage, ValidationError } from '@six-group/ui-library';
@@ -69,12 +69,13 @@ export class ValueAccessor implements ControlValueAccessor, AfterViewInit, OnDes
6969
const control = this.ngControl?.control;
7070

7171
const invalid = control.status === 'INVALID' && control.dirty && control.touched;
72-
let errorTexts;
73-
if (invalid) {
74-
errorTexts = this.initialErrorText || this.getErrorTexts(control);
75-
}
7672
element.invalid = invalid;
77-
element.errorText = errorTexts ?? '';
73+
74+
// If the module is configured to do so, display error messages for invalid controls
75+
if (!this.config.disableValidationService && invalid) {
76+
const errorTexts = this.initialErrorText || this.getErrorTexts(control);
77+
element.errorText = errorTexts ?? '';
78+
}
7879

7980
// When the module is configured to do so, display an asterisk next to any form control that has a required validator
8081
if (this.config.showAsteriskOnRequiredValidator && this.ngControl.control.hasValidator(Validators.required)) {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { InjectionToken } from '@angular/core';
22

33
export interface UiLibraryConfig {
4-
showAsteriskOnRequiredValidator: boolean;
4+
showAsteriskOnRequiredValidator?: boolean;
5+
disableValidationService?: boolean;
56
}
67

78
export const DEFAULT_UI_LIBRARY_CONFIG: UiLibraryConfig = {
89
showAsteriskOnRequiredValidator: false,
10+
disableValidationService: false,
911
};
1012

1113
export const UI_LIBRARY_CONFIG = new InjectionToken<UiLibraryConfig>('UiLibraryConfig');

0 commit comments

Comments
 (0)