Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Redirect to another view

Hi, First I write that I had a problem to set good title so I am sorry.

I explain it below:

I have form on url: /projects/view/:id which send a file but controller action is in DataController in method save: /data/save. I would like to get errors from this form and send it to view: /projects/view/:id.

How can I get this solution.

Thanks, nansss

PS. Sorry for my bad english.

edited Oct '16

You can use flash messages for this:

// Flash messages (Service definition)
$di->set('flash', function() {
    $flash = new \Phalcon\Flash\Session();
    return $flash;
});

// Controller code for example
if ($error) {
    $this->flash->error('Error message');
} else {
    $this->flash->success('Success message' );
}
return $this->response->redirect($this->request->getHTTPReferer());


1.1k

So how can i get it in view?

I don't understand, just if validation failes render view from /projects/view/:id with error messages. What's a problem ?



1.1k

I have a problem with understanding.

I added to service.php

$di->set('flash', function() {
    $flash = new \Phalcon\Flash\Session();
    return $flash;
});

and

class DataController extends BaseController
{
    public function saveAction()
    {
        $this->flash->error('error');
        return $this->response->redirect($this->request->getHTTPReferer());
    }
}

Redirect works good but I don't see an error.

Did you add them to your view file? Should be something like:

{% if flash.has('success') or flash.has('warning') or flash.has('notice') or flash.has('error') %}
<div id="flash-messages">
    <button type="button" class="close" onclick="$(this).parent().remove();"><i aria-hidden="true">&times;</i></button> 
    {{ flash.output() }}
</div> 
{% endif %}


107.4k
Accepted
answer
edited Oct '16

I thought you wanted error messages, not some generic flash message which dont tell you nothing. https://docs.phalconphp.com/pl/latest/reference/views.html#picking-views Just pick /projects/view/:id view and change this view to render errors if any and that's it. Also just better to make a same route for both displaying form and saving it. Like one is get, second is put :)



1.1k

Ok. I think now it's work.

View code:

<?php
    if($this->flashSession->has('success')):
    ?>
        <div class="alert alert-success alert-dismissable">
            <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
            <?php echo $this->flash->output(); ?>
        </div>
    <?php
    endif;
    if($this->flashSession->has('error')):
    ?>
    <div class="alert alert-danger alert-dismissable">
        <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
        <?php echo $this->flash->output(); ?>
    </div>
    <?php
    endif;
    ?>

Thanks guys.

edited Oct '16

I would just like more as user to know what i did wrong instead of just some generic message that i provided wrong data :)

Also use volt :D