As a novice web developer, I recently completed my first project using Vue and Laravel 8.
However, when trying to access my API, I encountered an error message: {"message":"The given data was invalid.","errors":{"email":["This email already exists."],"login":["This login is already taken."]}}
I have included the following code:
axios.post(this.$apiAdress + '/api/administrators?token=' + localStorage.getItem("api_token"),
self.record
)
.then(function (response) {
if (response.data.status == 'success') {
Swal.fire(
'Success',
'Record added successfully!',
'success'
)
} else {
Swal.fire(
'Error',
response,
'error'
)
}
this.$router.replace({path: '/administrators/create'});
}).catch(function (error) {
if (error.response.data.message == 'The given data was invalid.') {
Swal.fire(
'Error',
'Please fill in all required fields!',
'error'
)
window.scrollTo({top: 0});
}
The line: error.response.data.message indicates the presence of errors.
My goal is to display these errors using Swal:
Swal.fire(
'Error',
'.... Here goes the list of errors ....',
'error'
)
For instance:
Swal.fire(
'Error',
'This email already exists. <br/> This login is already taken.',
'error'
)
How can I achieve this?
Your assistance would be greatly appreciated.