Commit 25881e70 authored by Mat's avatar Mat

Ajax, fonction d'affichage des erreurs 400

parent 730f14ed
......@@ -28,6 +28,7 @@ class Owner
* @var string
*
* @ORM\Column(name="Lastname", type="string", length=100)
* @Assert\NotBlank()
* @Assert\Length(
* min = 3,
* max = 50,
......@@ -41,6 +42,7 @@ class Owner
* @var string
*
* @ORM\Column(name="Firstname", type="string", length=100)
* @Assert\NotBlank()
* @Assert\Length(
* min = 3,
* max = 50,
......
......@@ -125,7 +125,7 @@
form.parentNode.classList.remove('hidden');
}
else if (this.status === 400) {
console.log('BAD REQUEST. DISPLAY ERRORS');
console.log(this.status + ' ' + this.statusText);
}
}
});
......@@ -151,7 +151,7 @@
reloadTableOwner();
}
else if (this.status === 400) {
console.log('BAD REQUEST. DISPLAY ERRORS');
console.log(this.status + ' ' + this.statusText);
}
}
});
......@@ -206,13 +206,10 @@
form.parentNode.classList.add('hidden');
} else if (this.status === 400) {
console.log('400 Bad Request');
} else if (this.status === 404) {
console.log('404 Not Found');
handleErrors(this);
} else if (this.status === 500) {
console.log('500 Internal Server Error');
console.log(this.status + ' ' + this.statusText);
}
}
......
......@@ -32,7 +32,26 @@ function deleteAJAX(url, callback)
xhr.send(null);
}
function handleErrors()
function handleErrors(response)
{
// 30 lignes pour gérer les erreurs du formulaire POST renvoyé par l'API
let message = response.status + '\n' + response.statusText + '\n\n';
let JSONObject = JSON.parse(response.responseText);
let fields = form.querySelectorAll('input[id^="owner"]');
for (let key in JSONObject)
{
for (let field of fields)
{
let fieldName = field.getAttribute('id').replace('owner_', '');
if (JSONObject[key].hasOwnProperty(fieldName)) {
for (let error of JSONObject[key][fieldName].errors) {
message += '* ' + error + '\n';
}
}
}
}
alert(message);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment