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 Mar 2, 2021
1 parent 81d781f commit 43ca93a
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 112 deletions.
66 changes: 34 additions & 32 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 @@ -275,22 +276,24 @@ public function getForm(bool $throw = true): ?Form

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


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

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


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

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

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


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

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


/**
* Adds set of radio button controls to the form.
* @param string|object $label
*/
public function addRadioList(string $name, $label = null, array $items = null): Controls\RadioList
public function addRadioList(string $name, string|Stringable $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 $label
*/
public function addCheckboxList(string $name, $label = null, array $items = null): Controls\CheckboxList
{
public function addCheckboxList(
string $name,
string|Stringable $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 $label
*/
public function addSelect(string $name, $label = null, array $items = null, int $size = null): Controls\SelectBox
{
public function addSelect(
string $name,
string|Stringable $label = null,
array $items = null,
int $size = null,
): Controls\SelectBox {
return $this[$name] = (new Controls\SelectBox($label, $items))
->setHtmlAttribute('size', $size > 1 ? $size : null);
}


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

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


/**
* Adds push buttons with no default behavior.
* @param string|object $caption
*/
public function addButton(string $name, $caption = null): Controls\Button
public function addButton(string $name, string|Stringable $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 mixed $htmlId
* @property mixed $value
* @property string|object $caption
* @property string|Stringable $caption
* @property bool $disabled
* @property bool $omitted
* @property-read Html $control
Expand Down Expand Up @@ -55,7 +56,7 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements Con
/** @var callable[][] extension methods */
private static $extMethods = [];

private string|object|null $caption;
private string|Stringable|null $caption;

private array $errors = [];

Expand All @@ -68,10 +69,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 $caption = null)
{
$this->control = Html::el('input', ['type' => null, 'name' => null]);
$this->label = Html::el('label');
Expand All @@ -88,18 +86,16 @@ public function __construct($caption = null)

/**
* Sets textual caption or label.
* @param object|string $caption
* @return static
*/
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 @@ -251,9 +247,8 @@ public function getControl(): Html|string

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

/**
* Adds a validation rule.
* @param string|object $errorMessage
*/
public function addRule(callable|string $validator, $errorMessage = null, mixed $arg = null): static
{
public function addRule(
callable|string $validator,
string|Stringable $errorMessage = null,
mixed $arg = null,
): static {
$this->rules->addRule($validator, $errorMessage, $arg);
return $this;
}
Expand Down Expand Up @@ -434,9 +431,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 @@ -467,9 +463,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
9 changes: 3 additions & 6 deletions src/Forms/Controls/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@
namespace Nette\Forms\Controls;

use Nette\Utils\Html;
use Stringable;


/**
* Push button control with no default behavior.
*/
class Button extends BaseControl
{
/**
* @param string|object $caption
*/
public function __construct($caption = null)
public function __construct(string|Stringable $caption = null)
{
parent::__construct($caption);
$this->control->type = 'button';
Expand Down Expand Up @@ -49,9 +47,8 @@ public function getLabel($caption = null): Html|string|null

/**
* Generates control's HTML element.
* @param string|object $caption
*/
public function getControl($caption = null): Html
public function getControl(string|Stringable $caption = null): Html
{
$this->setOption('rendered', true);
$el = clone $this->control;
Expand Down
6 changes: 2 additions & 4 deletions src/Forms/Controls/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Nette;
use Nette\Utils\Html;
use Stringable;


/**
Expand All @@ -22,10 +23,7 @@ class Checkbox extends BaseControl
private Html $container;


/**
* @param string|object $label
*/
public function __construct($label = null)
public function __construct(string|Stringable $label = null)
{
parent::__construct($label);
$this->control->type = 'checkbox';
Expand Down
6 changes: 2 additions & 4 deletions src/Forms/Controls/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Nette;
use Nette\Utils\Html;
use Stringable;


/**
Expand All @@ -32,10 +33,7 @@ class CheckboxList extends MultiChoiceControl
protected Html $itemLabel;


/**
* @param string|object $label
*/
public function __construct($label = null, array $items = null)
public function __construct(string|Stringable $label = null, array $items = null)
{
parent::__construct($label, $items);
$this->control->type = 'checkbox';
Expand Down
6 changes: 2 additions & 4 deletions src/Forms/Controls/CsrfProtection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Nette;
use Nette\Application\UI\Presenter;
use Stringable;


/**
Expand All @@ -23,10 +24,7 @@ class CsrfProtection extends HiddenField
public ?Nette\Http\Session $session = null;


/**
* @param string|object $errorMessage
*/
public function __construct($errorMessage)
public function __construct(string|Stringable $errorMessage = null)
{
parent::__construct();
$this->setOmitted()
Expand Down
Loading

0 comments on commit 43ca93a

Please sign in to comment.