Skip to content

Commit

Permalink
Add generateField() method to InputInterface::class. (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Feb 14, 2024
1 parent 86f2c3e commit e9b07df
Show file tree
Hide file tree
Showing 52 changed files with 426 additions and 391 deletions.
9 changes: 9 additions & 0 deletions src/Input/Base/AbstractChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ abstract class AbstractChoiceList extends Element implements
use Attribute\Custom\HasTemplate;
use Attribute\Custom\HasUnchecked;
use Attribute\Custom\HasWidgetValidation;
use Attribute\Field\HasGenerateField;
use Attribute\HasClass;
use Attribute\HasId;
use Attribute\HasTabindex;
Expand Down Expand Up @@ -57,6 +58,10 @@ protected function buildChoiceListTag(string $type): string
/** @var string $name */
$name = $attributes['name'] ?? '';

if ($this->id === null) {
unset($attributes['id']);
}

if ($this->ariaDescribedBy === true) {
$attributes['aria-describedby'] = "$this->id-help";
}
Expand Down Expand Up @@ -90,6 +95,10 @@ protected function buildChoiceListTag(string $type): string
->name($name)
->separator($this->separator);

if ($this->id === null) {
$listItem = $listItem->id(null);
}

if ($this->enclosedByLabel === true) {
$listItem = $listItem->enclosedByLabel(true);
}
Expand Down
8 changes: 7 additions & 1 deletion src/Input/Base/AbstractInputChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ abstract class AbstractInputChoice extends Element implements
use Attribute\Custom\HasTemplate;
use Attribute\Custom\HasUnchecked;
use Attribute\Custom\HasWidgetValidation;
use Attribute\Field\HasGenerateField;
use Attribute\HasClass;
use Attribute\HasData;
use Attribute\HasId;
Expand Down Expand Up @@ -78,6 +79,11 @@ protected function buildChoiceTag(string $type): string

/** @var string $id */
$id = $attributes['id'] ?? $this->generateId("$type-");

if ($this->id === null) {
$id = null;
}

$labelFor = $this->labelFor ?? $id;
/** @var string $name */
$name = $attributes['name'] ?? '';
Expand Down Expand Up @@ -121,7 +127,7 @@ private function prepareTemplate(string $tag, string $labelTag, string $name): s
return $this->renderTemplate($this->template, $tokenValues);
}

