@@ -13,6 +13,54 @@ re-population and layout. The FormHelper is also flexible - it will do almost
1313everything for you using conventions, or you can use specific methods to get
1414only what you need.
1515
16+ ## Rendering Trusted HTML
17+
18+ FormHelper accepts [ HtmlString] ( ../../views#rendering-trusted-html ) and other
19+ ` Cake\View\HtmlStringable ` values in the following content positions:
20+
21+ - Captions for ` button() ` and ` postButton() ` , and content for ` postLink() ` and
22+ ` deleteLink() ` .
23+ - Label text for ` label() ` , the ` label ` option of ` control() ` , and the ` text `
24+ key of a label options array.
25+ - Option labels for ` radio() ` and ` multiCheckbox() ` .
26+ - Explicit error messages passed to ` error() ` or the ` error ` option of
27+ ` control() ` , including individual messages in an array of overrides.
28+
29+ These values render without escaping their markup, while ordinary strings
30+ continue to follow the existing escaping options:
31+
32+ ``` php
33+ use function Cake\View\html;
34+
35+ echo $this->Form->button(html('<strong >Save</strong >'));
36+
37+ echo $this->Form->control('name', [
38+ 'label' => html('<strong >Your name</strong >'),
39+ ]);
40+
41+ echo $this->Form->radio('priority', [
42+ ['value' => 'high', 'text' => html('<strong >High</strong >')],
43+ ['value' => 'normal', 'text' => 'Normal'],
44+ ]);
45+
46+ // Override one message while retaining other validation messages.
47+ echo $this->Form->error('name', [
48+ '_empty' => html('<strong >Please enter your name.</strong >'),
49+ ]);
50+ ```
51+
52+ Only explicit error overrides are treated as trusted HTML. Messages obtained
53+ from the form context, including fallback messages without an override, remain
54+ escaped by default even if they implement ` HtmlStringable ` .
55+
56+ Wrapping a value does not bypass escaping in attributes, input values,
57+ textarea contents, or select option text. Only wrap markup you control;
58+ ` HtmlString ` does not sanitize HTML.
59+
60+ ::: info Added in version 6.0.0
61+ Support for ` HtmlStringable ` content was added.
62+ :::
63+
1664## Starting a Form
1765
1866` method ` Cake\\ View\\ Helper\\ FormHelper::** create** (mixed $context = null, array $options = [ ] ): string
@@ -1833,10 +1881,10 @@ If it was 2009, you would get the following:
18331881
18341882## Creating Labels
18351883
1836- ` method ` Cake\\ View\\ Helper\\ FormHelper::** label** (string $fieldName, string $text, array $options): string
1884+ ` method ` Cake\\ View\\ Helper\\ FormHelper::** label** (string $fieldName, HtmlStringable| string|null $text = null , array $options = [ ] ): string
18371885
18381886- ` $fieldName ` - A field name in the form ` 'Modelname.fieldname' ` .
1839- - ` $text ` - An optional string providing the label caption text .
1887+ - ` $text ` - An optional string or ` HtmlStringable ` value providing the label caption.
18401888- ` $options ` - Optional. Array containing any of the
18411889 [ General Control Options] ( #general-control-options ) as well as any valid HTML attributes.
18421890
@@ -1882,7 +1930,7 @@ field errors and when necessary display customized error messages.
18821930` method ` Cake\\ View\\ Helper\\ FormHelper::** error** (string $fieldName, mixed $text, array $options): string
18831931
18841932- ` $fieldName ` - A field name in the form ` 'Modelname.fieldname' ` .
1885- - ` $text ` - Optional. A string or array providing the error message(s). If an
1933+ - ` $text ` - Optional. A string, ` HtmlStringable ` value, or array providing the error message(s). If an
18861934 array, then it should be a hash of key names => messages. Defaults to
18871935 ` null ` .
18881936- ` $options ` - An optional array that can only contain a boolean with the key
@@ -2053,15 +2101,15 @@ more complex button content you should use `button()`.
20532101
20542102### Creating Button Elements
20552103
2056- ` method ` Cake\\ View\\ Helper\\ FormHelper::** button** (string $title , array $options = [ ] ): string
2104+ ` method ` Cake\\ View\\ Helper\\ FormHelper::** button** (HtmlStringable| string $content , array $options = [ ] ): string
20572105
2058- - ` $title ` - Mandatory string providing the button's text caption.
2106+ - ` $content ` - Mandatory string or ` HtmlStringable ` value providing the button's caption.
20592107- ` $options ` - An optional array including any of the
20602108 [ General Control Options] ( #general-control-options ) , or of the specific button options (see below)
20612109 as well as any valid HTML attributes.
20622110
2063- Creates an HTML button with the specified title and a default type
2064- of ` 'button ' ` .
2111+ Creates an HTML button with the specified content and a default type
2112+ of ` 'submit ' ` .
20652113
20662114** Options for Button**
20672115
@@ -2072,9 +2120,9 @@ of `'button'`.
20722120 as ` submit() ` does. This is the default type.
20732121 2 . ` 'reset' ` - Creates a form reset button.
20742122 3 . ` 'button' ` - Creates a standard push button.
2075- - ` escapeTitle ` - Boolean. If set to ` true ` it will HTML encode
2076- the value provided inside ` $title ` . Defaults to ` true ` .
20772123- ` escape ` - Boolean. If set to ` true ` it will HTML encode
2124+ ordinary strings provided inside ` $content ` . Defaults to ` true ` .
2125+ - ` escapeAttributes ` - Boolean. If set to ` true ` it will HTML encode
20782126 all the HTML attributes generated for the button. Defaults to ` true ` .
20792127- ` confirm ` - The confirmation message to display on click. Defaults to
20802128 ` null ` .
@@ -2097,13 +2145,13 @@ Will output:
20972145<button type =" submit" >Submit Form</button >
20982146```
20992147
2100- Example use of the ` 'escapeTitle ' ` option:
2148+ Example use of the ` 'escape ' ` option:
21012149
21022150``` php
21032151// Will render unescaped HTML.
21042152echo $this->Form->button('<em >Submit Form</em >', [
21052153 'type' => 'submit',
2106- 'escapeTitle ' => false,
2154+ 'escape ' => false,
21072155]);
21082156```
21092157
@@ -2165,10 +2213,10 @@ template option on `FormHelper`.
21652213
21662214### Creating POST Buttons
21672215
2168- ` method ` Cake\\ View\\ Helper\\ FormHelper::** postButton** (string $title, mixed $url, array $options = [ ] ): string
2216+ ` method ` Cake\\ View\\ Helper\\ FormHelper::** postButton** (HtmlStringable| string $content, array|string $url, array $options = [ ] ): string
21692217
2170- - ` $title ` - Mandatory string providing the button's text caption. By default
2171- not HTML encoded.
2218+ - ` $content ` - Mandatory string or ` HtmlStringable ` value providing the button's
2219+ caption. Ordinary strings are HTML encoded by default .
21722220- ` $url ` - The URL of the form provided as a string or as array.
21732221- ` $options ` - An optional array including any of the
21742222 [ General Control Options] ( #general-control-options ) , or of the specific options (see below) as well
@@ -2218,9 +2266,9 @@ inside opened forms.
22182266
22192267### Creating POST Links
22202268
2221- ` method ` Cake\\ View\\ Helper\\ FormHelper::** postLink** (string $title , array|string|null $url = null, array $options = [ ] ): string
2269+ ` method ` Cake\\ View\\ Helper\\ FormHelper::** postLink** (HtmlStringable| string $content , array|string|null $url = null, array $options = [ ] ): string
22222270
2223- - ` $title ` - Mandatory string providing the text to be wrapped in ` <a> `
2271+ - ` $content ` - Mandatory string or ` HtmlStringable ` value to be wrapped in ` <a> `
22242272 tags.
22252273- ` $url ` - Optional. String or array which contains the URL
22262274 of the form (Cake-relative or external URL starting with ` http:// ` ).
@@ -2269,9 +2317,9 @@ use `Cake\View\Helper\FormHelper::button()` or
22692317
22702318### Creating DELETE Links
22712319
2272- ` method ` Cake\\ View\\ Helper\\ FormHelper::** deleteLink** (string $title , array|string|null $url = null, array $options = [ ] ): string
2320+ ` method ` Cake\\ View\\ Helper\\ FormHelper::** deleteLink** (HtmlStringable| string $content , array|string|null $url = null, array $options = [ ] ): string
22732321
2274- - ` $title ` - Mandatory string providing the text to be wrapped in ` <a> `
2322+ - ` $content ` - Mandatory string or ` HtmlStringable ` value to be wrapped in ` <a> `
22752323 tags.
22762324- ` $url ` - Optional. String or array which contains the URL
22772325 of the form (Cake-relative or external URL starting with ` http:// ` ).
0 commit comments