My approach was quite successful with the utilization of bootstrap's poppver
feature. It's worth mentioning that I implement Laravel along with bootstrap and jQuery in my projects. To pinpoint the fields with errors, I utilize a PHP variable called $errors
, which contains the names of input or textarea fields.
https://i.stack.imgur.com/Jvy3m.png
jQuery is used to add data- attributes to the error-prone fields
{{-- CUSTOM POPOVERS --}}
<script>
jQuery(function(){
/**
* The script is designed to highlight erroneous fields, displaying popover error messages within form fields
* Please ensure this script remains on this page instead of a separate JS file as it relies on the $error PHP variable
*/
errors_json = '<?php if (isset($errors)) echo json_encode($errors->toArray()); else echo []; ?>';
errors = $.parseJSON(errors_json);
$.each(errors, function (i, val) {
var input = $('input[name=' + i + ']'+','+'textarea[name=' + i + ']');
input.attr('title', 'Errors').attr('data-toggle', 'popover').attr('data-trigger', 'focus').attr('data-placement', 'top').attr('data-content', val);
input.addClass('highlight-error').popover();
});
});
</script>
Additionally, I employed CSS to achieve the red color in bootstrap popovers for indicating errors
.popover {
background-color: #dd4b39;
border: 1px solid #b33f2d;
border: 1px solid rgba(221, 75, 57, 0.25);
border-radius: 0;
color: white;
}
.popover-title {
background-color: #b33f2d;
border-bottom: 1px solid #ebebeb;
border-radius: 0;
}
.popover-content{
}
.popover.top > .arrow {
border-top-color: #dd4b39;
border-top-color: rgba(221, 75, 57, 0.25);
}
.popover.top > .arrow:after {
border-top-color: #dd4b39;
}
.popover.right > .arrow {
border-right-color: #dd4b39;
border-right-color: rgba(221, 75, 57, 0.25);
}
.popover.right > .arrow:after {
border-right-color: #dd4b39;
}
.popover.bottom > .arrow {
border-bottom-color: #dd4b39;
border-bottom-color: rgba(221, 75, 57, 0.25);
}
.popover.bottom > .arrow:after {
border-bottom-color: #dd4b39;
}
.popover.left > .arrow {
border-left-color: #dd4b39;
border-left-color: rgba(221, 75, 57, 0.25);
}
.popover.left > .arrow:after {
border-left-color: #dd4b39;
}