Overview

Namespaces

  • Faulancer
    • Controller
    • Exception
    • Form
      • Validator
        • Type
    • Helper
      • Reflection
    • Http
    • Mail
    • ORM
      • User
    • Service
      • Factory
    • ServiceLocator
    • Session
    • View
      • Helper

Classes

  • AssetList
  • Block
  • CodeBlock
  • EndBlock
  • EscapeString
  • FlashBag
  • FormData
  • FormError
  • GenerateCsrfToken
  • Highlighter
  • ParentTemplate
  • RenderBlock
  • RenderView
  • Route
  • Translate
  • User
  • Overview
  • Namespace
  • Class
 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: 77: 78: 79: 80: 81: 82: 
<?php
/**
 * Class FormError | FormError.php
 * @package Faulancer\View\Helper
 * @author  Florian Knapp <office@florianknapp.de>
 */
namespace Faulancer\View\Helper;

use Faulancer\Service\SessionManagerService;
use Faulancer\View\AbstractViewHelper;
use Faulancer\View\ViewController;

/**
 * Class FormError
 * @method $this->view->translate()
 */
class FormError extends AbstractViewHelper
{

    /** @var ViewController */
    protected $view;

    /** @var string */
    protected $field;

    /** @var SessionManagerService */
    protected $sessionManager;

    /**
     * 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;

        $this->sessionManager = $this->getServiceLocator()->get(SessionManagerService::class);

        return $this;
    }

    /**
     * Get an error for specific field
     *
     * @return string
     */
    public function get()
    {
        $error = $this->sessionManager->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 $this->sessionManager->hasFlashbagErrorsKey($this->field);
    }

}
API documentation generated by ApiGen