1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76:
<?php
/**
* Class FormError | FormError.php
* @package Faulancer\View\Helper
* @author Florian Knapp <office@florianknapp.de>
*/
namespace Faulancer\View\Helper;
use Faulancer\Session\SessionManager;
use Faulancer\View\AbstractViewHelper;
use Faulancer\View\ViewController;
/**
* Class FormError
*/
class FormError extends AbstractViewHelper
{
/** @var ViewController */
protected $view;
/** @var string */
protected $field;
/**
* Initializing form error helper
*
* @param ViewController $view
* @param string $field
* @return $this
*/
public function __invoke(ViewController $view, string $field)
{
$this->view = $view;
$this->field = $field;
return $this;
}
/**
* Get an error for specific field
*
* @return string
*/
public function get()
{
$error = SessionManager::instance()->getFlashbagError($this->field);
$result = '';
if (!empty($error)) {
$result = '<div class="form-error ' . $this->field . '">';
foreach ($error as $err) {
$result .= '<span>' . $this->view->translate($err['message']) . '</span>';
}
$result .= '</div>';
}
return $result;
}
/**
* Check if a field name has an error
*
* @return bool
*/
public function has()
{
return SessionManager::instance()->hasFlashbagErrorsKey($this->field);
}
}