Skip to content

Commit e67a8aa

Browse files
committed
feat(ngxerrors.directive): add hasError(‘*’) support for control state validity
1 parent 3ad99fb commit e67a8aa

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

example/app/stock-inventory/components/stock-branch/stock-branch.component.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,25 @@ import { FormGroup } from '@angular/forms';
2424
</div>
2525
</div>
2626
27-
<div>
28-
Pristine: {{ store.hasError('required', ['pristine']) }}
29-
Dirty: {{ store.hasError('required', ['dirty']) }}
30-
Touched: {{ store.hasError('required', ['touched']) }}
31-
Touched, Dirty: {{ store.hasError('required', ['touched', 'dirty']) }}
27+
<div *ngIf="myError.hasError('*', ['touched', 'dirty'])">
28+
The control is invalid
3229
</div>
3330
34-
<div>
35-
<input [ngStyle]="{ borderColor: store.hasError('required', ['touched']) ? 'red' : 'grey' }" type="text" placeholder="Manager Code" formControlName="code">
36-
</div>
31+
<input
32+
type="text"
33+
placeholder="Manager Code"
34+
formControlName="code"
35+
[class.required]="myError.hasError('required', ['dirty', 'touched'])">
3736
38-
<div ngxErrors="store.code" #store="ngxErrors">
39-
<div class="error" ngxError="required" [when]="['touched']">
40-
Manager ID is required
37+
<div ngxErrors="store.code" #myError="ngxErrors">
38+
<div class="error" ngxError="required" [when]="['dirty', 'touched']">
39+
Field is required
40+
</div>
41+
<div class="error" ngxError="minlength" [when]="['dirty', 'touched']">
42+
Min-length is {{ myError.getError('minlength')?.requiredLength }}
4143
</div>
42-
<div class="error" [ngxError]="['minlength', 'maxlength']" [when]="['dirty']">
43-
Minlength is 2, max length is 5
44+
<div class="error" ngxError="maxlength" [when]="['dirty', 'touched']">
45+
Max-length is {{ myError.getError('maxlength')?.requiredLength }}
4446
</div>
4547
</div>
4648

example/app/stock-inventory/services/stock-inventory.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ export class StockInventoryService {
3030
}
3131

3232
checkBranchId(id: string): Observable<boolean> {
33-
return Observable.of(false);
33+
return Observable.of(true);
3434
}
3535
}

src/ngxerrors.directive.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ export class NgxErrorsDirective implements OnChanges, AfterViewInit {
4343

4444
hasError(name: string, conditions: ErrorOptions): boolean {
4545
if (!this.ready) return;
46-
return this.control.hasError(name) && this.checkControlProps(conditions);
46+
const controlPropsState = this.checkControlProps(conditions);
47+
if (name.charAt(0) === '*') {
48+
return this.control.invalid && controlPropsState;
49+
}
50+
return this.control.hasError(name) && controlPropsState;
4751
}
4852

4953
getError(name: string) {

0 commit comments

Comments
 (0)