Skip to content

Commit

Permalink
Add prepend/append functionality to form field elements
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Feb 4, 2024
1 parent 7cf5050 commit 32a8759
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 18 deletions.
126 changes: 126 additions & 0 deletions src/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ abstract class AbstractElement extends Child implements ElementInterface
*/
protected array $hintAttributes = [];

/**
* Form element prepend contents
* @var ?string
*/
protected ?string $prepend = null;

/**
* Form element append contents
* @var ?string
*/
protected ?string $append = null;

/**
* Form element required property
* @var bool
Expand Down Expand Up @@ -160,6 +172,30 @@ public function setHint(string $hint): AbstractElement
return $this;
}

/**
* Set the prepend contents of the form element object
*
* @param string $prepend
* @return AbstractElement
*/
public function setPrepend(string $prepend): AbstractElement
{
$this->prepend = $prepend;
return $this;
}

/**
* Set the append contents of the form element object
*
* @param string $append
* @return AbstractElement
*/
public function setAppend(string $append): AbstractElement
{
$this->append = $append;
return $this;
}

/**
* Set an attribute of the label of the form element object
*
Expand Down Expand Up @@ -329,6 +365,16 @@ public function getLabel(): ?string
return $this->label;
}

/**
* Determine if form element has a label
*
* @return bool
*/
public function hasLabel(): bool
{
return !empty($this->label);
}

/**
* Get form element object hint
*
Expand All @@ -339,6 +385,56 @@ public function getHint(): ?string
return $this->hint;
}

/**
* Determine if form element has a hint
*
* @return bool
*/
public function hasHint(): bool
{
return !empty($this->hint);
}

/**
* Get form element object prepend contents
*
* @return ?string
*/
public function getPrepend(): ?string
{
return $this->prepend;
}

/**
* Determine if form element has prepend content
*
* @return bool
*/
public function hasPrepend(): bool
{
return !empty($this->prepend);
}

/**
* Get form element object append contents
*
* @return ?string
*/
public function getAppend(): ?string
{
return $this->append;
}

/**
* Determine if form element has append content
*
* @return bool
*/
public function hasAppend(): bool
{
return !empty($this->append);
}

/**
* Get the attributes of the form element object label
*
Expand All @@ -349,6 +445,16 @@ public function getLabelAttributes(): array
return $this->labelAttributes;
}

/**
* Determine if form element has label attributes
*
* @return bool
*/
public function hasLabelAttributes(): bool
{
return !empty($this->labelAttributes);
}

/**
* Get the attributes of the form element object hint
*
Expand All @@ -359,6 +465,16 @@ public function getHintAttributes(): array
return $this->hintAttributes;
}

/**
* Determine if form element has hint attributes
*
* @return bool
*/
public function hasHintAttributes(): bool
{
return !empty($this->hintAttributes);
}

/**
* Get validators
*
Expand All @@ -369,6 +485,16 @@ public function getValidators(): array
return $this->validators;
}

/**
* Determine if form element has validators
*
* @return bool
*/
public function hasValidators(): bool
{
return !empty($this->validators);
}

