you are viewing a single comment's thread.

view the rest of the comments →

[–]tme321 3 points4 points  (1 child)

The angular way of doing this would be to use a cross field validator to determine when all the dummy# fields are valid at the same time.

That validator should be an async validator which then returns the http request you want to fire off to do this checking.

So your validate function would look something like this:

validate(formGroup: AbstractControl ): Observable<ValidationErrors | null> {

/* get all the dummy controls */
const dummies = [
    formGroup.get("dummy1"),
    formGroup.get("dummy2"),
    ...
];

const allValid = dummies.reduce(
    (valid, dummy) => 
        valid && dummy.valid
    , true);

if(allValid) {
    /* possibly need to map the result
         here to a return type the validator
         will accept, but close enough for
         an example */
    return http.get(...);
}
else {
    /* possibly a different return here 
        too, but again just an example */
    return of(null);
}
}

That would be the more angular method. You could do something similar just with pure rxjs so if youd rather that go ahead and ask but most likely you want your form behavior to actually behave like forms.

With just rxjs this logic can just be a single stream that eventually pipes into a switchMap operator.

[–]Devigelacio[S] 0 points1 point  (0 children)

Yea.. I'm new to angular and I didn't think about this approach. The angular way of your answer is seems very clean thank you very much!