Skip to content

Commit

Permalink
Merge branch 'dev-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
delboy1978uk committed Dec 11, 2016
2 parents ca59d50 + 720fdd2 commit 30f23db
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 18 deletions.
22 changes: 22 additions & 0 deletions src/Field/FieldAbstract.php
Expand Up @@ -37,6 +37,9 @@ abstract class FieldAbstract implements FieldInterface
/** @var string $label */
private $label;

/** @var bool $required */
private $required;

use HasAttributesTrait;

/**
Expand All @@ -48,6 +51,7 @@ abstract public function init();

public function __construct($name, $value = null)
{
$this->required = false;
$this->filterCollection = new FilterCollection();
$this->validatorCollection = new ValidatorCollection();
$this->renderer = new TextRender();
Expand Down Expand Up @@ -274,4 +278,22 @@ public function setRenderer(FieldRendererInterface $renderer)
$this->renderer = $renderer;
return $this;
}

/**
* @return boolean
*/
public function isRequired()
{
return $this->required;
}

/**
* @param boolean $required
* @return FieldAbstract
*/
public function setRequired($required)
{
$this->required = $required;
return $this;
}
}
12 changes: 12 additions & 0 deletions src/Field/FieldInterface.php
Expand Up @@ -10,6 +10,7 @@
use Del\Form\Collection\FilterCollection;
use Del\Form\Collection\ValidatorCollection;
use Del\Form\Filter\FilterInterface;
use Del\Form\Renderer\Field\SelectRender;
use Del\Form\Validator\ValidatorInterface;
use Del\Form\Renderer\Field\FieldRendererInterface;
use Exception;
Expand Down Expand Up @@ -143,4 +144,15 @@ public function getRenderer();
public function setRenderer(FieldRendererInterface $renderer);

public function init();

/**
* @param bool $required
* @return $this
*/
public function setRequired($required);

/**
* @return bool
*/
public function isRequired();
}
17 changes: 16 additions & 1 deletion src/Renderer/AbstractFormRenderer.php
Expand Up @@ -15,6 +15,7 @@
use Del\Form\Renderer\Error\ErrorRendererInterface;
use DOMDocument;
use DomElement;
use DOMText;

