Exploring Angular 5's BehaviourSubject for validating multiple email fields

Working with Angular5, I have a project page that includes an upload feature using a core upload component from my organization. Users can upload up to 5 files and must fill in the email field before clicking the "upload" button.

My goal is to trigger a service call as soon as the user exits the email field, displaying an error message based on the response received.

I am implementing this functionality as follows:

Component HTML

<my-upload-compt (validateEmail)="checkEmail($event)" [isEmailValid]="isEmailValid"></my-upload-compt>

Component TypeScript

isEmailValid: BehaviorSubject<boolean>;

checkEmail($event){
    // perform service call, assuming it returns false.
    this.isEmailValid.next(false);
}

Core Component TypeScript

@Input() isEmailValid: BehaviorSubject<boolean>

ngOnInit(){
    this.isEmailValid.subscribe(value => {
    // display error message
});
}

My issue is that if a valid email is entered in the first field and an invalid one in the next, both fields show an error message. How can I ensure that only the specific field being focused out displays an error message? Please advise.

PS: The function call on focus out is functioning correctly; it's just that all fields show errors even if only one has an incorrect email.

Answer №1

Utilizing Angular's Reactive forms and AsyncValidatorFn is the ideal solution for this scenario. By leveraging these tools, you can easily achieve the specific functionality required for each field.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering an issue in a Next.js application while building it, where an error is triggered because the property 'protocol' of 'window.location' cannot be destructured due to being undefined

While building my nextjs application, I encountered the following error. My setup uses typescript for building purposes, although I am only using JavaScript. Build error occurred: TypeError: Cannot destructure property 'protocol' of 'window ...

Adjust the minimum time in ngb-timepicker in Angular version 4

I created a straightforward form for selecting a time range using the code snippet below: <form [formGroup]="editEventForm" (ngSubmit)="c(onSubmitUpdate())"> <div class="form-group bottom-form-details"> <div class="row"> < ...

Typescript - Error in Parsing: Expecting an expression

I am currently working with Vue and TypeScript and have encountered a problem. How can I resolve it? Here is the code snippet in question: private setTitle(systemConfig: any) { const systemConfigParse; let obj; systemConfigParse = JSON.parse(sy ...

Error in Nuxt/TypeScript: Unable to retrieve this - TS2339: Property 'XXX' is not found on type

I encountered an issue while working with Nuxt.js and TypeScript. In my project, I rely on dependencies such as axios or nuxt-i18n. As an example, let's focus on Axios. I followed the configuration outlined in the official documentation for nuxt/axios ...

Tips for testing SSM's GetParameter with aws-sdk-client-mock in typescript

I'm looking to create a test case for the SSM GetParameterCommand request in TypeScript. Here is my code snippet: const region = "us-east-1" const awsSSMClient = new SSMClient({ region }) export async function fetchParam(parameterName: string, decry ...

Utilizing Angular 1.x to Embed HTML Content within an iFrame

I have HTML content coming from the API. How can I show it using an iframe? Is there a way to achieve something like { ..., template: "<h3 style="margin: 0px; padding: 0px; ..." ...} If you could provide a demo, that would be great. Thank you! ...

Encountering an Angular error while trying to use the command "npm run dev:ssr" to observe server-side rendering on the localhost

When I run this command, it starts listening on port 4200 but the page keeps loading without ever fully loading and shows this error in the command prompt: Unhandled Promise rejection: connect ECONNREFUSED 127.0.0.1:6379 ; Zone: <root> ; Task: Promis ...

AngularJS 2 TypeScript structure

My application includes a user service for managing user operations and an interface for the user object. user.service.ts import {Injectable} from 'angular2/core'; export interface User { name: string; email?: string; picture?: string; } ...

Creating custom components that encapsulate the functionality of Angular Material tabs component

I am aiming to incorporate the Angular Material tabs component within my shared components. Here is the component I'm attempting to wrap: Note: Each tab can display a component: <mat-tab-group> <mat-tab label="First"> Content ...

Using ngControl in a custom validator directive can lead to a problem of cyclic dependency

I'm currently working on developing a custom Angular 2 validator directive and I'm looking to inject NgControl in the process. Here's an example of what I have: @Directive({ selector: '[ngModel][customValidator]', providers: [ ...

Combining objects with arrays to create a single unified data structure

I am trying to merge two array objects to achieve a specific structure, similar to the example below. "feedBackList" : [ { "questionNo" : 1, "scoring" : "5" }, { ...

issue with angular and typescript

I'm currently working on developing an Angular 2 application that incorporates touch gestures using hammerjs. My goal is to merge the quickstarter application from: Angular 2 with the hammerjs application from: Hammerjs sample. However, I keep encou ...

The ongoing ESLint conundrum: Balancing between "Unused variable" and "Unknown type" errors when utilizing imports for type annotations

I've encountered a linting issue and I need some guidance on how to resolve it. Here's the scenario - when running $ yarn lint -v yarn run v1.22.4 $ eslint . -v v6.8.0 With plugins vue and @typescript-eslint, I have the following code in a .ts ...

Exploring the distinctions between the Decorator and Mediator design patterns when implemented in JavaScript

After delving into JavaScript patterns, I noticed some interesting differences between Angular 4 and Angular 1.x. Angular 4 employs new patterns that caught my attention. What specific patterns does Angular 4 utilize? Is it possible to incorporate the De ...

Using a nodejs module is causing an error in my code

I am dealing with a module like this: module Net.Server { var socket:dgram.Socket; [...] } Here is my app.ts file: var server:Net.Server = new Server(); However, when I include this line at the beginning of the first file: import dgram = requ ...

Angular: Trouble with images not displaying correctly in live environment

My images are located in the src/assets/images/ directory and I reference them in HTML like this: <img src="assets/images/penndot-logo-full.png" /> This setup works fine in development mode, but in production, it gives a 404 error. After building m ...

Troubleshooting problems with the Quora pixel after refreshing the page

Having a strange issue with the Quora pixel integration on Angular. I have added the script in the HTML code as follows: ! function(q, e, v, n, t, s) { if (q.qp) return; n = q.qp = function() { n.qp ? n.qp.apply(n, arguments) : n.queue.push ...

Error in Typescript for the prop types of a stateless React component

When reviewing my project, I came across the following lines of code that are causing a Typescript error: export const MaskedField = asField(({ fieldState, fieldApi, ...props }) => { const {value} = fieldState; const {setValue, set ...

Mastering the use of SVG icons in Angular 4+: A comprehensive guide

I have an icon.svg file with a collection of icons that I would like to use in my app, similar to how we display material icons. Does anyone have any ideas on how to include the file in the app and use these icons? I've tried looking for solutions a ...

The specified type '{ rippleColor: any; }' cannot be assigned to type 'ColorValue | null | undefined'

I've been diving into learning reactnative (expo) with typescript, but I've hit a roadblock. TypeScript keeps showing me the error message Type '{ rippleColor: any; }' is not assignable to type 'ColorValue | null | undefined' ...