Do something like this:
<?php
namespace Helper;
/**
* Description of Flash
*
* @author Webvizitky, Softdream <[email protected]>,<[email protected]>
* @copyright (c) 2013, Softdream, Webvizitky
* @package name
* @category name
*
*/
class Flash {
protected static $instance;
/**
* @var \Phalcon\Flash flash adapter
*/
protected $adapter;
private function __construct() {
}
/**
* Get created instance of object
* @return \Helper\Flash Description
*/
protected static function getInstance(){
if(!self::$instance){
self::$instance = new self();
}
return self::$instance;
}
public function setAdapter($adapter){
$this->adapter = $adapter;
}
/**
* @return \Phalcon\Flash Description
*/
public function getAdapter(){
return $this->adapter;
}
public function init(){
if(!$this->adapter){
throw new \Exception('Adapter is not set.');
}
}
public static function registerAdapter(\Phalcon\Flash $adapter){
$instance = self::getInstance();
if(!$instance->getAdapter()){
$instance->setAdapter($adapter);
}
}
public static function warning($message, \Phalcon\Flash $adapter = null){
$instance = self::getInstance();
if($adapter && !$instance->getAdapter()){
$instance->registerAdapter($adapter);
}
$instance->init();
$adapter = $instance->getAdapter();
foreach($message as $msg){
$adapter->warning($msg);
}
}
public static function notice($message, \Phalcon\Flash $adapter = null){
$instance = self::getInstance();
if($adapter && !$instance->getAdapter()){
$instance->registerAdapter($adapter);
}
$instance->init();
$adapter = $instance->getAdapter();
foreach($message as $msg){
$adapter->notice($msg);
}
}
public static function success($message, \Phalcon\Adapter $adapter = null){
$instance = self::getInstance();
if($adapter && !$instance->getAdapter()){
$instance->registerAdapter($adapter);
}
$instance->init();
$adapter = $instance->getAdapter();
foreach($message as $msg){
$adapter->success($msg);
}
}
public static function error($message, \Phalcon\Flash $adapter = null){
$instance = self::getInstance();
if($adapter && !$instance->getAdapter()){
$instance->registerAdapter($adapter);
}
$instance->init();
$adapter = $instance->getAdapter();
foreach($message as $msg){
$adapter->error($msg);
}
}
}
And just move $adapter->error(), success(), waring(), notice() below to the loop and in the loop set join string like this:
foreach($message as $msg){
$msgs .= $msg.'<br />';
}
$adapter->notice($msg);
So with this solution you can throw more then one errors in one flash messages. And how to use it:
if(!$validEmail){
$err[] = 'My first error';
}
$err[] = 'My second error';
\Helper\Flash::notice($err,$this->flash);
I hope this help.
You can do it with singlenton too, and appending errors into singlenton object, then throw it.
With this solution you can check, if the error message has been set before or not, with just create a help variable to store hash of messages.