Skip to content

Commit

Permalink
Merge branch 'stayallive-patch-1'. Improve Boostrap4 Error Messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
claar committed Jan 30, 2019
2 parents 1a0349f + 3834c22 commit c60a32e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Former/Form/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ protected function getHelp()
// Replace help text with error if any found
$errors = $this->app['former']->getErrors();
if ($errors and $this->app['former']->getOption('error_messages')) {
$inline = $this->app['former.framework']->createHelp($errors);
$inline = $this->app['former.framework']->createValidationError($errors);
}

return join(null, array($inline, $block));
Expand Down
27 changes: 21 additions & 6 deletions src/Former/Framework/TwitterBootstrap4.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ class TwitterBootstrap4 extends Framework implements FrameworkInterface
* @var array
*/
protected $states = array(
'has-warning',
'has-error',
'has-success',
'is-invalid',
);

/**
Expand Down Expand Up @@ -158,7 +156,7 @@ public function filterFieldClasses($classes)
*/
public function errorState()
{
return 'has-error';
return 'is-invalid';
}

/**
Expand Down Expand Up @@ -241,6 +239,10 @@ public function getFieldClasses(Field $field, $classes)
$classes[] = 'form-control';
}

if ($this->app['former']->getErrors($field->getName())) {
$classes[] = $this->errorState();
}

return $this->addClassesToField($field, $classes);
}

Expand Down Expand Up @@ -334,7 +336,20 @@ public function getActionClasses()
*/
public function createHelp($text, $attributes = array())
{
return Element::create('span', $text, $attributes)->addClass('form-text text-muted');
return Element::create('small', $text, $attributes)->addClass('text-muted');
}

/**
* Render an validation error text
*
* @param string $text
* @param array $attributes
*
* @return string
*/
public function createValidationError($text, $attributes = array())
{
return Element::create('div', $text, $attributes)->addClass('invalid-feedback');
}

/**
Expand All @@ -347,7 +362,7 @@ public function createHelp($text, $attributes = array())
*/
public function createBlockHelp($text, $attributes = array())
{
return Element::create('p', $text, $attributes)->addClass('form-text text-muted');
return Element::create('small', $text, $attributes)->addClass('form-text text-muted');
}

/**
Expand Down
20 changes: 19 additions & 1 deletion src/Former/Traits/Framework.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php
namespace Former\Traits;

use Former\Interfaces\FrameworkInterface;
use HtmlObject\Element;

/**
* Base helpers and common methods to all frameworks
*/
abstract class Framework
abstract class Framework implements FrameworkInterface
{
/**
* The Container
Expand Down Expand Up @@ -309,4 +310,21 @@ public function wrapLabel($label)
{
return $label;
}

////////////////////////////////////////////////////////////////////
//////////////////////////// RENDER BLOCKS /////////////////////////
////////////////////////////////////////////////////////////////////

/**
* Render an validation error text
*
* @param string $text
* @param array $attributes
*
* @return string
*/
public function createValidationError($text, $attributes = array())
{
return $this->createHelp($text, $attributes);
}
}
6 changes: 3 additions & 3 deletions tests/Framework/TwitterBootstrap4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ public function testCanCreateWithErrors()

$required = $this->former->text('required')->__toString();
$matcher =
'<div class="form-group has-error">'.
'<div class="form-group is-invalid">'.
'<label for="required" class="control-label">Required</label>'.
'<input class="form-control" id="required" type="text" name="required">'.
'<span class="form-text text-muted">The required field is required.</span>'.
'<input class="form-control is-invalid" id="required" type="text" name="required">'.
'<div class="invalid-feedback">The required field is required.</div>'.
'</div>';

$this->assertEquals($matcher, $required);
Expand Down

0 comments on commit c60a32e

Please sign in to comment.