[ACS-9374] Add validator to aca-shared

This commit is contained in:
Shivangi917
2025-07-28 08:04:50 -04:00
parent ca259f7cd1
commit 3791cf2978
5 changed files with 19 additions and 15 deletions
@@ -31,7 +31,7 @@ import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { FormControl, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { noWhitespaceValidator } from 'projects/aca-content/folder-rules/src/rule-details/validators/no-whitespace.validator';
import { noWhitespaceValidator } from 'projects/aca-shared/src/lib/validators/no-whitespace.validator';
@Component({
imports: [CommonModule, TranslatePipe, MatButtonModule, MatIconModule, MatFormFieldModule, MatInputModule, FormsModule, ReactiveFormsModule],
@@ -106,6 +106,6 @@ export class SearchInputControlComponent implements OnInit {
}
isTermTooShort() {
return !!(this.searchTerm.trim() && this.searchTerm.trim().length < 2);
return this.searchTerm.trim()?.length < 2;
}
}
@@ -36,17 +36,21 @@
(submit)="onSearchSubmit($event)"
(searchChange)="onSearchChange($event)"
/>
<mat-error *ngIf="hasLibrariesConstraint" class="app-search-error">
{{ 'SEARCH.INPUT.HINT' | translate }}
<mat-error *ngIf="searchInputControl.searchFieldFormControl.touched" class="app-search-error">
<ng-container *ngIf="hasLibrariesConstraint; else checkRequired">
{{ 'SEARCH.INPUT.HINT' | translate }}
</ng-container>
<ng-template #checkRequired>
<ng-container *ngIf="searchInputControl.searchFieldFormControl.errors?.required; else checkWhitespace">
{{ 'SEARCH.INPUT.REQUIRED' | translate }}
</ng-container>
</ng-template>
<ng-template #checkWhitespace>
<ng-container *ngIf="searchInputControl.searchFieldFormControl.errors?.whitespace">
{{ 'SEARCH.INPUT.WHITESPACE' | translate }}
</ng-container>
</ng-template>
</mat-error>
<mat-error
*ngIf="searchInputControl.searchFieldFormControl.errors?.required && searchInputControl.searchFieldFormControl.touched" class="app-search-error">
{{ 'SEARCH.INPUT.REQUIRED' | translate }}
</mat-error>
<mat-error *ngIf="searchInputControl.searchFieldFormControl.errors?.whitespace && searchInputControl.searchFieldFormControl.touched" class="app-search-error">
{{ 'SEARCH.INPUT.WHITESPACE' | translate }}
</mat-error>
<div id="search-options" class="app-search-options">
<mat-checkbox *ngFor="let option of searchOptions"
id="{{ option.id }}"
@@ -47,13 +47,13 @@ describe('SearchInputComponent', () => {
return error?.nativeElement.textContent.trim();
}
async function openMenu() {
async function openMenu(): Promise<MatMenuHarness> {
const menu = await loader.getHarness(MatMenuHarness);
await menu.open();
return menu;
}
async function getCheckbox(id: string) {
function getCheckbox(id: string): Promise<MatCheckboxHarness> {
const overlayLoader = TestbedHarnessEnvironment.documentRootLoader(fixture);
return overlayLoader.getHarness(MatCheckboxHarness.with({ selector: `#${id}` }));
}
@@ -27,7 +27,7 @@ import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';
export const noWhitespaceValidator = (): ValidatorFn => {
return (control: AbstractControl): ValidationErrors | null => {
const rawValue = control.value;
if (rawValue === null || rawValue === '') {
if (!rawValue) {
return null;
}