53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
|
|
export function displayFormErrors(error: ValidationErrors, form: HTMLElement|null = null) {
|
|
if (error.response && error.response.status === 422) {
|
|
const errors = error.response.data.errors
|
|
const formBase = (form) ? form : document.body
|
|
Object.entries(errors).forEach(([key, value]) => {
|
|
displayError(key, value, formBase)
|
|
})
|
|
// Object.keys(errors).forEach(key => {
|
|
// displayError(key, errors[key], formBase)
|
|
// })
|
|
}
|
|
}
|
|
|
|
interface ValidationErrors {
|
|
response: {
|
|
status: number,
|
|
data: {
|
|
errors: object
|
|
}
|
|
}
|
|
}
|
|
|
|
export function displayError(key: string, message: string, form: HTMLElement|null = null) {
|
|
const formBase = (form) ? form : document
|
|
const input = formBase.querySelector(`input[name="${key}"], select[name="${key}"], textarea[name="${key}"]`)
|
|
if (input) {
|
|
const formControl = input.closest('.form-control')
|
|
if (formControl) {
|
|
formControl.classList.add('invalid-control')
|
|
const errorMessage: HTMLElement|null = formControl.querySelector('.error-message')
|
|
if (errorMessage) {
|
|
errorMessage.innerText = message
|
|
}
|
|
if(key === 'form_info') {
|
|
formControl.classList.remove('hidden')
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export function cleanErrorsForm(form = null) {
|
|
const formBase = (form) ? form : document.body
|
|
const inputsErrors = formBase.querySelectorAll('.invalid-control')
|
|
inputsErrors.forEach(input => {
|
|
input.classList.remove('invalid-control')
|
|
const errorMessage: HTMLElement|null = input.querySelector('.error-message')
|
|
if (errorMessage) {
|
|
errorMessage.innerText = ''
|
|
}
|
|
})
|
|
}
|