From 5060a6641dbdf72ea277e30f353931da2617c00a Mon Sep 17 00:00:00 2001 From: Devin Date: Fri, 16 Mar 2018 09:57:16 -0700 Subject: [PATCH] Fixed bug with radio button not being checked (#501) * Fixed bug with radio button not being checked Fixes: https://github.com/LaravelCollective/html/issues/496 Loose comparison needs to be done because Laravel model casting may be in affect and therfore 1 == true and 0 == false. * typo --- src/FormBuilder.php | 20 +++++++++++++++++--- tests/FormBuilderTest.php | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index c701de9..bdfa63f 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -190,7 +190,7 @@ class FormBuilder { $this->model = $model; } - + /** * Get the current model instance on the form builder. * @@ -871,7 +871,7 @@ class FormBuilder return $this->getRadioCheckedState($name, $value, $checked); default: - return $this->getValueAttribute($name) === $value; + return $this->compareValues($name, $value); } } @@ -924,7 +924,21 @@ class FormBuilder return $checked; } - return $this->getValueAttribute($name) === $value; + return $this->compareValues($name, $value); + } + + /** + * Determine if the provide value loosely compares to the value assigned to the field. + * Use loose comparison because Laravel model casting may be in affect and therefore + * 1 == true and 0 == false. + * + * @param string $name + * @param string $value + * @return bool + */ + protected function compareValues($name, $value) + { + return $this->getValueAttribute($name) == $value; } /** diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index dc98c81..4a846cb 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -658,6 +658,21 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals('', $form4); } + public function testFormRadioWithAttributeCastToBoolean() + { + $this->setModel(['itemA' => true, 'itemB' => false]); + + $radio1 = $this->formBuilder->radio('itemA', 1); + $radio2 = $this->formBuilder->radio('itemA', 0); + $radio3 = $this->formBuilder->radio('itemB', 1); + $radio4 = $this->formBuilder->radio('itemB', 0); + + $this->assertEquals('', $radio1); + $this->assertEquals('', $radio2); + $this->assertEquals('', $radio3); + $this->assertEquals('', $radio4); + } + public function testFormRadioRepopulation() { $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Contracts\Session\Session'));