import { HttpErrorResponse } from '@angular/common/http' ; import { FormGroup } from '@angular/forms' ; export function handleServerSideValidation ( error , form ) { if ( error . status !== 422 ) { return undefined ; } const unhandledErrors = [ ] ; const validationError = error . error ?. errors ; Object . keys ( validationError || { } ) . forEach ( element => { const formControl = form . get ( element ) ; if ( formControl ) { formControl . setErrors ( { serverSideError : validationError [ element ] . join ( '' ) } ) ; } else { unhandledErrors . push ( validationError [ element ] . join ( '' ) ) ; } } ) ; if ( unhandledErrors . length ) { return unhandledErrors . join ( ) ; } return undefined ; }