-
Notifications
You must be signed in to change notification settings - Fork 2
FormControl
Pair\Html\FormControl is the abstract base class for all Pair form controls (Text, Email, Select, Textarea, Checkbox, etc.).
It centralizes:
- HTML attribute composition
- value/label metadata
- client+server validation flags (
required, lengths, pattern) - common rendering helpers used by subclasses
new FormControl(string $name, array $attributes = [])Behavior:
- If name ends with
[], Pair strips the suffix and automatically enables array mode (arrayName = true). - Custom attributes passed in constructor are stored in
$attributesand rendered later.
Identity and metadata:
id(string $id): staticlabel(string $label): staticlabelClass(string $class): staticdescription(string $description): static-
caption(string $caption): static(only forButton,Meter,Progress,Textarea)
State:
required(bool $required = true): staticdisabled(bool $disabled = true): staticreadonly(bool $readonly = true): staticvalue(string|int|float|DateTime|null $value): static
Input rules:
minLength(int $length): staticmaxLength(int $length): staticpattern(string $pattern): staticplaceholder(string $placeholder): staticinputmode(string $mode): staticautofocus(bool $autofocus = true): static
Attribute helpers:
class(string|array $class): staticdata(string $name, string $value): staticaria(string $name, string $value): statictitle(string $title): staticform(string $formId): staticarrayName(): static
Output and validation:
-
render(): string(abstract, implemented by concrete controls) validate(): boolprint(): voidprintLabel(): void-
__toString(): string(delegates torender())
FormControl enforces some API restrictions.
caption(...) allowed only on:
ButtonMeterProgressTextarea
pattern(...) allowed only on:
TextSearchTelEmailPasswordUrl
placeholder(...) is blocked on:
CheckboxRadioFileColorRangeHidden
aria(...) is ignored (no exception) on:
HiddenFileImage
inputmode(...) is ignored (no exception) on:
-
Button,Checkbox,Color,Date,Datetime,File,Hidden,Image,Month,Password,Select,Textarea,Time
Protected helpers:
-
nameProperty(): stringbuilds safename="..."(with[]when array mode is active) -
processProperties(): stringappends common HTML attributes -
renderInput(string $type): stringbuilds default<input>markup for simple controls
processProperties() currently handles:
-
id,required,disabled,readonly,placeholder,pattern, CSSclass - all key/value attributes in
$attributes
required HTML attribute is not printed for Checkbox and Radio.
getLabelText() behavior:
- if label is not set, Pair derives it from field name (
userEmail->User Email) - if label is uppercase and longer than 3 chars, it is treated as translation key (
Translator::do(...)) - otherwise, raw label text is used
printLabel() adds:
-
<span class="required-field">...</span>when field is required and not disabled/readonly - optional tooltip icon when
description(...)is set
Default validate() checks POST value (Post::get($this->name)) and validates:
- required not empty
- min length
- max length
Failures are logged through Logger and return false.
Concrete controls may override validate() (for example Email, Url, Number, Select, etc.).
Basic text field:
$title = (new \Pair\Html\FormControls\Text('title'))
->label('Title')
->required()
->minLength(3)
->maxLength(120)
->placeholder('Insert title')
->class(['form-control', 'form-control-lg']);
echo $title->render();Array-style values:
$tags = (new \Pair\Html\FormControls\Text('tags[]'))
->arrayName()
->placeholder('Tag');Button caption (allowed) vs text caption (not allowed):
$save = (new \Pair\Html\FormControls\Button('save'))->caption('Save');
// This throws AppException:
// (new \Pair\Html\FormControls\Text('name'))->caption('X');-
class('a b')is treated as one class string; useclass(['a', 'b'])to add multiple classes explicitly. -
title(...)currently applies to all controls (the internal unsupported list is declared but not enforced). -
__get()throwsAppExceptionwhen property does not exist (ErrorCodes::PROPERTY_NOT_FOUND).
See also: Form, FormControls, Post, Translator.