[ACS-9374] Fix error validation process

This commit is contained in:
Shivangi917
2025-07-28 08:08:45 -04:00
parent a8c68452ba
commit 9a5e8b5bd5
3 changed files with 39 additions and 15 deletions
@@ -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);
@@ -22,7 +22,20 @@
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
*/
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();
}
@@ -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() {