Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods name() and value() for input fields #281

Merged
merged 4 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Field/Base/DateTimeInputField.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ protected function beforeRender(): void

final protected function generateInput(): string
{
$value = $this->getInputData()->getValue();
$value = $this->getValue();

if (!is_string($value) && $value !== null) {
throw new InvalidArgumentException(
Expand All @@ -172,7 +172,7 @@ final protected function generateInput(): string

$inputAttributes = $this->getInputAttributes();

return Html::input($this->getInputType(), $this->getInputData()->getName(), $value, $inputAttributes)->render();
return Html::input($this->getInputType(), $this->getName(), $value, $inputAttributes)->render();
}

abstract protected function getInputType(): string;
Expand Down
15 changes: 10 additions & 5 deletions src/Field/Base/InputData/FormModelInputData.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function getValidationRules(): iterable
*
* @throws InvalidArgumentException If the attribute name contains non-word characters or empty form name for
* tabular inputs.
* @return string The generated input name.
* @return string|null The generated input name.
*/
public function getName(): string
public function getName(): ?string
{
$data = $this->parseProperty($this->property);
$formName = $this->model->getFormName();
Expand Down Expand Up @@ -102,11 +102,16 @@ public function getPlaceholder(): ?string
* For example, if {@see getInputName()} returns `Post[content]`, this method will return `post-content`.
*
* @throws InvalidArgumentException If the attribute name contains non-word characters.
* @return string The generated input ID.
* @return string|null The generated input ID.
*/
public function getId(): string
public function getId(): ?string
{
$name = mb_strtolower($this->getName(), 'UTF-8');
$name = $this->getName();
if ($name === null) {
return null;
}
Tigrov marked this conversation as resolved.
Show resolved Hide resolved

$name = mb_strtolower($name, 'UTF-8');
return str_replace(['[]', '][', '[', ']', ' ', '.'], ['', '-', '-', '', '-', '-'], $name);
}

Expand Down
62 changes: 62 additions & 0 deletions src/Field/Base/InputData/InputDataWithCustomNameAndValueTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Form\Field\Base\InputData;

trait InputDataWithCustomNameAndValueTrait
{
private ?InputDataInterface $inputData = null;
private bool $useCustomName = false;
private ?string $customName = null;
private bool $useCustomValue = false;
private mixed $customValue = null;

final public function inputData(InputDataInterface $inputData): static
{
$new = clone $this;
$new->inputData = $inputData;
$new->useCustomName = false;
$new->useCustomValue = false;
return $new;
}

final protected function getInputData(): InputDataInterface
{
if ($this->inputData === null) {
$this->inputData = new PureInputData();
}

return $this->inputData;
}

final public function name(?string $name): static
{
$new = clone $this;
$new->customName = $name;
$new->useCustomName = true;
return $new;
}

final public function value(mixed $value): static
{
$new = clone $this;
$new->customValue = $value;
$new->useCustomValue = true;
return $new;
}

final protected function getName(): ?string
{
return $this->useCustomName
? $this->customName
: $this->getInputData()->getName();
}

final protected function getValue(): mixed
{
return $this->useCustomValue
? $this->customValue
: $this->getInputData()->getValue();
}
}
4 changes: 2 additions & 2 deletions src/Field/Base/InputField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace Yiisoft\Form\Field\Base;

use Yiisoft\Form\Field\Base\InputData\InputDataTrait;
use Yiisoft\Form\Field\Base\InputData\InputDataWithCustomNameAndValueTrait;
use Yiisoft\Form\Field\Part\Error;
use Yiisoft\Form\Field\Part\Hint;
use Yiisoft\Form\Field\Part\Label;
use Yiisoft\Html\Html;

abstract class InputField extends PartsField
{
use InputDataTrait;
use InputDataWithCustomNameAndValueTrait;

protected ?string $inputId = null;
protected ?string $inputIdFromTag = null;
Expand Down
4 changes: 2 additions & 2 deletions src/Field/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function uncheckValue(bool|float|int|string|Stringable|null $value): self

protected function generateInput(): string
{
$value = $this->getInputData()->getValue();
$value = $this->getValue();

if (!is_bool($value)
&& !is_string($value)
Expand All @@ -237,7 +237,7 @@ protected function generateInput(): string
unset($inputAttributes['value']);
$inputValue ??= '1';

$checkbox = Html::checkbox($this->getInputData()->getName(), $inputValue, $inputAttributes);
$checkbox = Html::checkbox($this->getName(), $inputValue, $inputAttributes);

$label = $this->inputLabel ?? $this->getInputData()->getLabel();

Expand Down
8 changes: 4 additions & 4 deletions src/Field/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use InvalidArgumentException;
use LogicException;
use Stringable;
use Yiisoft\Form\Field\Base\InputData\InputDataTrait;
use Yiisoft\Form\Field\Base\InputData\InputDataWithCustomNameAndValueTrait;
use Yiisoft\Form\Field\Base\PartsField;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
Expand All @@ -25,7 +25,7 @@
*/
final class CheckboxList extends PartsField implements ValidationClassInterface
{
use InputDataTrait;
use InputDataWithCustomNameAndValueTrait;
use ValidationClassTrait;

private CheckboxListWidget $widget;
Expand Down Expand Up @@ -144,12 +144,12 @@ public function itemFormatter(?Closure $formatter): self

protected function generateInput(): string
{
$name = $this->getInputData()->getName();
$name = $this->getName();
if (empty($name)) {
throw new LogicException('"CheckboxList" field requires non-empty name.');
}

$value = $this->getInputData()->getValue();
$value = $this->getValue();

$value ??= [];
if (!is_iterable($value)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Field/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,15 @@ protected function beforeRender(): void

protected function generateInput(): string
{
$value = $this->getInputData()->getValue();
$value = $this->getValue();

if (!is_string($value) && $value !== null) {
throw new InvalidArgumentException('Email field requires a string or null value.');
}

$attributes = $this->getInputAttributes();

return Html::input('email', $this->getInputData()->getName(), $value, $attributes)->render();
return Html::input('email', $this->getName(), $value, $attributes)->render();
}

protected function prepareContainerAttributes(array &$attributes): void
Expand Down
10 changes: 1 addition & 9 deletions src/Field/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ final class File extends InputField implements EnrichmentFromRulesInterface, Val

private bool|float|int|string|Stringable|null $uncheckValue = null;
private array $uncheckInputAttributes = [];
private string|Stringable|null $value = null;

/**
* The accept attribute value is a string that defines the file types the file input should accept. This string is
Expand Down Expand Up @@ -153,13 +152,6 @@ public function addUncheckInputAttributes(array $attributes): self
return $new;
}

public function value(string|Stringable|null $value): self
{
$new = clone $this;
$new->value = $value;
return $new;
}

/**
* @psalm-suppress MixedAssignment,MixedArgument
*/
Expand All @@ -183,7 +175,7 @@ protected function generateInput(): string
{
$inputAttributes = $this->getInputAttributes();

$tag = Html::file($this->getInputData()->getName(), $this->value, $inputAttributes);
$tag = Html::file($this->getName(), attributes: $inputAttributes);
if ($this->uncheckValue !== null) {
$tag = $tag->uncheckValue($this->uncheckValue);
if (!empty($this->uncheckInputAttributes)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Field/Hidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ final class Hidden extends InputField

protected function generateInput(): string
{
$value = $this->getInputData()->getValue();
$value = $this->getValue();

if (!is_string($value) && !is_numeric($value) && $value !== null) {
throw new InvalidArgumentException('Hidden widget requires a string, numeric or null value.');
}

$inputAttributes = $this->getInputAttributes();

return Html::hiddenInput($this->getInputData()->getName(), $value, $inputAttributes)->render();
return Html::hiddenInput($this->getName(), $value, $inputAttributes)->render();
}
}
4 changes: 2 additions & 2 deletions src/Field/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,15 @@ protected function beforeRender(): void

protected function generateInput(): string
{
$value = $this->getInputData()->getValue();
$value = $this->getValue();

if (!is_numeric($value) && $value !== null) {
throw new InvalidArgumentException('Number field requires a numeric or null value.');
}

$inputAttributes = $this->getInputAttributes();

return Html::input('number', $this->getInputData()->getName(), $value, $inputAttributes)->render();
return Html::input('number', $this->getName(), $value, $inputAttributes)->render();
}

protected function prepareContainerAttributes(array &$attributes): void
Expand Down
4 changes: 2 additions & 2 deletions src/Field/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,15 @@ protected function beforeRender(): void

protected function generateInput(): string
{
$value = $this->getInputData()->getValue();
$value = $this->getValue();

if (!is_string($value) && $value !== null) {
throw new InvalidArgumentException('Password field requires a string or null value.');
}

$inputAttributes = $this->getInputAttributes();

return Html::passwordInput($this->getInputData()->getName(), $value, $inputAttributes)->render();
return Html::passwordInput($this->getName(), $value, $inputAttributes)->render();
}

protected function prepareContainerAttributes(array &$attributes): void
Expand Down
8 changes: 4 additions & 4 deletions src/Field/RadioList.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use InvalidArgumentException;
use LogicException;
use Stringable;
use Yiisoft\Form\Field\Base\InputData\InputDataTrait;
use Yiisoft\Form\Field\Base\InputData\InputDataWithCustomNameAndValueTrait;
use Yiisoft\Form\Field\Base\PartsField;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
Expand All @@ -23,7 +23,7 @@
*/
final class RadioList extends PartsField implements ValidationClassInterface
{
use InputDataTrait;
use InputDataWithCustomNameAndValueTrait;
use ValidationClassTrait;

private RadioListWidget $widget;
Expand Down Expand Up @@ -142,12 +142,12 @@ public function itemFormatter(?Closure $formatter): self

protected function generateInput(): string
{
$name = $this->getInputData()->getName();
$name = $this->getName();
if (empty($name)) {
throw new LogicException('"RadioList" field requires non-empty name.');
}

$value = $this->getInputData()->getValue();
$value = $this->getValue();

if (!is_bool($value)
&& !is_string($value)
Expand Down
4 changes: 2 additions & 2 deletions src/Field/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ protected function beforeRender(): void

protected function generateInput(): string
{
$value = $this->getInputData()->getValue();
$value = $this->getValue();

if (!is_string($value) && !is_numeric($value) && $value !== null) {
throw new InvalidArgumentException('Range field requires a string, numeric or null value.');
}

$tag = Html::range($this->getInputData()->getName(), $value, $this->getInputAttributes());
$tag = Html::range($this->getName(), $value, $this->getInputAttributes());
if ($this->showOutput) {
$tag = $tag
->showOutput()
Expand Down
4 changes: 2 additions & 2 deletions src/Field/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ protected function beforeRender(): void

protected function generateInput(): string
{
$value = $this->getInputData()->getValue();
$value = $this->getValue();
$multiple = (bool) ($this->inputAttributes['multiple'] ?? false);

if ($multiple) {
Expand Down Expand Up @@ -290,7 +290,7 @@ protected function generateInput(): string

return $this->select
->addAttributes($selectAttributes)
->name($this->getInputData()->getName())
->name($this->getName())
->values($value)
->render();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Field/Telephone.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,15 @@ protected function beforeRender(): void

protected function generateInput(): string
{
$value = $this->getInputData()->getValue();
$value = $this->getValue();

if (!is_string($value) && $value !== null) {
throw new InvalidArgumentException('Telephone field requires a string or null value.');
}

$inputAttributes = $this->getInputAttributes();

return Html::input('tel', $this->getInputData()->getName(), $value, $inputAttributes)->render();
return Html::input('tel', $this->getName(), $value, $inputAttributes)->render();
}

protected function prepareContainerAttributes(array &$attributes): void
Expand Down
4 changes: 2 additions & 2 deletions src/Field/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ protected function beforeRender(): void

protected function generateInput(): string
{
$value = $this->getInputData()->getValue();
$value = $this->getValue();

if (!is_string($value) && $value !== null) {
throw new InvalidArgumentException('Text field requires a string or null value.');
}

$inputAttributes = $this->getInputAttributes();

return Html::textInput($this->getInputData()->getName(), $value, $inputAttributes)->render();
return Html::textInput($this->getName(), $value, $inputAttributes)->render();
}

protected function prepareContainerAttributes(array &$attributes): void
Expand Down
Loading