From 9a5e8b5bd5fe6340bb0764f732208c291d204f3c Mon Sep 17 00:00:00 2001 From: Shivangi917 Date: Thu, 24 Jul 2025 09:14:38 -0400 Subject: [PATCH] [ACS-9374] Fix error validation process --- .../search-input-control.component.spec.ts | 1 - .../search-input-control.component.ts | 49 ++++++++++++++----- .../search-input/search-input.component.ts | 4 +- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/projects/aca-content/src/lib/components/search/search-input-control/search-input-control.component.spec.ts b/projects/aca-content/src/lib/components/search/search-input-control/search-input-control.component.spec.ts index 384a68aa1..b6dfac27a 100644 --- a/projects/aca-content/src/lib/components/search/search-input-control/search-input-control.component.spec.ts +++ b/projects/aca-content/src/lib/components/search/search-input-control/search-input-control.component.spec.ts @@ -80,7 +80,6 @@ describe('SearchInputControlComponent', () => { }); it('should check if searchTerm has a length less than 2', () => { - expect(component.isTermTooShort()).toBe(false); component.searchTerm = 'd'; fixture.detectChanges(); expect(component.isTermTooShort()).toBe(true); diff --git a/projects/aca-content/src/lib/components/search/search-input-control/search-input-control.component.ts b/projects/aca-content/src/lib/components/search/search-input-control/search-input-control.component.ts index e00155221..60fd45758 100644 --- a/projects/aca-content/src/lib/components/search/search-input-control/search-input-control.component.ts +++ b/projects/aca-content/src/lib/components/search/search-input-control/search-input-control.component.ts @@ -22,7 +22,20 @@ * from Hyland Software. If not, see . */ -import { Component, EventEmitter, Input, Output, ViewEncapsulation, ViewChild, ElementRef, OnInit, inject, DestroyRef } from '@angular/core'; +import { + Component, + EventEmitter, + Input, + Output, + ViewEncapsulation, + ViewChild, + ElementRef, + OnInit, + inject, + DestroyRef, + OnChanges, + SimpleChanges +} from '@angular/core'; import { CommonModule } from '@angular/common'; import { TranslatePipe } from '@ngx-translate/core'; import { MatButtonModule } from '@angular/material/button'; @@ -41,9 +54,22 @@ import { noWhitespaceValidator } from '@alfresco/aca-shared'; encapsulation: ViewEncapsulation.None, host: { class: 'app-search-control' } }) -export class SearchInputControlComponent implements OnInit { +export class SearchInputControlComponent implements OnInit, OnChanges { private readonly destroyRef = inject(DestroyRef); + private validateInput(): void { + const errors = this.searchFieldFormControl.errors; + if (errors?.whitespace) { + this.validationError.emit('SEARCH.INPUT.WHITESPACE'); + } else if (errors?.required) { + this.validationError.emit('SEARCH.INPUT.REQUIRED'); + } else if (this.hasLibrariesConstraint && this.isTermTooShort()) { + this.validationError.emit('SEARCH.INPUT.MIN_LENGTH'); + } else { + this.validationError.emit(''); + } + } + /** Type of the input field to render, e.g. "search" or "text" (default). */ @Input() inputType = 'text'; @@ -96,16 +122,7 @@ export class SearchInputControlComponent implements OnInit { this.searchFieldFormControl.events.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((event) => { if (event instanceof TouchedChangeEvent || event instanceof StatusChangeEvent) { if (this.searchFieldFormControl.touched) { - const errors = this.searchFieldFormControl.errors; - if (errors?.whitespace) { - this.validationError.emit('SEARCH.INPUT.WHITESPACE'); - } else if (this.hasLibrariesConstraint) { - this.validationError.emit('SEARCH.INPUT.MIN_LENGTH'); - } else if (errors?.required) { - this.validationError.emit('SEARCH.INPUT.REQUIRED'); - } else { - this.validationError.emit(''); - } + this.validateInput(); } else { this.validationError.emit(''); } @@ -113,6 +130,14 @@ export class SearchInputControlComponent implements OnInit { }); } + ngOnChanges(changes: SimpleChanges): void { + if (changes['hasLibrariesConstraint'] && !changes['hasLibrariesConstraint'].firstChange) { + if (this.searchFieldFormControl.touched) { + this.validateInput(); + } + } + } + openDropdown() { this.searchInput.nativeElement.focus(); } diff --git a/projects/aca-content/src/lib/components/search/search-input/search-input.component.ts b/projects/aca-content/src/lib/components/search/search-input/search-input.component.ts index 47b7c0589..9099b751e 100644 --- a/projects/aca-content/src/lib/components/search/search-input/search-input.component.ts +++ b/projects/aca-content/src/lib/components/search/search-input/search-input.component.ts @@ -153,8 +153,8 @@ export class SearchInputComponent implements OnInit, OnDestroy { showInputValue() { this.appService.setAppNavbarMode('collapsed'); this.has400LibraryError = false; - this.hasLibrariesConstraint = this.evaluateLibrariesConstraint(); this.searchedWord = this.getUrlSearchTerm(); + this.hasLibrariesConstraint = this.evaluateLibrariesConstraint(); if (this.searchInputControl) { this.searchInputControl.searchTerm = this.searchedWord; @@ -203,8 +203,8 @@ export class SearchInputComponent implements OnInit, OnDestroy { } this.has400LibraryError = false; - this.hasLibrariesConstraint = this.evaluateLibrariesConstraint(); this.searchedWord = searchTerm; + this.hasLibrariesConstraint = this.evaluateLibrariesConstraint(); } searchByOption() {