abstract class AbstractFormRenderer implements FormRendererInterface
{
Expand Down Expand Up @@ -129,7 +130,7 @@ private function processFields(FieldCollection $fields)
public function renderError()
{
$errorBlock = null;
if (!$this->field->isValid() && $this->displayErrors === true) {
if ($this->errorRenderer->shouldRender($this->field) && $this->displayErrors === true) {
$this->block->setAttribute('class', 'has-error ');
$errorBlock = $this->errorRenderer->render($this->field);
}
Expand All @@ -143,6 +144,20 @@ protected function createLabelElement()
{
$label = $this->dom->createElement('label');
$label->setAttribute('for', $this->field->getId());
if ($this->field->isRequired()) {
$label = $this->addRequiredAsterisk($label);
}
return $label;
}


public function addRequiredAsterisk(DomElement $label)
{
$span = $this->dom->createElement('span');
$span->setAttribute('class', 'text-danger');
$text = new DOMText('* ');
$span->appendChild($text);
$label->appendChild($span);
return $label;
}

Expand Down
10 changes: 10 additions & 0 deletions src/Renderer/Error/AbstractErrorRender.php
Expand Up @@ -8,6 +8,7 @@
namespace Del\Form\Renderer\Error;

use DOMDocument;
use Del\Form\Field\FieldInterface;

abstract class AbstractErrorRender implements ErrorRendererInterface
{
Expand All @@ -22,4 +23,13 @@ public function __construct(DOMDocument $dom)
{
$this->dom = $dom;
}

/**
* @param FieldInterface $field
* @return bool
*/
public function shouldRender(FieldInterface $field)
{
return !$field->isValid() && ($field->isRequired() || !empty($field->getValue()));
}
}
8 changes: 4 additions & 4 deletions src/Renderer/Error/DefaultErrorRender.php
Expand Up @@ -23,10 +23,10 @@ public function render(FieldInterface $field)
$helpBlock = $this->dom->createElement('span');
$helpBlock->setAttribute('class', 'help-block');

if ($field->hasCustomErrorMessage()) {
$helpBlock = $this->addCustomErrorMessage($helpBlock, $field);
} else {
$helpBlock = $this->addErrorMessages($helpBlock, $field);
if ($this->shouldRender($field)) {
$helpBlock = $field->hasCustomErrorMessage()
? $this->addCustomErrorMessage($helpBlock, $field)
: $this->addErrorMessages($helpBlock, $field);
}
return $helpBlock;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Renderer/Error/ErrorRendererInterface.php
Expand Up @@ -17,4 +17,10 @@ interface ErrorRendererInterface
* @return mixed
*/
public function render(FieldInterface $field);

/**
* @param FieldInterface $field
* @return bool
*/
public function shouldRender(FieldInterface $field);
}
8 changes: 4 additions & 4 deletions src/Renderer/Error/HorizontalFormErrorRender.php
Expand Up @@ -23,10 +23,10 @@ public function render(FieldInterface $field)
$helpBlock = $this->dom->createElement('span');
$helpBlock->setAttribute('class', 'help-block');

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

$div = $this->dom->createElement('div');
Expand Down
3 changes: 0 additions & 3 deletions src/Renderer/HorizontalFormRenderer.php
Expand Up @@ -7,9 +7,6 @@

namespace Del\Form\Renderer;

use Del\Form\Field\CheckBox;
use Del\Form\Field\Radio;
use Del\Form\Field\Submit;
use Del\Form\Renderer\Error\HorizontalFormErrorRender;
use DOMElement;
use DOMText;
Expand Down
12 changes: 7 additions & 5 deletions tests/unit/Del/FormTest.php
Expand Up @@ -315,11 +315,12 @@ public function testRenderWithErrors()
$form = new Form('testform');
$validator = new ValidatorAdapterZf(new NotEmpty());
$text = new Text('text');
$text->addValidator($validator);
$text->addValidator($validator)
->setRequired(true);
$form->addField($text);
$form->populate(['text' => null]);
$html = $form->render();
$this->assertEquals('<form name="testform" method="post" id="testform"><div class="has-error form-group"><label for=""></label><input name="text" type="text" class="form-control"><span class="help-block">Value is required and can\'t be empty<br></span></div></form>'."\n", $html);
$this->assertEquals('<form name="testform" method="post" id="testform"><div class="has-error form-group"><label for=""><span class="text-danger">* </span></label><input name="text" type="text" class="form-control"><span class="help-block">Value is required and can\'t be empty<br></span></div></form>'."\n", $html);
}


Expand All @@ -328,12 +329,13 @@ public function testRenderWithCustomErrors()
$form = new Form('testform');
$validator = new ValidatorAdapterZf(new NotEmpty());
$text = new Text('text');
$text->addValidator($validator);
$text->setCustomErrorMessage('This can\'t be empty!');
$text->addValidator($validator)
->setCustomErrorMessage('This can\'t be empty!')
->setRequired(true);
$form->addField($text);
$form->populate(['text' => null]);
$html = $form->render();
$this->assertEquals('<form name="testform" method="post" id="testform"><div class="has-error form-group"><label for=""></label><input name="text" type="text" class="form-control"><span class="help-block">This can\'t be empty!</span></div></form>'."\n", $html);
$this->assertEquals('<form name="testform" method="post" id="testform"><div class="has-error form-group"><label for=""><span class="text-danger">* </span></label><input name="text" type="text" class="form-control"><span class="help-block">This can\'t be empty!</span></div></form>'."\n", $html);
}

public function testGetAndSetRenderer()
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/Del/Renderer/HorizontalFormRendererTest.php
Expand Up @@ -68,10 +68,12 @@ public function testRenderHorizontalFormWithErrors()
$email->setLabel('Email');
$email->setPlaceholder('Email');
$email->setCustomErrorMessage('wtf');
$email->setRequired(true);
$password = new Text\Password('password');
$password->setId('password');
$password->setLabel('Password');
$password->setPlaceholder('Password');
$password->setRequired(true);
$remember = new Radio('choose');
$remember->setId('choose');
$remember->setLabel('Choose');
Expand All @@ -87,6 +89,6 @@ public function testRenderHorizontalFormWithErrors()
// Render the form
$html = $form->render();

$this->assertEquals('<form class="form-horizontal" name="test" method="post" id="test"><div class="has-error form-group"><label for="email" class="col-sm-2 control-label">Email</label><div class="col-sm-10"><input name="email" type="email" class="form-control" id="email" placeholder="Email"></div><div class="col-sm-offset-2 col-sm-10"><span class="help-block">wtf</span></div></div><div class="has-error form-group"><label for="password" class="col-sm-2 control-label">Password</label><div class="col-sm-10"><input name="password" type="password" class="form-control" id="password" placeholder="Password"></div><div class="col-sm-offset-2 col-sm-10"><span class="help-block">Value is required and can\'t be empty<br></span></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><div class="radio"><label for="choose"><input name="choose" type="radio" id="choose">Choose</label></div></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><input name="submit" value="Sign in" type="submit" class="btn btn-primary"></div></div></form>'."\n", $html);
$this->assertEquals('<form class="form-horizontal" name="test" method="post" id="test"><div class="has-error form-group"><label for="email" class="col-sm-2 control-label"><span class="text-danger">* </span>Email</label><div class="col-sm-10"><input name="email" type="email" class="form-control" id="email" placeholder="Email"></div><div class="col-sm-offset-2 col-sm-10"><span class="help-block">wtf</span></div></div><div class="has-error form-group"><label for="password" class="col-sm-2 control-label"><span class="text-danger">* </span>Password</label><div class="col-sm-10"><input name="password" type="password" class="form-control" id="password" placeholder="Password"></div><div class="col-sm-offset-2 col-sm-10"><span class="help-block">Value is required and can\'t be empty<br></span></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><div class="radio"><label for="choose"><input name="choose" type="radio" id="choose">Choose</label></div></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><input name="submit" value="Sign in" type="submit" class="btn btn-primary"></div></div></form>'."\n", $html);
}
}

0 comments on commit 30f23db

Please sign in to comment.