From 0eed235b81183e4b1da7e54bcf10c5844d661fe2 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Sat, 27 Oct 2018 17:48:31 +0200 Subject: [PATCH 1/4] Fix the error state for Bootstrap 4 --- src/Former/Framework/TwitterBootstrap4.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Former/Framework/TwitterBootstrap4.php b/src/Former/Framework/TwitterBootstrap4.php index 9059b013..70b0c72d 100644 --- a/src/Former/Framework/TwitterBootstrap4.php +++ b/src/Former/Framework/TwitterBootstrap4.php @@ -65,9 +65,8 @@ class TwitterBootstrap4 extends Framework implements FrameworkInterface * @var array */ protected $states = array( - 'has-warning', - 'has-error', - 'has-success', + 'is-valid', + 'is-invalid', ); /** @@ -158,7 +157,7 @@ public function filterFieldClasses($classes) */ public function errorState() { - return 'has-error'; + return 'is-invalid'; } /** @@ -241,6 +240,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); } From 2e638dff5e9163cb96f2170f4671fb21dcd10e47 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Sat, 27 Oct 2018 18:09:19 +0200 Subject: [PATCH 2/4] Fix Bootstrap 4 validation messages not being colored red --- src/Former/Form/Group.php | 6 +++--- src/Former/Framework/TwitterBootstrap4.php | 14 +++++++++++++- src/Former/Traits/Framework.php | 20 +++++++++++++++++++- tests/Framework/TwitterBootstrap4Test.php | 6 +++--- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Former/Form/Group.php b/src/Former/Form/Group.php index 17403d51..d4e681c2 100644 --- a/src/Former/Form/Group.php +++ b/src/Former/Form/Group.php @@ -435,9 +435,9 @@ 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); - } + if ($errors and $this->app['former']->getOption('error_messages')) { + $inline = $this->app['former.framework']->createValidationError($errors); + } return join(null, array($inline, $block)); } diff --git a/src/Former/Framework/TwitterBootstrap4.php b/src/Former/Framework/TwitterBootstrap4.php index 70b0c72d..fac23646 100644 --- a/src/Former/Framework/TwitterBootstrap4.php +++ b/src/Former/Framework/TwitterBootstrap4.php @@ -65,7 +65,6 @@ class TwitterBootstrap4 extends Framework implements FrameworkInterface * @var array */ protected $states = array( - 'is-valid', 'is-invalid', ); @@ -340,6 +339,19 @@ public function createHelp($text, $attributes = array()) return Element::create('span', $text, $attributes)->addClass('form-text 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'); + } + /** * Render an help text * diff --git a/src/Former/Traits/Framework.php b/src/Former/Traits/Framework.php index 96378448..e68f622c 100644 --- a/src/Former/Traits/Framework.php +++ b/src/Former/Traits/Framework.php @@ -1,12 +1,13 @@ createHelp($text, $attributes); + } } diff --git a/tests/Framework/TwitterBootstrap4Test.php b/tests/Framework/TwitterBootstrap4Test.php index 4a495efa..e302dfec 100644 --- a/tests/Framework/TwitterBootstrap4Test.php +++ b/tests/Framework/TwitterBootstrap4Test.php @@ -138,10 +138,10 @@ public function testCanCreateWithErrors() $required = $this->former->text('required')->__toString(); $matcher = - '
'. + '
'. ''. - ''. - 'The required field is required.'. + ''. + '
The required field is required.
'. '
'; $this->assertEquals($matcher, $required); From ecd1772354cd054fc1a81396ed4b307a66d7c88c Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Sat, 27 Oct 2018 18:12:30 +0200 Subject: [PATCH 3/4] Fix tabs vs spaces :) --- src/Former/Form/Group.php | 6 ++-- src/Former/Framework/TwitterBootstrap4.php | 24 ++++++++-------- src/Former/Traits/Framework.php | 32 +++++++++++----------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/Former/Form/Group.php b/src/Former/Form/Group.php index d4e681c2..8b33aef9 100644 --- a/src/Former/Form/Group.php +++ b/src/Former/Form/Group.php @@ -435,9 +435,9 @@ 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']->createValidationError($errors); - } + if ($errors and $this->app['former']->getOption('error_messages')) { + $inline = $this->app['former.framework']->createValidationError($errors); + } return join(null, array($inline, $block)); } diff --git a/src/Former/Framework/TwitterBootstrap4.php b/src/Former/Framework/TwitterBootstrap4.php index fac23646..98ac4488 100644 --- a/src/Former/Framework/TwitterBootstrap4.php +++ b/src/Former/Framework/TwitterBootstrap4.php @@ -339,18 +339,18 @@ public function createHelp($text, $attributes = array()) return Element::create('span', $text, $attributes)->addClass('form-text 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'); - } + /** + * 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'); + } /** * Render an help text diff --git a/src/Former/Traits/Framework.php b/src/Former/Traits/Framework.php index e68f622c..e9d1b282 100644 --- a/src/Former/Traits/Framework.php +++ b/src/Former/Traits/Framework.php @@ -311,20 +311,20 @@ 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); - } + //////////////////////////////////////////////////////////////////// + //////////////////////////// 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); + } } From 3834c22aa51e099d64473550f3ea8b9e2f79efe0 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Sat, 27 Oct 2018 18:22:12 +0200 Subject: [PATCH 4/4] Fix the Bootstrap 4 help blocks --- src/Former/Framework/TwitterBootstrap4.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Former/Framework/TwitterBootstrap4.php b/src/Former/Framework/TwitterBootstrap4.php index 98ac4488..5161b243 100644 --- a/src/Former/Framework/TwitterBootstrap4.php +++ b/src/Former/Framework/TwitterBootstrap4.php @@ -336,7 +336,7 @@ 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'); } /** @@ -362,7 +362,7 @@ public function createValidationError($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'); } /**