Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propose showing validation errors below inputs #666

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<span class="invalid-feedback" role="alert"><strong>'.$this->request->session()->get('errors')->first($name).'</strong></span>';
}
}

return $this->toHtmlString('<input' . $this->html->attributes($options) . '>');
return $this->toHtmlString('<input' . $this->html->attributes($options) . '/>'.$error_feedback);
}

/**
Expand Down Expand Up @@ -658,15 +669,24 @@ 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 = '<span class="invalid-feedback" role="alert"><strong>'.$this->request->session()->get('errors')->first($name).'</strong></span>';
}
}
// 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.
$selectAttributes = $this->html->attributes($selectAttributes);

$list = implode('', $html);

return $this->toHtmlString("<select{$selectAttributes}>{$list}</select>");
return $this->toHtmlString("<select{$selectAttributes}>{$list}</select>",$error_feedback);
}

/**
Expand Down