Skip to content

Commit 0760f68

Browse files
committed
Add code examples for generic form fields
1 parent fe14324 commit 0760f68

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

docs/php/api/form_builder/form_fields.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,26 @@ Objects of this class require a label.
3737
The return value of `getSaveValue()` is the integer representation of the boolean value, i.e. `0` or `1`.
3838
The class implements `IAttributeFormField`, `IAutoFocusFormField`, `ICssClassFormField`, and `IImmutableFormField`.
3939

40+
Example:
41+
42+
```php
43+
BooleanFormField::create('example')
44+
->label('foo.bar.example')
45+
->value(true)
46+
```
47+
4048

4149
### `CheckboxFormField`
4250

4351
`CheckboxFormField` extends `BooleanFormField` and offers a simple HTML checkbox.
4452

53+
Example:
54+
55+
```php
56+
CheckboxFormField::create('example')
57+
->label('foo.bar.example')
58+
```
59+
4560

4661
### `ClassNameFormField`
4762

@@ -58,6 +73,15 @@ The class implements `IAttributeFormField`, `IAutoFocusFormField`, `ICssClassFor
5873

5974
Additionally, the default id of a `ClassNameFormField` object is `className`, the default label is `wcf.form.field.className`, and if either an interface or a parent class is required, a default description is set if no description has already been set (`wcf.form.field.className.description.interface` and `wcf.form.field.className.description.parentClass`, respectively).
6075

76+
Example:
77+
78+
```php
79+
ClassNameFormField::create('example')
80+
->label('foo.bar.example')
81+
->classExists()
82+
->implementedInterface(ExampleInterface::class)
83+
```
84+
6185

6286
### `CurrencyFormField`
6387

@@ -87,44 +111,101 @@ The following methods are specific to this form field class:
87111
- `supportTime($supportsTime = true)` and `supportsTime()` can be used to toggle whether, in addition to a date, a time can also be specified.
88112
By default, specifying a time is disabled.
89113

114+
Example:
115+
116+
```php
117+
DateFormField::create('example')
118+
->label('foo.bar.example')
119+
->saveValueFormat('Y-m-d')
120+
->value(DateUtil::format(DateUtil::getDateTimeByTimestamp(TIME_NOW), 'Y-m-d'))
121+
```
90122

91123
### `DescriptionFormField`
92124

93125
`DescriptionFormField` is a [multi-line text form field](#multilinetextformfield) with `description` as the default id and `wcf.global.description` as the default label.
94126

127+
Example:
128+
129+
```php
130+
DescriptionFormField::create('example')
131+
```
132+
95133

96134
### `EmailFormField`
97135

98136
`EmailFormField` is a form field to enter an email address which is internally validated using `UserUtil::isValidEmail()`.
99137
The class implements `IAttributeFormField`, `IAutoCompleteFormField`, `IAutoFocusFormField`, `ICssClassFormField`, `II18nFormField`, `IImmutableFormField`, `IInputModeFormField`, `IPatternFormField`, and `IPlaceholderFormField`.
100138

139+
Example:
140+
141+
```php
142+
EmailFormField::create('example')
143+
->label('foo.bar.example')
144+
```
145+
101146

102147
### `FloatFormField`
103148

104149
`FloatFormField` is an implementation of [AbstractNumericFormField](#abstractnumericformfield) for floating point numbers.
105150

151+
Example:
152+
153+
```php
154+
FloatFormField::create('example')
155+
->label('foo.bar.example')
156+
->value(0.5)
157+
```
158+
106159

107160
### `HiddenFormField`
108161

109162
`HiddenFormField` is a form field without any user-visible UI.
110163
Even though the form field is invisible to the user, the value can still be modified by the user, e.g. by leveraging the web browsers developer tools.
111164
The `HiddenFormField` *must not* be used to transfer sensitive information or information that the user should not be able to modify.
112165

166+
Example:
167+
168+
```php
169+
HiddenFormField::create('example')
170+
->value('hidden value')
171+
```
172+
113173

114174
### `IconFormField`
115175

116176
`IconFormField` is a form field to select a FontAwesome icon.
117177

178+
Example:
179+
180+
```php
181+
IconFormField::create('example')
182+
->label('foo.bar.example')
183+
```
184+
118185

119186
### `IntegerFormField`
120187

121188
`IntegerFormField` is an implementation of [AbstractNumericFormField](#abstractnumericformfield) for integers.
122189

190+
Example:
191+
192+
```php
193+
IntegerFormField::create('example')
194+
->label('foo.bar.example')
195+
->value(10)
196+
```
123197

124198
### `IsDisabledFormField`
125199

126200
`IsDisabledFormField` is a [boolean form field](#booleanformfield) with `isDisabled` as the default id.
127201

202+
Example:
203+
204+
```php
205+
IsDisabledFormField::create()
206+
->label('foo.bar.example')
207+
```
208+
128209

129210
### `ItemListFormField`
130211

@@ -142,6 +223,14 @@ By default, `ItemListFormField::SAVE_VALUE_TYPE_CSV` is used.
142223

143224
If `ItemListFormField::SAVE_VALUE_TYPE_ARRAY` is used as save value type, `ItemListFormField` objects register a [custom form field data processor](validation_data.md#customformfielddataprocessor) to add the relevant array into the `$parameters` array directly using the object property as the array key.
144225

226+
Example:
227+
228+
```php
229+
ItemListFormField::create('example')
230+
->label('foo.bar.example')
231+
->saveValueType(ItemListFormField::SAVE_VALUE_TYPE_CSV)
232+
```
233+
145234

146235
### `LanguageItemFormNode`
147236

@@ -162,12 +251,32 @@ The methods `rows($rows)` and `getRows()` can be used to set and get the number
162251
The default number of rows is `10`.
163252
These methods do **not**, however, restrict the number of text rows that can be entered.
164253

254+
Example:
255+
256+
```php
257+
MultilineTextFormField::create('example')
258+
->label('foo.bar.example')
259+
->maximumLength(5000)
260+
->rows(5)
261+
```
262+
165263

166264
### `MultipleSelectionFormField`
167265

168266
`MultipleSelectionFormField` is a form fields that allows the selection of multiple options out of a predefined list of available options.
169267
The class implements `IAttributeFormField`, `ICssClassFormField`, `IFilterableSelectionFormField`, and `IImmutableFormField`.
170268

269+
Example:
270+
271+
```php
272+
MultipleSelectionFormField::create('example')
273+
->label('foo.bar.example')
274+
->options([
275+
1 => 'one',
276+
2 => 'two',
277+
])
278+
```
279+
171280

172281
### `NoticeFormNode`
173282

@@ -187,6 +296,16 @@ NoticeFormNode::create('name')
187296
`RadioButtonFormField` is a form fields that allows the selection of a single option out of a predefined list of available options using radiobuttons.
188297
The class implements `IAttributeFormField`, `ICssClassFormField`, `IImmutableFormField`, and `ISelectionFormField`.
189298

299+
Example:
300+
301+
```php
302+
RadioButtonFormField::create('example')
303+
->label('foo.bar.example')
304+
->options([
305+
1 => 'one',
306+
2 => 'two',
307+
])
308+
```
190309

191310
### `RatingFormField`
192311

@@ -198,6 +317,15 @@ When the field is shown, there will be `maximum() - minimum() + 1` icons be show
198317
If a rating values is set, the first `getValue()` icons will instead use the classes that can be set and gotten via `activeCssClasses(array $cssClasses)` and `getActiveCssClasses()`.
199318
By default, the only default class is `star-o` and the active classes are `star` and `orange`.
200319

320+
Example:
321+
322+
```php
323+
RatingFormField::create('example')
324+
->label('foo.bar.example')
325+
->minimum(1)
326+
->maximum(5)
327+
```
328+
201329

202330
### `SelectFormField`
203331

@@ -220,34 +348,73 @@ The default id of instances of this class is `showOrder` and their default label
220348

221349
!!! info "It is important that the relevant object property is always kept updated. Whenever a new object is added or an existing object is edited or delete, the values of the other objects have to be adjusted to ensure consecutive numbering."
222350

351+
Example:
352+
353+
```php
354+
ShowOrderFormField::create('example')
355+
->options(new FooList())
356+
```
357+
223358

224359
### `SingleSelectionFormField`
225360

226361
`SingleSelectionFormField` is a form fields that allows the selection of a single option out of a predefined list of available options.
227362
The class implements `ICssClassFormField`, `IFilterableSelectionFormField`, `IImmutableFormField`, and `INullableFormField`.
228363
If the field is nullable and the current form field value is considered `empty` by PHP, `null` is returned as the save value.
229364

365+
Example:
366+
367+
```php
368+
SingleSelectionFormField::create('example')
369+
->options(['option1', 'option2', 'option3'])
370+
```
371+
230372

231373
### `SortOrderFormField`
232374

233375
`SingleSelectionFormField` is a [single selection form field](#singleselectionformfield) with default id `sortOrder`, default label `wcf.global.showOrder` and default options `ASC: wcf.global.sortOrder.ascending` and `DESC: wcf.global.sortOrder.descending`.
234376

377+
Example:
378+
379+
```php
380+
SingleSelectionFormField::create('example')
381+
```
382+
235383

236384
### `TextFormField`
237385

238386
`TextFormField` is a form field that allows entering a single line of text.
239387
The class implements `IAttributeFormField`, `IAutoCompleteFormField`, `ICssClassFormField`, `IImmutableFormField`, `II18nFormField`, `IInputModeFormField`, `IMaximumLengthFormField`, `IMinimumLengthFormField`, `IPatternFormField`, and `IPlaceholderFormField`.
240388

389+
Example:
390+
391+
```php
392+
TextFormField::create('example')
393+
->label('foo.bar.example')
394+
```
395+
241396

242397
### `TitleFormField`
243398

244399
`TitleFormField` is a [text form field](#textformfield) with `title` as the default id and `wcf.global.title` as the default label.
245400

401+
Example:
402+
403+
```php
404+
TitleFormField::create()
405+
```
406+
246407

247408
### `UrlFormField`
248409

249410
`UrlFormField` is a [text form field](#textformfield) whose values are checked via `Url::is()`.
250411

412+
Example:
413+
414+
```php
415+
UrlFormField::create('example')
416+
->label('foo.bar.example')
417+
```
251418

252419

253420
## Specific Fields

0 commit comments

Comments
 (0)