mirror of https://github.com/ghostfolio/ghostfolio
Browse Source
* Add form validation against DTO for activity and account * Update changelogpull/3281/head
committed by
GitHub
4 changed files with 94 additions and 15 deletions
@ -0,0 +1,38 @@ |
|||||
|
import { FormGroup } from '@angular/forms'; |
||||
|
import { plainToInstance } from 'class-transformer'; |
||||
|
import { validate } from 'class-validator'; |
||||
|
|
||||
|
export async function validateObjectForForm<T>({ |
||||
|
classDto, |
||||
|
form, |
||||
|
ignoreFields = [], |
||||
|
object |
||||
|
}: { |
||||
|
classDto: { new (): T }; |
||||
|
form: FormGroup; |
||||
|
ignoreFields?: string[]; |
||||
|
object: T; |
||||
|
}): Promise<void> { |
||||
|
const objectInstance = plainToInstance(classDto, object); |
||||
|
const errors = await validate(objectInstance as object); |
||||
|
|
||||
|
const nonIgnoredErrors = errors.filter(({ property }) => { |
||||
|
return !ignoreFields.includes(property); |
||||
|
}); |
||||
|
|
||||
|
if (nonIgnoredErrors.length === 0) { |
||||
|
return Promise.resolve(); |
||||
|
} |
||||
|
|
||||
|
for (const { constraints, property } of nonIgnoredErrors) { |
||||
|
const formControl = form.get(property); |
||||
|
|
||||
|
if (formControl) { |
||||
|
formControl.setErrors({ |
||||
|
validationError: Object.values(constraints)[0] |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return Promise.reject(nonIgnoredErrors); |
||||
|
} |
Loading…
Reference in new issue