We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to throw errors in modals using Flash Session?

Be more specific Paul please, what exactly do you want to achieve? I dont see any problem to pass flash messages via. JSON or HTML through AJAX and then dislay them.



93.7k
Accepted
answer

It should not be a problem at all. Here is made up example from my configuration using bootstrap.

// Controller
if ($this->request->isPost() AND $form->isValid($this->request->getPost())) {
    $result = $this->crudHelper->updateRecord($options);
    if ($result === true) {
        $this->flash->success($this->translations->admin->general->editSuccess);
        return $this->response->redirect($this->session->cms->previousPage);
    }
    $this->flash->error($result);
}
// Volt
{% if flash.has("success") or flash.has("warning") or flash.has("notice") or flash.has("error") %}
<div class="modal fade" tabindex="-1"" role="dialog" aria-labelledby="placeholderModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="placeholderModalLabel">Modal Heading</h4>
            </div> 
                <div class="modal-body">
                    {{ flash.output() }}
                </div> 
            <div class="modal-footer">     
                <button type="button" class="btn btn-default modal-footer-close" data-dismiss="modal">Modal button</button>
            </div>
        </div>
    </div>
</div>
<script>
    $('.modal').modal({
        show: true
    });    
</script>
{% endif %}