Skip to content

Commit

Permalink
Fix invalid feedback visibility of checkboxes and radios for Bootstrap 4
Browse files Browse the repository at this point in the history
E.g. with Laravel:
@php
    $validator = Validator::make(request()->all(), [
        'failed_checkbox' => 'required',
        'failed_radio' => 'required',
        'failed_inline_checkbox' => 'required',
        'failed_inline_radio' => 'required',
        'failed_checkboxes' => 'required',
        'failed_inline_checkboxes' => 'required',
    ]);

    $validator->fails();
    Former::withErrors($validator);
@endphp

{!! Former::open()->method('GET') !!}
    {!! Former::checkbox('failed_checkbox')->text('Failed checkbox') !!}
    {!! Former::radio('failed_radio')->text('Failed radio') !!}
{!! Former::checkbox('failed_inline_checkbox')->text('Failed inline
checkbox')->inline() !!}
{!! Former::radio('failed_inline_radio')->text('Failed inline
radio')->inline() !!}
{!! Former::checkboxes('failed_checkboxes')->checkboxes('first',
'second', 'third', 'fourth') !!}
{!!
Former::checkboxes('failed_inline_checkboxes')->checkboxes('first',
'second', 'third', 'fourth')->inline() !!}
{!! Former::close() !!}

Reference:
formers#581 (comment)
  • Loading branch information
tortuetorche committed Dec 30, 2020
1 parent 84aea08 commit 397a763
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/Former/Form/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,17 @@ public function contents($contents)
public function wrapField($field)
{
$label = $this->getLabel($field);
$help = $this->getHelp();
if ($field->isCheckable() && $this->app['former']->framework() == 'TwitterBootstrap4') {
$wrapperClass = $field->isInline() ? 'form-check form-check-inline' : 'form-check';
if ($this->app['former']->getErrors($field->getName())) {
$hiddenInput = Element::create('input', null, ['type' => 'hidden'])->class('form-check-input is-invalid');
$help = $hiddenInput.$help;
}
$help = Element::create('div', $help)->class($wrapperClass);
}
$field = $this->prependAppend($field);
$field .= $this->getHelp();
$field .= $help;

return $this->wrap($field, $label);
}
Expand Down
12 changes: 11 additions & 1 deletion src/Former/Traits/Checkable.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ public function check($checked = true)
return $this;
}


/**
* Check if the checkables are inline
*
* @return boolean
*/
public function isInline()
{
return $this->inline;
}

////////////////////////////////////////////////////////////////////
////////////////////////// INTERNAL METHODS ////////////////////////
////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -391,7 +402,6 @@ protected function createCheckable($item, $fallbackValue = 1)
$wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check';

$element = Element::create('div', $element)->class($wrapper_class)->render();

} else {
// Original way is to add the 'input' inside the 'label'
$element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render();
Expand Down

0 comments on commit 397a763

Please sign in to comment.