/**
* Get whether the form element object is required
*
Expand Down
10 changes: 10 additions & 0 deletions src/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public static function create(string $name, array $field): AbstractElement
$hint = $field['hint'] ?? null;
$hintAttribs = $field['hint-attributes'] ?? null;
$labelAttribs = $field['label-attributes'] ?? null;
$prepend = $field['prepend'] ?? null;
$append = $field['append'] ?? null;

$errorPre = (isset($field['error']) && ($field['error'] == 'pre'));

Expand Down Expand Up @@ -142,6 +144,14 @@ public static function create(string $name, array $field): AbstractElement
if (($labelAttribs !== null) && is_array($labelAttribs)) {
$element->setLabelAttributes($labelAttribs);
}
// Set prepend content.
if ($prepend !== null) {
$element->setPrepend($prepend);
}
// Set append content.
if ($append !== null) {
$element->setAppend($append);
}
// Set the hint.
if ($hint !== null) {
$element->setHint($hint);
Expand Down
69 changes: 51 additions & 18 deletions src/Fieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public function prepareForView(): array

foreach ($this->fields as $groups) {
foreach ($groups as $field) {
if ($field->getLabel() !== null) {
if ($field->hasLabel()) {
$labelFor = $field->getName() . (($field->getNodeName() == 'fieldset') ? '1' : '');
$label = new Child('label', $field->getLabel());
$label->setAttribute('for', $labelFor);
Expand All @@ -466,7 +466,7 @@ public function prepareForView(): array
$fields[$field->getName() . '_label'] = $label->render();
}

if ($field->getHint() !== null) {
if ($field->hasHint()) {
$hint = new Child('span', $field->getHint());
if ($field->getHintAttributes() !== null) {
$hint->setAttributes($field->getHintAttributes());
Expand Down Expand Up @@ -504,13 +504,13 @@ protected function prepareTable(): void
}

$tr = new Child('tr');
if ($field->getLabel() !== null) {
if ($field->hasLabel()) {
$td = new Child('td');
$labelFor = $field->getName() . (($field->getNodeName() == 'fieldset') ? '1' : '');

$label = new Child('label', $field->getLabel());
$label->setAttribute('for', $labelFor);
if ($field->getLabelAttributes() !== null) {
if ($field->hasLabelAttributes()) {
$label->setAttributes($field->getLabelAttributes());
}
if ($field->isRequired()) {
Expand All @@ -524,21 +524,32 @@ protected function prepareTable(): void
$tr->addChild($td);
}

$td = new Child('td');
$nodeValue = null;
$options = [];
if ($field->hasAppend()) {
$nodeValue = $field->getAppend();
$options = ['childrenFirst' => true];
} else if ($field->hasPrepend()) {
$nodeValue = $field->getPrepend();
$options = ['childrenFirst' => false];
}

$td = new Child('td', $nodeValue, $options);

if ($field->isErrorPre()) {
$td->addChildren($errors);
}
$td->addChild($field);

if ($field->getHint() !== null) {
if ($field->hasHint()) {
$hint = new Child('span', $field->getHint());
if ($field->getHintAttributes() !== null) {
if ($field->hasHintAttributes()) {
$hint->setAttributes($field->getHintAttributes());
}
$td->addChild($hint);
}

if ($field->getLabel() === null) {
if ($field->hasLabel()) {
$td->setAttribute('colspan', 2);
}
if (!$field->isErrorPre()) {
Expand Down Expand Up @@ -569,12 +580,23 @@ protected function prepareElement(string $element): void
}
}

$container = new Child($element);
if ($field->getLabel() !== null) {
$nodeValue = null;
$options = [];
if ($field->hasAppend()) {
$nodeValue = $field->getAppend();
$options = ['childrenFirst' => true];
} else if ($field->hasPrepend()) {
$nodeValue = $field->getPrepend();
$options = ['childrenFirst' => false];
}

$container = new Child($element, $nodeValue, $options);

if ($field->hasLabel()) {
$labelFor = $field->getName() . (($field->getNodeName() == 'fieldset') ? '1' : '');
$label = new Child('label', $field->getLabel());
$label->setAttribute('for', $labelFor);
if ($field->getLabelAttributes() !== null) {
if ($field->hasLabelAttributes()) {
$label->setAttributes($field->getLabelAttributes());
}
if ($field->isRequired()) {
Expand All @@ -592,9 +614,9 @@ protected function prepareElement(string $element): void
}
$container->addChild($field);

if ($field->getHint() !== null) {
if ($field->hasHint()) {
$hint = new Child('span', $field->getHint());
if ($field->getHintAttributes() !== null) {
if ($field->hasHintAttributes()) {
$hint->setAttributes($field->getHintAttributes());
}
$container->addChild($hint);
Expand Down Expand Up @@ -625,13 +647,13 @@ protected function prepareDl(): void
}
}

if ($field->getLabel() !== null) {
if ($field->hasLabel()) {
$dt = new Child('dt');
$labelFor = $field->getName() . (($field->getNodeName() == 'fieldset') ? '1' : '');

$label = new Child('label', $field->getLabel());
$label->setAttribute('for', $labelFor);
if ($field->getLabelAttributes() !== null) {
if ($field->hasLabelAttributes()) {
$label->setAttributes($field->getLabelAttributes());
}
if ($field->isRequired()) {
Expand All @@ -645,15 +667,26 @@ protected function prepareDl(): void
$dl->addChild($dt);
}

$dd = new Child('dd');
$nodeValue = null;
$options = [];
if ($field->hasAppend()) {
$nodeValue = $field->getAppend();
$options = ['childrenFirst' => true];
} else if ($field->hasPrepend()) {
$nodeValue = $field->getPrepend();
$options = ['childrenFirst' => false];
}

$dd = new Child('dd', $nodeValue, $options);

if ($field->isErrorPre()) {
$dd->addChildren($errors);
}
$dd->addChild($field);

if ($field->getHint() !== null) {
if ($field->hasHint()) {
$hint = new Child('span', $field->getHint());
if ($field->getHintAttributes() !== null) {
if ($field->hasHintAttributes()) {
$hint->setAttributes($field->getHintAttributes());
}
$dd->addChild($hint);
Expand Down

0 comments on commit 32a8759

Please sign in to comment.