PHP Classes

File: src/Renderer/Error/DefaultErrorRender.php

Recommend this page to a friend!
  Packages of Derek McLean   HTML Forms   src/Renderer/Error/DefaultErrorRender.php   Download  
File: src/Renderer/Error/DefaultErrorRender.php
Role: Class source
Content type: text/plain
Description: Class source
Class: HTML Forms
Generate and validate submitted HTML forms
Author: By
Last change:
Date: 8 years ago
Size: 1,946 bytes
 

 

Contents

Class file image Download
<?php
/**
 * User: delboy1978uk
 * Date: 04/12/2016
 * Time: 23:36
 */

namespace Del\Form\Renderer\Error;


use Del\Form\Field\FieldInterface;
use DOMElement;
use DOMText;

class DefaultErrorRender extends AbstractErrorRender implements ErrorRendererInterface
{
    /**
     * @param FieldInterface $field
     * @return DOMElement
     */
    public function render(FieldInterface $field)
    {
        $helpBlock = $this->getDom()->createElement('span');
        $helpBlock->setAttribute('class', 'help-block');

        if ($this->shouldRender($field)) {
            $helpBlock = $field->hasCustomErrorMessage()
                ? $this->addCustomErrorMessage($helpBlock, $field)
                : $this->addErrorMessages($helpBlock, $field);
        }
        return $helpBlock;
    }

    /**
     * @param DOMElement $helpBlock
     * @param FieldInterface $field
     * @return DOMElement
     */
    private function addCustomErrorMessage(DOMElement $helpBlock, FieldInterface $field)
    {
        $message = $field->getCustomErrorMessage();
        $text = $this->createText($message);
        $helpBlock->appendChild($text);
        return $helpBlock;
    }

    /**
     * @param DOMElement $helpBlock
     * @param FieldInterface $field
     * @return DOMElement
     */
    private function addErrorMessages(DOMElement $helpBlock, FieldInterface $field)
    {
        $messages = $field->getMessages();

        foreach ($messages as $message) {
            $helpBlock = $this->appendMessage($helpBlock, $message);
        }
        return $helpBlock;
    }

    /**
     * @param DOMElement $helpBlock
     * @param $message
     * @return DOMElement
     */
    private function appendMessage(DOMElement $helpBlock, $message)
    {
        $text = $this->createText($message);
        $br = $this->createLineBreak();
        $helpBlock->appendChild($text);
        $helpBlock->appendChild($br);
        return $helpBlock;
    }

}