From 77bdc8c8fb088076124784291fa00a28b338f439 Mon Sep 17 00:00:00 2001 From: Dimitar Yanakiev Date: Sat, 3 Oct 2020 21:34:52 +0300 Subject: [PATCH] Propose showing validation errors below inputs --- src/FormBuilder.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 6a228ff7..5749619b 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -305,8 +305,19 @@ public function input($type, $name, $value = null, $options = []) $merge = compact('type', 'value', 'id'); $options = array_merge($options, $merge); + + $options['autocomplete'] = 'new-'.$name.'_090_'.time(); + + if(!isset($options['class'])) $options['class'] = ''; + $error_feedback = ''; + if($this->request->session()->get('errors')) { + if($this->request->session()->get('errors')->has($name)) { + $options['class'] = $options['class'] . ' is-invalid'; + $error_feedback = ''.$this->request->session()->get('errors')->first($name).''; + } + } - return $this->toHtmlString('html->attributes($options) . '>'); + return $this->toHtmlString('html->attributes($options) . '/>'.$error_feedback); } /** @@ -658,7 +669,16 @@ public function select( $optgroupAttributes = $optgroupsAttributes[$value] ?? []; $html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes, $optgroupAttributes); } - + $selectAttributes['autocomplete'] = 'new-'.$name.'_090_'.time(); + + if(!isset($selectAttributes['class'])) $selectAttributes['class'] = ''; + $error_feedback = ''; + if($this->request->session()->get('errors')) { + if($this->request->session()->get('errors')->has($name)) { + $selectAttributes['class'] = $selectAttributes['class'] . ' is-invalid'; + $error_feedback = ''.$this->request->session()->get('errors')->first($name).''; + } + } // Once we have all of this HTML, we can join this into a single element after // formatting the attributes into an HTML "attributes" string, then we will // build out a final select statement, which will contain all the values. @@ -666,7 +686,7 @@ public function select( $list = implode('', $html); - return $this->toHtmlString("{$list}"); + return $this->toHtmlString("{$list}",$error_feedback); } /**