first commit
This commit is contained in:
40
resources/js/utilities/form.ts
Normal file
40
resources/js/utilities/form.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
export function displayFormErrors(error: any, form: HTMLElement|null = null) {
|
||||
if (error.response && error.response.status === 422) {
|
||||
let errors = error.response.data.errors
|
||||
const formBase = (form) ? form : document.body
|
||||
Object.keys(errors).forEach(key => {
|
||||
displayError(key, errors[key], formBase)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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 = ''
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user