Skip to content

Commit

Permalink
used 'string|Stringable' for labels and messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 8, 2024
1 parent dffc07d commit 01039d1
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 120 deletions.
2 changes: 1 addition & 1 deletion src/Forms/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected function receiveHttpData(): ?array
public $inner;


public function getLabel($caption = null): Html|string|null
public function getLabel(string|\Stringable|null $caption = null): Html|string|null
{
return $this->inner->getLabel()
? '{label ' . $this->inner->lookupPath(Form::class) . '/}'
Expand Down
89 changes: 51 additions & 38 deletions src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Nette;
use Nette\Utils\ArrayHash;
use Stringable;


/**
Expand Down Expand Up @@ -314,9 +315,13 @@ public function getForm(bool $throw = true): ?Form

/**
* Adds single-line text input control to the form.
* @param string|object|null $label
*/
public function addText(string $name, $label = null, ?int $cols = null, ?int $maxLength = null): Controls\TextInput
public function addText(
string $name,
string|Stringable|null $label = null,
?int $cols = null,
?int $maxLength = null,
): Controls\TextInput
{
return $this[$name] = (new Controls\TextInput($label, $maxLength))
->setHtmlAttribute('size', $cols);
Expand All @@ -325,11 +330,10 @@ public function addText(string $name, $label = null, ?int $cols = null, ?int $ma

/**
* Adds single-line text input control used for sensitive input such as passwords.
* @param string|object|null $label
*/
public function addPassword(
string $name,
$label = null,
string|Stringable|null $label = null,
?int $cols = null,
?int $maxLength = null,
): Controls\TextInput
Expand All @@ -342,9 +346,13 @@ public function addPassword(

/**
* Adds multi-line text input control to the form.
* @param string|object|null $label
*/
public function addTextArea(string $name, $label = null, ?int $cols = null, ?int $rows = null): Controls\TextArea
public function addTextArea(
string $name,
string|Stringable|null $label = null,
?int $cols = null,
?int $rows = null,
): Controls\TextArea
{
return $this[$name] = (new Controls\TextArea($label))
->setHtmlAttribute('cols', $cols)->setHtmlAttribute('rows', $rows);
Expand All @@ -353,9 +361,8 @@ public function addTextArea(string $name, $label = null, ?int $cols = null, ?int

/**
* Adds input for email.
* @param string|object|null $label
*/
public function addEmail(string $name, $label = null): Controls\TextInput
public function addEmail(string $name, string|Stringable|null $label = null): Controls\TextInput
{
return $this[$name] = (new Controls\TextInput($label))
->addRule(Form::Email);
Expand All @@ -364,9 +371,8 @@ public function addEmail(string $name, $label = null): Controls\TextInput

/**
* Adds input for integer.
* @param string|object|null $label
*/
public function addInteger(string $name, $label = null): Controls\TextInput
public function addInteger(string $name, string|Stringable|null $label = null): Controls\TextInput
{
return $this[$name] = (new Controls\TextInput($label))
->setNullable()
Expand All @@ -376,9 +382,8 @@ public function addInteger(string $name, $label = null): Controls\TextInput

/**
* Adds input for float.
* @param string|object|null $label
*/
public function addFloat(string $name, $label = null): Controls\TextInput
public function addFloat(string $name, string|Stringable|null $label = null): Controls\TextInput
{
return $this[$name] = (new Controls\TextInput($label))
->setNullable()
Expand All @@ -390,49 +395,52 @@ public function addFloat(string $name, $label = null): Controls\TextInput

/**
* Adds input for date selection.
* @param string|object|null $label
*/
public function addDate(string $name, $label = null): Controls\DateTimeControl
public function addDate(string $name, string|object|null $label = null): Controls\DateTimeControl
{
return $this[$name] = new Controls\DateTimeControl($label, Controls\DateTimeControl::TypeDate);
}


/**
* Adds input for time selection.
* @param string|object|null $label
*/
public function addTime(string $name, $label = null, bool $withSeconds = false): Controls\DateTimeControl
public function addTime(
string $name,
string|object|null $label = null,
bool $withSeconds = false,
): Controls\DateTimeControl
{
return $this[$name] = new Controls\DateTimeControl($label, Controls\DateTimeControl::TypeTime, $withSeconds);
}


/**
* Adds input for date and time selection.
* @param string|object|null $label
*/
public function addDateTime(string $name, $label = null, bool $withSeconds = false): Controls\DateTimeControl
public function addDateTime(
string $name,
string|object|null $label = null,
bool $withSeconds = false,
): Controls\DateTimeControl
{
return $this[$name] = new Controls\DateTimeControl($label, Controls\DateTimeControl::TypeDateTime, $withSeconds);
}


/**
* Adds control that allows the user to upload files.
* @param string|object|null $label
*/
public function addUpload(string $name, $label = null): Controls\UploadControl
public function addUpload(string $name, string|Stringable|null $label = null): Controls\UploadControl
{
return $this[$name] = new Controls\UploadControl($label, multiple: false);
}


/**
* Adds control that allows the user to upload multiple files.
* @param string|object|null $label
*/
public function addMultiUpload(string $name, $label = null): Controls\UploadControl
public function addMultiUpload(string $name, string|Stringable|null $label = null): Controls\UploadControl
{
return $this[$name] = new Controls\UploadControl($label, multiple: true);
}
Expand All @@ -450,39 +458,48 @@ public function addHidden(string $name, $default = null): Controls\HiddenField

/**
* Adds check box control to the form.
* @param string|object|null $caption
*/
public function addCheckbox(string $name, $caption = null): Controls\Checkbox
public function addCheckbox(string $name, string|Stringable|null $caption = null): Controls\Checkbox
{
return $this[$name] = new Controls\Checkbox($caption);
}


/**
* Adds set of radio button controls to the form.
* @param string|object|null $label
*/
public function addRadioList(string $name, $label = null, ?array $items = null): Controls\RadioList
public function addRadioList(
string $name,
string|Stringable|null $label = null,
?array $items = null,
): Controls\RadioList
{
return $this[$name] = new Controls\RadioList($label, $items);
}


/**
* Adds set of checkbox controls to the form.
* @param string|object|null $label
*/
public function addCheckboxList(string $name, $label = null, ?array $items = null): Controls\CheckboxList
public function addCheckboxList(
string $name,
string|Stringable|null $label = null,
?array $items = null,
): Controls\CheckboxList
{
return $this[$name] = new Controls\CheckboxList($label, $items);
}


/**
* Adds select box control that allows single item selection.
* @param string|object|null $label
*/
public function addSelect(string $name, $label = null, ?array $items = null, ?int $size = null): Controls\SelectBox
public function addSelect(
string $name,
string|Stringable|null $label = null,
?array $items = null,
?int $size = null,
): Controls\SelectBox
{
return $this[$name] = (new Controls\SelectBox($label, $items))
->setHtmlAttribute('size', $size > 1 ? $size : null);
Expand All @@ -491,11 +508,10 @@ public function addSelect(string $name, $label = null, ?array $items = null, ?in

/**
* Adds select box control that allows multiple item selection.
* @param string|object|null $label
*/
public function addMultiSelect(
string $name,
$label = null,
string|Stringable|null $label = null,
?array $items = null,
?int $size = null,
): Controls\MultiSelectBox
Expand All @@ -507,29 +523,26 @@ public function addMultiSelect(

/**
* Adds color picker.
* @param string|object|null $label
*/
public function addColor(string $name, $label = null): Controls\ColorPicker
public function addColor(string $name, string|Stringable|null $label = null): Controls\ColorPicker
{
return $this[$name] = new Controls\ColorPicker($label);
}


/**
* Adds button used to submit form.
* @param string|object|null $caption
*/
public function addSubmit(string $name, $caption = null): Controls\SubmitButton
public function addSubmit(string $name, string|Stringable|null $caption = null): Controls\SubmitButton
{
return $this[$name] = new Controls\SubmitButton($caption);
}


/**
* Adds push buttons with no default behavior.
* @param string|object|null $caption
*/
public function addButton(string $name, $caption = null): Controls\Button
public function addButton(string $name, string|Stringable|null $caption = null): Controls\Button
{
return $this[$name] = new Controls\Button($caption);
}
Expand Down
33 changes: 14 additions & 19 deletions src/Forms/Controls/BaseControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Nette\Forms\Form;
use Nette\Forms\Rules;
use Nette\Utils\Html;
use Stringable;


/**
Expand All @@ -23,7 +24,7 @@
* @property-read string $htmlName
* @property string|bool|null $htmlId
* @property mixed $value
* @property string|object $caption
* @property string|Stringable $caption
* @property bool $disabled
* @property bool $omitted
* @property-read Html $control
Expand All @@ -49,7 +50,7 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements Con

/** @var callable[][] extension methods */
private static array $extMethods = [];
private string|object|null $caption;
private string|Stringable|null $caption;
private array $errors = [];
private ?bool $omitted = null;
private Rules $rules;
Expand All @@ -59,10 +60,7 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements Con
private array $options = [];


/**
* @param string|object $caption
*/
public function __construct($caption = null)
public function __construct(string|Stringable|null $caption = null)
{
$this->control = Html::el('input', ['type' => null, 'name' => null]);
$this->label = Html::el('label');
Expand All @@ -79,17 +77,15 @@ public function __construct($caption = null)

/**
* Sets textual caption or label.
* @param object|string $caption
*/
public function setCaption($caption): static
public function setCaption(string|Stringable $caption): static
{
$this->caption = $caption;
return $this;
}


/** @return object|string */
public function getCaption()
public function getCaption(): string|Stringable|null
{
return $this->caption;
}
Expand Down Expand Up @@ -249,10 +245,9 @@ public function getControl()

/**
* Generates label's HTML element.
* @param string|object $caption
* @return Html|string|null
*/
public function getLabel($caption = null)
public function getLabel(string|Stringable|null $caption = null)
{
$label = clone $this->label;
$label->for = $this->getHtmlId();
Expand Down Expand Up @@ -400,11 +395,13 @@ public function translate($value, ...$parameters): mixed

/**
* Adds a validation rule.
* @param string|object $errorMessage
* @return static
*/
public function addRule(callable|string $validator, $errorMessage = null, mixed $arg = null)
{
public function addRule(
callable|string $validator,
string|Stringable|null $errorMessage = null,
mixed $arg = null,
) {
$this->rules->addRule($validator, $errorMessage, $arg);
return $this;
}
Expand Down Expand Up @@ -446,9 +443,8 @@ public function getRules(): Rules

/**
* Makes control mandatory.
* @param bool|string|object $value
*/
public function setRequired($value = true): static
public function setRequired(string|Stringable|bool $value = true): static
{
$this->rules->setRequired($value);
return $this;
Expand Down Expand Up @@ -480,9 +476,8 @@ public function validate(): void

/**
* Adds error message to the list.
* @param string|object $message
*/
public function addError($message, bool $translate = true): void
public function addError(string|Stringable $message, bool $translate = true): void
{
$this->errors[] = $translate ? $this->translate($message) : $message;
}
Expand Down

0 comments on commit 01039d1

Please sign in to comment.