This is how I json_encode validation errors to View:
if ($this->form_validation->run() == FALSE) { //if validation does not run
$errors = $this->form_validation->error_array();
echo json_encode(['error' => true, 'errors' => $errors]);
}
if (res.errors) {
var errorMsgs = "";
errorMsgs += res.errors.name1 ? res.errors.name1 + "<br/>" : ""; //only if have an error msg.
errorMsgs += res.errors.name2 ? res.errors.name2 + "<br/>" : ""; //only if have an error msg.
errorMsgs += res.errors.name3 ? res.errors.name3 : ""; //only if have an error msg.
// SweetAlert
swal({
text: errorMsgs // error msg
});
}
This is Name1 field error message.<br/>This is Name2 field error message.<br/>This is Name3 field error message.
found a solution myself. :D
just use "\n" instead of "<br>"!
errorMsgs += res.errors.name1 ? res.errors.name1 + "\n" : "";
Case https://sweetalert.js.org/ VS https://sweetalert2.github.io/
For : https://sweetalert.js.org/
As documentation says : html is no longer used.
https://sweetalert.js.org/docs/
Instead use the content object.
swal({
content: "input",
});
For : https://sweetalert2.github.io/
if you are using this this SweetAlert2 plugin from here
https://sweetalert2.github.io/
You can get your desired result with html
swal({
title: "<i>Title</i>",
html: 'A custom message.</br> jkldfjkjdklfjlk',
});
using second plugin :
<script src="https://unpkg.com/[email protected]/dist/sweetalert2.all.js"></script>
you can get with html:
if (res.errors) {
var errorMsgs = "";
errorMsgs += res.errors.name1 ? res.errors.name1 + "<br/>" : ""; //only if have an error msg.
errorMsgs += res.errors.name2 ? res.errors.name2 + "<br/>" : ""; //only if have an error msg.
errorMsgs += res.errors.name3 ? res.errors.name3 : ""; //only if have an error msg.
// SweetAlert
swal({
html: errorMsgs // error msg
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With