Skip to content

Commit

Permalink
fix(validator): always emit value
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Balet committed Jul 17, 2024
1 parent 79b4657 commit f485fdb
Showing 1 changed file with 2 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import { UntypedFormControl } from '@angular/forms'
import { FormControl } from '@angular/forms'
import { parsePhoneNumber, PhoneNumber } from 'libphonenumber-js'

export const phoneNumberValidator = (control: UntypedFormControl) => {
export const phoneNumberValidator = (control: FormControl) => {
const error = { validatePhoneNumber: true }
let numberInstance: PhoneNumber

if (control.value) {
try {
numberInstance = parsePhoneNumber(control.value)
} catch (e) {
control.setValue(null)
return error
}

if (numberInstance && !numberInstance.isValid()) {
control.setValue(null)
if (!control.touched) {
control.markAsTouched()
}
return error
}
}

return null
}

0 comments on commit f485fdb

Please sign in to comment.