private function renderEnclosedByLabel(string $tag, string $labelFor): string
private function renderEnclosedByLabel(string $tag, string|null $labelFor): string
{
if ($this->labelContent === '' || $this->notLabel) {
return $tag;
Expand Down
11 changes: 11 additions & 0 deletions src/Input/Contract/InputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ public function class(string $value, bool $override = false): static;
*/
public function attributes(array $values): static;

/**
* Generate the id and name attributes for the field.
*
* @param string $modelName The name of the model.
* @param string $fieldName The name of the field.
* @param bool $arrayable Whether the field is arrayable.
*
* @return static A new instance or clone of the current object with the id and name attributes set.
*/
public function generateField(string $modelName, string $fieldName, bool $arrayable = false): static;

/**
* Set the ID of the widget.
*
Expand Down
8 changes: 2 additions & 6 deletions tests/Input/Button/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public function testAttribute(): void
<input class="value" id="button-6582f2d099e8b" type="button">
</div>
HTML,
Button::widget()->attributes([
'class' => 'value',
])->id('button-6582f2d099e8b')->render()
Button::widget()->attributes(['class' => 'value'])->id('button-6582f2d099e8b')->render()
);
}

Expand Down Expand Up @@ -83,9 +81,7 @@ public function testDataAttributes(): void
<input id="button-6582f2d099e8b" type="button" data-value="value">
</div>
HTML,
Button::widget()->dataAttributes([
'value' => 'value',
])->id('button-6582f2d099e8b')->render()
Button::widget()->dataAttributes(['value' => 'value'])->id('button-6582f2d099e8b')->render()
);
}

Expand Down
16 changes: 4 additions & 12 deletions tests/Input/Button/CustomMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public function testContainerAttributes(): void
<input id="button-6582f2d099e8b" type="button">
</div>
HTML,
Button::widget()->containerAttributes([
'class' => 'value',
])->id('button-6582f2d099e8b')->render()
Button::widget()->containerAttributes(['class' => 'value'])->id('button-6582f2d099e8b')->render()
);
}

Expand Down Expand Up @@ -65,9 +63,7 @@ public function testContainerWithFalseWithDefinitions(): void
<<<HTML
<input id="button-6582f2d099e8b" type="button">
HTML,
Button::widget([
'container()' => [false],
])->id('button-6582f2d099e8b')->render()
Button::widget(['container()' => [false]])->id('button-6582f2d099e8b')->render()
);
}

Expand Down Expand Up @@ -114,9 +110,7 @@ public function testPrefixContainerAttributes(): void
->id('button-6582f2d099e8b')
->prefix('Prefix')
->prefixContainer(true)
->prefixContainerAttributes([
'class' => 'value',
])
->prefixContainerAttributes(['class' => 'value'])
->render()
);
}
Expand Down Expand Up @@ -182,9 +176,7 @@ public function testSuffixContainerAttributes(): void
->id('button-6582f2d099e8b')
->suffix('Suffix')
->suffixContainer(true)
->suffixContainerAttributes([
'class' => 'value',
])
->suffixContainerAttributes(['class' => 'value'])
->render()
);
}
Expand Down
4 changes: 1 addition & 3 deletions tests/Input/Button/LabelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ public function testLabelAttributes(): void
HTML,
Button::widget()
->id('button-6582f2d099e8b')
->labelAttributes([
'class' => 'value',
])
->labelAttributes(['class' => 'value'])
->labelContent('Label')
->render()
);
Expand Down
8 changes: 2 additions & 6 deletions tests/Input/ButtonGroup/CustomMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public function testContainerAttributes(): void
Button::widget()->id('button-6582f2d099e8a')->type('submit')->value('Submit'),
Button::widget()->id('button-6582f2d099e8b')->type('reset')->value('Reset')
)
->containerAttributes([
'class' => 'value',
])
->containerAttributes(['class' => 'value'])
->render()
);
}
Expand Down Expand Up @@ -97,9 +95,7 @@ public function testContainerWithFalseWithDefinitions(): void
<input id="button-6582f2d099e8a" type="submit" value="Submit">
<input id="button-6582f2d099e8b" type="reset" value="Reset">
HTML,
ButtonGroup::widget([
'container()' => [false],
])
ButtonGroup::widget(['container()' => [false]])
->buttons(
Button::widget()->id('button-6582f2d099e8a')->type('submit')->value('Submit'),
Button::widget()->id('button-6582f2d099e8b')->type('reset')->value('Reset')
Expand Down
17 changes: 6 additions & 11 deletions tests/Input/Checkbox/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ public function testAttributes(): void
<<<HTML
<input class="value" id="value" type="checkbox">
HTML,
Checkbox::widget()->attributes([
'class' => 'value',
'id' => 'value',
])->render()
Checkbox::widget()->attributes(['class' => 'value', 'id' => 'value'])->render()
);
}

Expand Down Expand Up @@ -112,9 +109,7 @@ public function testDataAttributes(): void
<<<HTML
<input id="checkbox-6582f2d099e8b" type="checkbox" data-value="value">
HTML,
Checkbox::widget()->dataAttributes([
'value' => 'value',
])->id('checkbox-6582f2d099e8b')->render()
Checkbox::widget()->dataAttributes(['value' => 'value'])->id('checkbox-6582f2d099e8b')->render()
);
}

Expand Down Expand Up @@ -343,19 +338,19 @@ public function testWithoutId(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input type="checkbox">
<input name="ModelName[fieldName]" type="checkbox">
HTML,
Checkbox::widget()->id(null)->render()
Checkbox::widget()->generateField('ModelName', 'fieldName')->id(null)->render()
);
}

public function testWithoutName(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="checkbox-6582f2d099e8b" type="checkbox">
<input id="modelname-fieldname" type="checkbox">
HTML,
Checkbox::widget()->id('checkbox-6582f2d099e8b')->name(null)->render()
Checkbox::widget()->generateField('ModelName', 'fieldName')->name(null)->render()
);
}
}
30 changes: 15 additions & 15 deletions tests/Input/Checkbox/CustomMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public function testContainerAttributes(): void
HTML,
Checkbox::widget()
->container(true)
->containerAttributes([
'class' => 'value',
])
->containerAttributes(['class' => 'value'])
->id('checkbox-6582f2d099e8b')
->render()
);
Expand Down Expand Up @@ -81,9 +79,17 @@ public function testContainerWithFalseWithDefinitions(): void
<<<HTML
<input id="checkbox-6582f2d099e8b" type="checkbox">
HTML,
Checkbox::widget([
'container()' => [false],
])->id('checkbox-6582f2d099e8b')->render()
Checkbox::widget(['container()' => [false]])->id('checkbox-6582f2d099e8b')->render()
);
}

public function testGenerateFieldId(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="modelname-fieldname" name="ModelName[fieldName]" type="checkbox">
HTML,
Checkbox::widget()->generateField('ModelName', 'fieldName')->render()
);
}

Expand Down Expand Up @@ -124,9 +130,7 @@ public function testPrefixContainerAttributes(): void
->id('checkbox-6582f2d099e8b')
->prefix('prefix')
->prefixContainer(true)
->prefixContainerAttributes([
'class' => 'value',
])
->prefixContainerAttributes(['class' => 'value'])
->render()
);
}
Expand Down Expand Up @@ -202,9 +206,7 @@ public function testSuffixContainerAttributes(): void
->id('checkbox-6582f2d099e8b')
->suffix('suffix')
->suffixContainer(true)
->suffixContainerAttributes([
'class' => 'value',
])
->suffixContainerAttributes(['class' => 'value'])
->render()
);
}
Expand Down Expand Up @@ -265,9 +267,7 @@ public function testUncheckAttributes(): void
Checkbox::widget()
->id('checkbox-6582f2d099e8b')
->name('value')
->uncheckAttributes([
'class' => 'value',
])
->uncheckAttributes(['class' => 'value'])
->uncheckValue('0')
->value(1)
->render()
Expand Down
42 changes: 21 additions & 21 deletions tests/Input/CheckboxList/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ public function testAttributes(): void
</div>
HTML,
CheckboxList::widget()
->attributes([
'class' => 'value',
])
->attributes(['class' => 'value'])
->id('checkboxlist-65858c272ea89')
->items(
Checkbox::widget()->id('checkbox-6599b6a33dd96')->labelContent('Apple')->value(1),
Expand Down Expand Up @@ -543,20 +541,21 @@ public function testWithoutId(): void
Assert::equalsWithoutLE(
<<<HTML
<div>
<input id="checkbox-6599b6a33dd96" type="checkbox" value="1">
<label for="checkbox-6599b6a33dd96">Apple</label>
<input id="checkbox-6599b6a33dd98" type="checkbox" value="2">
<label for="checkbox-6599b6a33dd98">Banana</label>
<input id="checkbox-6599b6a33dd97" type="checkbox" value="3">
<label for="checkbox-6599b6a33dd97">Orange</label>
<input name="ModelName[fieldName][]" type="checkbox" value="1">
<label>Apple</label>
<input name="ModelName[fieldName][]" type="checkbox" value="2">
<label>Banana</label>
<input name="ModelName[fieldName][]" type="checkbox" value="3">
<label>Orange</label>
</div>
HTML,
CheckboxList::widget()
->generateField('ModelName', 'fieldName')
->id(null)
->items(
Checkbox::widget()->id('checkbox-6599b6a33dd96')->labelContent('Apple')->value(1),
Checkbox::widget()->id('checkbox-6599b6a33dd98')->labelContent('Banana')->value(2),
Checkbox::widget()->id('checkbox-6599b6a33dd97')->labelContent('Orange')->value(3),
Checkbox::widget()->labelContent('Apple')->value(1),
Checkbox::widget()->labelContent('Banana')->value(2),
Checkbox::widget()->labelContent('Orange')->value(3),
)
->render(),
);
Expand All @@ -567,20 +566,21 @@ public function testWithoutName(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="checkboxlist-65858c272ea89">
<input id="checkbox-6599b6a33dd96" type="checkbox" value="1">
<label for="checkbox-6599b6a33dd96">Apple</label>
<input id="checkbox-6599b6a33dd98" type="checkbox" value="2">
<label for="checkbox-6599b6a33dd98">Banana</label>
<input id="checkbox-6599b6a33dd97" type="checkbox" value="3">
<label for="checkbox-6599b6a33dd97">Orange</label>
<input id="modelname-fieldname" type="checkbox" value="1">
<label for="modelname-fieldname">Apple</label>
<input id="modelname-fieldname" type="checkbox" value="2">
<label for="modelname-fieldname">Banana</label>
<input id="modelname-fieldname" type="checkbox" value="3">
<label for="modelname-fieldname">Orange</label>
</div>
HTML,
CheckboxList::widget()
->generateField('ModelName', 'fieldName')
->id('checkboxlist-65858c272ea89')
->items(
Checkbox::widget()->id('checkbox-6599b6a33dd96')->labelContent('Apple')->value(1),
Checkbox::widget()->id('checkbox-6599b6a33dd98')->labelContent('Banana')->value(2),
Checkbox::widget()->id('checkbox-6599b6a33dd97')->labelContent('Orange')->value(3),
Checkbox::widget()->labelContent('Apple')->value(1),
Checkbox::widget()->labelContent('Banana')->value(2),
Checkbox::widget()->labelContent('Orange')->value(3),
)
->name(null)
->render(),
Expand Down

0 comments on commit e9b07df

Please sign in to comment.