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

the flash message can't be show

Everyone can give a flash demo to show the message in temp file, thanks



26.3k

So I am using session flash messages.

In my bootstrap index.php file, when defining services:


use Phalcon\Flash\Session as Flash;

$di = new \Phalcon\DI\FactoryDefault();

//Set up the flash service with classes from Zurb Foundation CSS framework
    $di->set('flash', function() {
        return new Flash(array(
            'error' => 'alert-box warning',
            'success' => 'alert-box success',
            'notice' => 'alert-box info',
            'warning' => 'alert-box warning',
        ));
    });

//bla bla rest of bootstrap

In my controller:


$this->flash->error('WTF, some error occured!');

In my view:


<?php echo $this->getContent() ?>

<div class="row">
    <div class="small-12 medium-12 large-12 columns">
        <?php $this->flash->output(); ?>
    </div>
</div>


1.7k

Conradaek, Thank you very much first, but if it has two errors or more, it can't do it.

if this function can add a params like this : $this->flash->output('error1') is more better.



26.3k

Hmmm I don't understand what your problem really is. Let's say there are 10 messages stored by flash service.

What you want to do with them?

  • Output all messages in one place?

This is simply done by the code provided by me in my former post.

Controller:


$this->flash->error('WTF, some error occured!');
$this->flash->error('Another error!');
$this->flash->success('And one success!');

In your view:


echo $this->getContent();
$this->flash->output();

This way all messages will be generated. The code will produce:


<div data-alert class="alert-box warning">WTF, some error occured!</div>
<div data-alert class="alert-box warning">Another error!</div>
<div data-alert class="alert-box success">And one success!</div>
  • Output only one message?

You can use method outputMessage (string $type, string $message) described at the end of https://docs.phalcon.io/en/latest/api/Phalcon_Flash_Session.html. Or you can extend flash service and build your own.

  • Want to output validation messages near to form elements? That is another story:)


1.7k

Cool! Thank you for all, I will look at about validation messages, I am newly for phalcon, Thanks again



17.0k

Conradaek, you wrote : Want to output validation messages near to form elements? That is another story:)

Can you please tell me that story ? And best practice advice :D



26.3k

@dextrality

Hi! I am beginner so my expertise is not huge. I have used such expression to highlight that this is completely diffirent topic than flash messages. I am using Zurb Foundation as a CSS framework in my project. Forms are described here.

I have made a base class for forms with a methods to render form elements. You can read this topic in the docs https://docs.phalcon.io/en/latest/reference/forms.html#rendering-forms


namespace Conradaek\Common\Library\Forms;

use Phalcon\Forms\Element\Text,
    Phalcon\Forms\Element\Select;

class BaseForm extends \Phalcon\Forms\Form {

    public function renderElement($field) {

        //get a form element
        $element = $this->get($field);

        $message = $this->getMessagesFor($field)->offsetGet(0);

        //add Zurb Foundation's error class to an element
        if($message){
            $element->setAttribute("class","error");
            echo '<label class="error">';
        }
        else {
            echo '<label>';
        }

        //prints label
        echo $element->getLabel();

        //prints element
        echo $element;
        echo '</label>';

        if($message) {
            echo '<small class="error">';
            //Print a message
            echo $message;
            echo '</small>';
        }
    }

    public function renderForm() {

        //get form elements
        $elements = $this->getElements();

        //render all elements
        foreach($elements as $i => $element) {
            $this->renderElement($element->getName());
        }
    }
}


81.1k

Hello good I'm also doing my thesis and would like to use Zurb Phalcon Foundation Framework but'm reading forums and time consuming and I do not have any project that already uploaded so that it can analyze Use