PHP Classes

File: src/Renderer/Error/HorizontalFormErrorRender.php

Recommend this page to a friend!
  Classes of Derek McLean   HTML Forms   src/Renderer/Error/HorizontalFormErrorRender.php   Download  
File: src/Renderer/Error/HorizontalFormErrorRender.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: 6 years ago
Size: 2,087 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
HorizontalFormErrorRender extends AbstractErrorRender implements ErrorRendererInterface
{
   
/**
     * @param FieldInterface $field
     * @return DOMElement
     */
   
public function render(FieldInterface $field)
    {
       
$helpBlock = $this->createElement('span');
       
$helpBlock->setAttribute('class', 'help-block');

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

       
$div = $this->createElement('div');
       
$div->setAttribute('class', 'col-sm-offset-2 col-sm-10');
       
$div->appendChild($helpBlock);
        return
$div;
    }

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

}