Skip to content

Commit

Permalink
Add support null value. (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Nov 2, 2021
1 parent e7aac20 commit 7a544f5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Widget/Range.php
Expand Up @@ -107,8 +107,8 @@ protected function run(): string
/** @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.range.html#input.range.attrs.value */
$value = HtmlForm::getAttributeValue($new->getFormModel(), $new->attribute);

if (!is_numeric($value)) {
throw new InvalidArgumentException('Range widget must be a numeric value.');
if (!is_numeric($value) && null !== $value) {
throw new InvalidArgumentException('Range widget must be a numeric or null value.');
}

$new->outputAttributes['for'] = $name;
Expand Down
30 changes: 22 additions & 8 deletions tests/Widget/FieldRangeTest.php
Expand Up @@ -114,14 +114,28 @@ public function testRender(): void

public function testValue(): void
{
// string value numeric `1`.
// value null
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', ['i' => 6]);
$expected = <<<'HTML'
<div>
<label for="typeform-tonull">To Null</label>
<input type="range" id="typeform-tonull" name="TypeForm[toNull]" oninput="i7.value=this.value">
<output id="i7" name="i7" for="TypeForm[toNull]"></output>
</div>
HTML;
$this->assertEqualsWithoutLE(
$expected,
Field::widget()->config($this->formModel, 'toNull')->range()->render(),
);

// value string numeric `1`
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', ['i' => 7]);
$this->formModel->setAttribute('string', '1');
$expected = <<<'HTML'
<div>
<label for="typeform-string">String</label>
<input type="range" id="typeform-string" name="TypeForm[string]" value="1" placeholder="Typed your text string." oninput="i7.value=this.value">
<output id="i7" name="i7" for="TypeForm[string]">1</output>
<input type="range" id="typeform-string" name="TypeForm[string]" value="1" placeholder="Typed your text string." oninput="i8.value=this.value">
<output id="i8" name="i8" for="TypeForm[string]">1</output>
<div>Write your text string.</div>
</div>
HTML;
Expand All @@ -130,14 +144,14 @@ public function testValue(): void
Field::widget()->config($this->formModel, 'string')->range()->render(),
);

// int value 1
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', ['i' => 7]);
// value int 1
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', ['i' => 8]);
$this->formModel->setAttribute('int', '1');
$expected = <<<'HTML'
<div>
<label for="typeform-int">Int</label>
<input type="range" id="typeform-int" name="TypeForm[int]" value="1" oninput="i8.value=this.value">
<output id="i8" name="i8" for="TypeForm[int]">1</output>
<input type="range" id="typeform-int" name="TypeForm[int]" value="1" oninput="i9.value=this.value">
<output id="i9" name="i9" for="TypeForm[int]">1</output>
</div>
HTML;
$this->assertEqualsWithoutLE(
Expand All @@ -149,7 +163,7 @@ public function testValue(): void
public function testValueException(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Range widget must be a numeric value.');
$this->expectExceptionMessage('Range widget must be a numeric or null value.');
Field::widget()->config($this->formModel, 'array')->range()->render();
}

Expand Down
27 changes: 19 additions & 8 deletions tests/Widget/RangeTest.php
Expand Up @@ -100,24 +100,35 @@ public function testRender(): void

public function testValue(): void
{
// string value numeric `1`
// value null
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', ['i' => 6]);
$expected = <<<'HTML'
<input type="range" id="typeform-tonull" name="TypeForm[toNull]" oninput="i7.value=this.value">
<output id="i7" name="i7" for="TypeForm[toNull]"></output>
HTML;
$this->assertEqualsWithoutLE(
$expected,
Range::widget()->config($this->formModel, 'toNull')->render(),
);

// value string numeric `1`
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', ['i' => 7]);
$this->formModel->setAttribute('string', '1');
$expected = <<<'HTML'
<input type="range" id="typeform-string" name="TypeForm[string]" value="1" oninput="i7.value=this.value">
<output id="i7" name="i7" for="TypeForm[string]">1</output>
<input type="range" id="typeform-string" name="TypeForm[string]" value="1" oninput="i8.value=this.value">
<output id="i8" name="i8" for="TypeForm[string]">1</output>
HTML;
$this->assertEqualsWithoutLE(
$expected,
Range::widget()->config($this->formModel, 'string')->render(),
);

// int value 1
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', ['i' => 7]);
// value int 1
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', ['i' => 8]);
$this->formModel->setAttribute('int', '1');
$expected = <<<'HTML'
<input type="range" id="typeform-int" name="TypeForm[int]" value="1" oninput="i8.value=this.value">
<output id="i8" name="i8" for="TypeForm[int]">1</output>
<input type="range" id="typeform-int" name="TypeForm[int]" value="1" oninput="i9.value=this.value">
<output id="i9" name="i9" for="TypeForm[int]">1</output>
HTML;
$this->assertEqualsWithoutLE(
$expected,
Expand All @@ -128,7 +139,7 @@ public function testValue(): void
public function testValueException(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Range widget must be a numeric value.');
$this->expectExceptionMessage('Range widget must be a numeric or null value.');
Range::widget()->config($this->formModel, 'array')->render();
}

Expand Down

0 comments on commit 7a544f5

Please sign in to comment.