Form helper library.
Automatic generation of HTML FORMs from given data.
string
'skip'
Constant used internally to skip the securing process, and neither add the field to the hash or to the unlocked fields.
Cake\View\Helper\HtmlHelper
Cake\View\Helper\UrlHelper
Cake\View\View
The View instance this helper is attached to
array<string, mixed>
Runtime config
bool
Whether the config property has already been configured with defaults
Cake\View\Form\ContextInterface|null
Context for the current form.
Cake\View\Form\ContextFactory|null
Context factory.
array<string, mixed>
Default config for the helper.
array<string, array<string>>
Default widgets
array<string>
Grouped input types.
array<string, array>
A helper lookup table used to lazy load helper objects.
string|null
Prefix for id attribute.
array<string>
A list of id suffixes used in the current rendering.
string
The action attribute value of the last created form. Used to make form/request specific hashes for form tampering protection.
Cake\View\Widget\WidgetLocator
Locator for input widgets.
Cake\View\StringTemplate|null
StringTemplate instance.
array<string>
The default sources.
Cake\Form\FormProtector|null
Form protector
array
Other helpers used by FormHelper
string|null
Defines the type of form being created. Set by FormHelper::create().
array<string>
The supported sources that can be used to populate input values.
Missing method handler - implements various simple input types. Is used to create inputs of various types. e.g. $this->Form->text();
will create <input type="text"/>
while $this->Form->range();
will create <input type="range"/>
Construct the widgets and binds the default context providers
Returns an array that can be used to describe the internal state of this object.
Lazy loads helpers.
Clear the stored ID suffixes.
Deletes a single config key.
Reads a config key.
Writes a config key.
Returns a string to be used as onclick handler for confirm dialogs.
Return a CSRF input if the request data is present. Used to secure forms in conjunction with CsrfMiddleware.
Generate an ID suitable for use in an ID attribute.
Extracts a single option from an options array.
Create the URL for a form based on the options.
Find the matching context provider for the data.
Generates an input element
Generate label for input
Generates an group template element
Generate an ID attribute for an element.
Generate an ID suffix.
Sets field defaults and adds field to form security input hash. Will also add the error class if the field contains validation errors.
Generates an input container template
Generate a label for an input() call.
Returns the input type that was guessed for the provided fieldName, based on the internal type it is associated too, its name and the variables that can be found in the view template
Determine if a field is disabled.
Correctly store the last created form action URL.
Magically set option type and corresponding options
Selects the variable containing the options for a select field if present, and sets the value to the 'options' key in the options array.
Generates input options array
Adds the given class to the element options
Add a new context type.
Add a new widget to FormHelper.
Generate a set of controls for $fields
. If $fields is empty the fields of current model will be used.
Creates a <button>
tag.
Creates a checkbox input widget.
Merge provided config with existing config. Unlike config()
which does a recursive merge for nested keys, this method does a simple merge.
Get the context instance for the current form set.
Set the context factory the helper will use.
Generates a form control element complete with label and wrapper div.
Generate a set of controls for $fields
wrapped in a fieldset element.
Returns an HTML form element.
Create FormProtector instance.
Generate an input tag with type "date".
Generate an input tag with type "datetime-local".
Creates input of type email.
Closes an HTML form, cleans up values set by FormHelper::create(), and writes hidden input fields where appropriate.
Returns a formatted error message for given form field, '' if no errors.
Wrap a set of inputs in a fieldset
Creates file input widget.
Formats a template string with $data
Returns the config.
Returns the config for this specific key.
Get form protector instance.
Gets a single field value from the sources available.
Gets templates to use or a specific template.
Gets the value sources.
Get the view instance this helper is bound to.
Get the widget locator currently used by the helper.
Creates a hidden input field.
Event listeners.
Constructor hook method.
Returns true if there is an error for the given field, otherwise false
Returns a formatted LABEL element for HTML forms.
Generate an input tag with type "month".
Creates a set of checkboxes out of options.
Creates input of type number.
Creates input of type password.
Create a <button>
tag with a surrounding <form>
that submits via POST as default.
Creates an HTML link, but access the URL using the method you specify (defaults to POST). Requires javascript to be enabled in browser.
Creates a set of radio widgets.
Restores the default values built into FormHelper.
Creates input of type search.
Generates a hidden field with a security hash based on the fields used in the form.
Returns a formatted SELECT element.
Sets the config.
Set required attribute and custom validity JS.
Sets templates to use.
Sets the value sources.
Set the widget locator the helper will use.
Creates a submit button element. This method will generate <input />
elements that can be used to submit, and reset forms by using $options. image submits can be created by supplying an image path for $caption.
Returns the templater instance.
Creates input of type text.
Creates a textarea widget.
Generate an input tag with type "time".
Add to the list of fields that are currently unlocked.
Validate value sources.
Render a named widget.
Returns a SELECT element for years
__call(string $method, array $params): string
Missing method handler - implements various simple input types. Is used to create inputs of various types. e.g. $this->Form->text();
will create <input type="text"/>
while $this->Form->range();
will create <input type="range"/>
$this->Form->search('User.query', ['value' => 'test']);
Will make an input like:
<input type="search" id="UserQuery" name="User[query]" value="test"/>
The first argument to an input type should always be the fieldname, in Model.field
format. The second argument should always be an array of attributes for the input.
string
$method Method name / input type to make.
array
$params Parameters for the method call
string
Cake\Core\Exception\CakeException
__construct(Cake\View\View $view, array<string, mixed> $config = [])
Construct the widgets and binds the default context providers
Cake\View\View
$view The View this helper is being attached to.
array<string, mixed>
$config optional Configuration settings for the helper.
__debugInfo(): array<string, mixed>
Returns an array that can be used to describe the internal state of this object.
array<string, mixed>
__get(string $name): Cake\View\Helper|null|void
Lazy loads helpers.
string
$name Name of the property being accessed.
Cake\View\Helper|null|void
_clearIds(): void
Clear the stored ID suffixes.
void
_configDelete(string $key): void
Deletes a single config key.
string
$key Key to delete.
void
Cake\Core\Exception\CakeException
_configRead(string|null $key): mixed
Reads a config key.
string|null
$key Key to read.
mixed
_configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void
Writes a config key.
array<string, mixed>|string
$key Key to write to.
mixed
$value Value to write.
string|bool
$merge optional True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
void
Cake\Core\Exception\CakeException
_confirm(string $okCode, string $cancelCode): string
Returns a string to be used as onclick handler for confirm dialogs.
string
$okCode Code to be executed after user chose 'OK'
string
$cancelCode Code to be executed after user chose 'Cancel'
string
_csrfField(): string
Return a CSRF input if the request data is present. Used to secure forms in conjunction with CsrfMiddleware.
string
_domId(string $value): string
Generate an ID suitable for use in an ID attribute.
string
$value The value to convert into an ID.
string
_extractOption(string $name, array<string, mixed> $options, mixed $default = null): mixed
Extracts a single option from an options array.
string
$name The name of the option to pull out.
array<string, mixed>
$options The array of options you want to extract.
mixed
$default optional The default option value
mixed
_formUrl(Cake\View\Form\ContextInterface $context, array<string, mixed> $options): array|string
Create the URL for a form based on the options.
Cake\View\Form\ContextInterface
$context The context object to use.
array<string, mixed>
$options An array of options from create()
array|string
_getContext(mixed $data = []): Cake\View\Form\ContextInterface
Find the matching context provider for the data.
If no type can be matched a NullContext will be returned.
mixed
$data optional The data to get a context provider for.
Cake\View\Form\ContextInterface
RuntimeException
_getInput(string $fieldName, array<string, mixed> $options): array|string
Generates an input element
string
$fieldName the field name
array<string, mixed>
$options The options for the input element
array|string
_getLabel(string $fieldName, array<string, mixed> $options): string|false
Generate label for input
string
$fieldName The name of the field to generate label for.
array<string, mixed>
$options Options list.
string|false
_groupTemplate(array<string, mixed> $options): string
Generates an group template element
array<string, mixed>
$options The options for group template
string
_id(string $name, string $val): string
Generate an ID attribute for an element.
Ensures that id's for a given set of fields are unique.
string
$name The ID attribute name.
string
$val The ID attribute value.
string
_idSuffix(string $val): string
Generate an ID suffix.
Ensures that id's for a given set of fields are unique.
string
$val The ID attribute value.
string
_initInputField(string $field, array<string, mixed>|array<string> $options = []): array<string, mixed>
Sets field defaults and adds field to form security input hash. Will also add the error class if the field contains validation errors.
secure
- boolean whether the field should be added to the security fields. Disabling the field using the disabled
option, will also omit the field from being part of the hashed key.default
- mixed - The value to use if there is no value in the form's context.disabled
- mixed - Either a boolean indicating disabled state, or the string in a numerically indexed value.id
- mixed - If true
it will be auto generated based on field name.This method will convert a numerically indexed 'disabled' into an associative array value. FormHelper's internals expect associative options.
The output of this function is a more complete set of input attributes that can be passed to a form widget to generate the actual input.
string
$field Name of the field to initialize options for.
array<string, mixed>|array<string>
$options optional Array of options to append options into.
array<string, mixed>
_inputContainerTemplate(array<string, mixed> $options): string
Generates an input container template
array<string, mixed>
$options The options for input container template
string
_inputLabel(string $fieldName, array<string, mixed>|string|null $label = null, array<string, mixed> $options = []): string
Generate a label for an input() call.
$options can contain a hash of id overrides. These overrides will be used instead of the generated values if present.
string
$fieldName The name of the field to generate label for.
array<string, mixed>|string|null
$label optional Label text or array with label attributes.
array<string, mixed>
$options optional Options for the label element.
string
_inputType(string $fieldName, array<string, mixed> $options): string
Returns the input type that was guessed for the provided fieldName, based on the internal type it is associated too, its name and the variables that can be found in the view template
string
$fieldName the name of the field to guess a type for
array<string, mixed>
$options the options passed to the input method
string
_isDisabled(array<string, mixed> $options): bool
Determine if a field is disabled.
array<string, mixed>
$options The option set.
bool
_lastAction(array|string|null $url = null): void
Correctly store the last created form action URL.
array|string|null
$url optional The URL of the last form.
void
_magicOptions(string $fieldName, array<string, mixed> $options, bool $allowOverride): array<string, mixed>
Magically set option type and corresponding options
string
$fieldName The name of the field to generate options for.
array<string, mixed>
$options Options list.
bool
$allowOverride Whether it is allowed for this method to overwrite the 'type' key in options.
array<string, mixed>
_optionsOptions(string $fieldName, array<string, mixed> $options): array<string, mixed>
Selects the variable containing the options for a select field if present, and sets the value to the 'options' key in the options array.
string
$fieldName The name of the field to find options for.
array<string, mixed>
$options Options list.
array<string, mixed>
_parseOptions(string $fieldName, array<string, mixed> $options): array<string, mixed>
Generates input options array
string
$fieldName The name of the field to parse options for.
array<string, mixed>
$options Options list.
array<string, mixed>
addClass(array<string, mixed> $options, string $class, string $key = 'class'): array<string, mixed>
Adds the given class to the element options
array<string, mixed>
$options Array options/attributes to add a class to
string
$class The class name being added.
string
$key optional the key to use for class. Defaults to 'class'
.
array<string, mixed>
addContextProvider(string $type, callable $check): void
Add a new context type.
Form context types allow FormHelper to interact with data providers that come from outside CakePHP. For example if you wanted to use an alternative ORM like Doctrine you could create and connect a new context class to allow FormHelper to read metadata from doctrine.
string
$type The type of context. This key can be used to overwrite existing providers.
callable
$check A callable that returns an object when the form context is the correct type.
void
addWidget(string $name, Cake\View\Widget\WidgetInterface|array $spec): void
Add a new widget to FormHelper.
Allows you to add or replace widget instances with custom code.
string
$name The name of the widget. e.g. 'text'.
Cake\View\Widget\WidgetInterface|array
$spec Either a string class name or an object implementing the WidgetInterface.
void
allControls(array $fields = [], array<string, mixed> $options = []): string
Generate a set of controls for $fields
. If $fields is empty the fields of current model will be used.
You can customize individual controls through $fields
.
$this->Form->allControls([ 'name' => ['label' => 'custom label'] ]);
You can exclude fields by specifying them as false
:
$this->Form->allControls(['title' => false]);
In the above example, no field would be generated for the title field.
array
$fields optional An array of customizations for the fields that will be generated. This array allows you to set custom types, labels, or other options.
array<string, mixed>
$options optional Options array. Valid keys are:
string
button(string $title, array<string, mixed> $options = []): string
Creates a <button>
tag.
type
- Value for "type" attribute of button. Defaults to "submit".escapeTitle
- HTML entity encode the title of the button. Defaults to true.escape
- HTML entity encode the attributes of button tag. Defaults to true.confirm
- Confirm message to show. Form execution will only continue if confirmed then.string
$title The button's caption. Not automatically HTML encoded
array<string, mixed>
$options optional Array of options and HTML attributes.
string
checkbox(string $fieldName, array<string, mixed> $options = []): array<string>|string
Creates a checkbox input widget.
value
- the value of the checkboxchecked
- boolean indicate that this checkbox is checked.hiddenField
- boolean|string. Set to false to disable a hidden input from being generated. Passing a string will define the hidden input value.disabled
- create a disabled input.default
- Set the default value for the checkbox. This allows you to start checkboxes as checked, without having to check the POST data. A matching POST data value, will overwrite the default value.string
$fieldName Name of a field, like this "modelname.fieldname"
array<string, mixed>
$options optional Array of HTML attributes.
array<string>|string
configShallow(array<string, mixed>|string $key, mixed|null $value = null): $this
Merge provided config with existing config. Unlike config()
which does a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
array<string, mixed>|string
$key The key to set, or a complete array of configs.
mixed|null
$value optional The value to set.
$this
context(Cake\View\Form\ContextInterface|null $context = null): Cake\View\Form\ContextInterface
Get the context instance for the current form set.
If there is no active form null will be returned.
Cake\View\Form\ContextInterface|null
$context optional Either the new context when setting, or null to get.
Cake\View\Form\ContextInterface
contextFactory(Cake\View\Form\ContextFactory|null $instance = null, array $contexts = []): Cake\View\Form\ContextFactory
Set the context factory the helper will use.
Cake\View\Form\ContextFactory|null
$instance optional The context factory instance to set.
array
$contexts optional An array of context providers.
Cake\View\Form\ContextFactory
control(string $fieldName, array<string, mixed> $options = []): string
Generates a form control element complete with label and wrapper div.
See each field type method for more information. Any options that are part of $attributes or $options for the different type methods can be included in $options
for control(). Additionally, any unknown keys that are not in the list below, or part of the selected type's options will be treated as a regular HTML attribute for the generated input.
type
- Force the type of widget you want. e.g. type => 'select'
label
- Either a string label, or an array of options for the label. See FormHelper::label().options
- For widgets that take options e.g. radio, select.error
- Control the error message that is produced. Set to false
to disable any kind of error reporting (field error and error messages).empty
- String or boolean to enable empty select box options.nestedInput
- Used with checkbox and radio inputs. Set to false to render inputs outside of label elements. Can be set to true on any input to force the input inside the label. If you enable this option for radio buttons you will also need to modify the default radioWrapper
template.templates
- The templates you want to use for this input. Any templates will be merged on top of the already loaded templates. This option can either be a filename in /config that contains the templates you want to load, or an array of templates to use.labelOptions
- Either false
to disable label around nestedWidgets e.g. radio, multicheckbox or an array of attributes for the label tag. selected
will be added to any classes e.g. class => 'myclass'
where widget is checkedstring
$fieldName This should be "modelname.fieldname"
array<string, mixed>
$options optional Each type of input takes different options.
string
controls(array $fields, array<string, mixed> $options = []): string
Generate a set of controls for $fields
wrapped in a fieldset element.
You can customize individual controls through $fields
.
$this->Form->controls([ 'name' => ['label' => 'custom label'], 'email' ]);
array
$fields An array of the fields to generate. This array allows you to set custom types, labels, or other options.
array<string, mixed>
$options optional Options array. Valid keys are:
string
create(mixed $context = null, array<string, mixed> $options = []): string
Returns an HTML form element.
type
Form method defaults to autodetecting based on the form context. If the form context's isCreate() method returns false, a PUT request will be done.method
Set the form's method attribute explicitly.url
The URL the form submits to. Can be a string or a URL array.encoding
Set the accept-charset encoding for the form. Defaults to Configure::read('App.encoding')
enctype
Set the form encoding explicitly. By default type => file
will set enctype
to multipart/form-data
.templates
The templates you want to use for this form. Any templates will be merged on top of the already loaded templates. This option can either be a filename in /config that contains the templates you want to load, or an array of templates to use.context
Additional options for the context class. For example the EntityContext accepts a 'table' option that allows you to set the specific Table class the form should be based on.idPrefix
Prefix for generated ID attributes.valueSources
The sources that values should be read from. See FormHelper::setValueSources()templateVars
Provide template variables for the formStart template.mixed
$context optional The context for which the form is being defined. Can be a ContextInterface instance, ORM entity, ORM resultset, or an array of meta data. You can use null
to make a context-less form.
array<string, mixed>
$options optional An array of html attributes and options.
string
createFormProtector(array<string, mixed> $formTokenData): Cake\Form\FormProtector
Create FormProtector instance.
array<string, mixed>
$formTokenData Token data.
Cake\Form\FormProtector
date(string $fieldName, array<string, mixed> $options = []): string
Generate an input tag with type "date".
See dateTime() options.
string
$fieldName The field name.
array<string, mixed>
$options optional Array of options or HTML attributes.
string
dateTime(string $fieldName, array<string, mixed> $options = []): string
Generate an input tag with type "datetime-local".
value
| default
The default value to be used by the input. If set to true
current datetime will be used.string
$fieldName The field name.
array<string, mixed>
$options optional Array of options or HTML attributes.
string
email(string $fieldName, array $options = []): string
Creates input of type email.
string
$fieldName array
$options optional string
end(array<string, mixed> $secureAttributes = []): string
Closes an HTML form, cleans up values set by FormHelper::create(), and writes hidden input fields where appropriate.
Resets some parts of the state, shared among multiple FormHelper::create() calls, to defaults.
array<string, mixed>
$secureAttributes optional Secure attributes which will be passed as HTML attributes into the hidden input elements generated for the Security Component.
string
error(string $field, array|string|null $text = null, array<string, mixed> $options = []): string
Returns a formatted error message for given form field, '' if no errors.
Uses the error
, errorList
and errorItem
templates. The errorList
and errorItem
templates are used to format multiple error messages per field.
escape
boolean - Whether to html escape the contents of the error.string
$field A field name, like "modelname.fieldname"
array|string|null
$text optional Error message as string or array of messages. If an array, it should be a hash of key names => messages.
array<string, mixed>
$options optional See above.
string
fieldset(string $fields = '', array<string, mixed> $options = []): string
Wrap a set of inputs in a fieldset
string
$fields optional the form inputs to wrap in a fieldset
array<string, mixed>
$options optional Options array. Valid keys are:
string
file(string $fieldName, array<string, mixed> $options = []): string
Creates file input widget.
string
$fieldName Name of a field, in the form "modelname.fieldname"
array<string, mixed>
$options optional Array of HTML attributes.
string
formatTemplate(string $name, array<string, mixed> $data): string
Formats a template string with $data
string
$name The template name.
array<string, mixed>
$data The data to insert.
string
getConfig(string|null $key = null, mixed $default = null): mixed
Returns the config.
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
string|null
$key optional The key to get or null for the whole config.
mixed
$default optional The return value when the key does not exist.
mixed
getConfigOrFail(string $key): mixed
Returns the config for this specific key.
The config value for this key must exist, it can never be null.
string
$key The key to get.
mixed
InvalidArgumentException
getFormProtector(): Cake\Form\FormProtector
Get form protector instance.
Cake\Form\FormProtector
Cake\Core\Exception\CakeException
getSourceValue(string $fieldname, array<string, mixed> $options = []): mixed
Gets a single field value from the sources available.
string
$fieldname The fieldname to fetch the value for.
array<string, mixed>
$options optional The options containing default values.
mixed
getTemplates(string|null $template = null): array|string
Gets templates to use or a specific template.
string|null
$template optional String for reading a specific template, null for all.
array|string
getValueSources(): array<string>
Gets the value sources.
Returns a list, but at least one item, of valid sources, such as: 'context'
, 'data'
and 'query'
.
array<string>
getView(): Cake\View\View
Get the view instance this helper is bound to.
Cake\View\View
getWidgetLocator(): Cake\View\Widget\WidgetLocator
Get the widget locator currently used by the helper.
Cake\View\Widget\WidgetLocator
hidden(string $fieldName, array<string, mixed> $options = []): string
Creates a hidden input field.
string
$fieldName Name of a field, in the form of "modelname.fieldname"
array<string, mixed>
$options optional Array of HTML attributes.
string
implementedEvents(): array<string, mixed>
Event listeners.
By defining one of the callback methods a helper is assumed to be interested in the related event.
Override this method if you need to add non-conventional event listeners. Or if you want helpers to listen to non-standard events.
array<string, mixed>
initialize(array<string, mixed> $config): void
Constructor hook method.
Implement this method to avoid having to overwrite the constructor and call parent.
array<string, mixed>
$config The configuration settings provided to this helper.
void
isFieldError(string $field): bool
Returns true if there is an error for the given field, otherwise false
string
$field This should be "modelname.fieldname"
bool
label(string $fieldName, string|null $text = null, array<string, mixed> $options = []): string
Returns a formatted LABEL element for HTML forms.
Will automatically generate a for
attribute if one is not provided.
for
- Set the for attribute, if its not defined the for attribute will be generated from the $fieldName parameter using FormHelper::_domId().escape
- Set to false
to turn off escaping of label text. Defaults to true
.Examples:
The text and for attribute are generated off of the fieldname
echo $this->Form->label('published'); <label for="PostPublished">Published</label>
Custom text:
echo $this->Form->label('published', 'Publish'); <label for="published">Publish</label>
Custom attributes:
echo $this->Form->label('published', 'Publish', [ 'for' => 'post-publish' ]); <label for="post-publish">Publish</label>
Nesting an input tag:
echo $this->Form->label('published', 'Publish', [ 'for' => 'published', 'input' => $this->text('published'), ]); <label for="post-publish">Publish <input type="text" name="published"></label>
If you want to nest inputs in the labels, you will need to modify the default templates.
string
$fieldName This should be "modelname.fieldname"
string|null
$text optional Text that will appear in the label field. If $text is left undefined the text will be inflected from the fieldName.
array<string, mixed>
$options optional An array of HTML attributes.
string
month(string $fieldName, array<string, mixed> $options = []): string
Generate an input tag with type "month".
See dateTime() options.
string
$fieldName The field name.
array<string, mixed>
$options optional Array of options or HTML attributes.
string
multiCheckbox(string $fieldName, iterable $options, array<string, mixed> $attributes = []): string
Creates a set of checkboxes out of options.
escape
- If true contents of options will be HTML entity encoded. Defaults to true.val
The selected value of the input.class
- When using multiple = checkbox the class name to apply to the divs. Defaults to 'checkbox'.disabled
- Control the disabled attribute. When creating checkboxes, true
will disable all checkboxes. You can also set disabled to a list of values you want to disable when creating checkboxes.hiddenField
- Set to false to remove the hidden field that ensures a value is always submitted.label
- Either false
to disable label around the widget or an array of attributes for the label tag. selected
will be added to any classes e.g. 'class' => 'myclass'
where widget is checkedCan be used in place of a select box with the multiple attribute.
string
$fieldName Name attribute of the SELECT
iterable
$options Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the checkboxes element.
array<string, mixed>
$attributes optional The HTML attributes of the select element.
string
number(string $fieldName, array $options = []): string
Creates input of type number.
string
$fieldName array
$options optional string
password(string $fieldName, array $options = []): string
Creates input of type password.
string
$fieldName array
$options optional string
postButton(string $title, array|string $url, array<string, mixed> $options = []): string
Create a <button>
tag with a surrounding <form>
that submits via POST as default.
This method creates a <form>
element. So do not use this method in an already opened form. Instead use FormHelper::submit() or FormHelper::button() to create buttons inside opened forms.
data
- Array with key/value to pass in input hiddenmethod
- Request method to use. Set to 'delete' or others to simulate HTTP/1.1 DELETE (or others) request. Defaults to 'post'.form
- Array with any option that FormHelper::create() can takeconfirm
- Confirm message to show. Form execution will only continue if confirmed then.string
$title The button's caption. Not automatically HTML encoded
array|string
$url URL as string or array
array<string, mixed>
$options optional Array of options and HTML attributes.
string
postLink(string $title, array|string|null $url = null, array<string, mixed> $options = []): string
Creates an HTML link, but access the URL using the method you specify (defaults to POST). Requires javascript to be enabled in browser.
This method creates a <form>
element. If you want to use this method inside of an existing form, you must use the block
option so that the new form is being set to a view block that can be rendered outside of the main form.
If all you are looking for is a button to submit your form, then you should use FormHelper::button()
or FormHelper::submit()
instead.
data
- Array with key/value to pass in input hiddenmethod
- Request method to use. Set to 'delete' to simulate HTTP/1.1 DELETE request. Defaults to 'post'.confirm
- Confirm message to show. Form execution will only continue if confirmed then.block
- Set to true to append form to view block "postLink" or provide custom block name.onclick
will be replaced.string
$title The content to be wrapped by tags.
array|string|null
$url optional Cake-relative URL or array of URL parameters, or external URL (starts with http://)
array<string, mixed>
$options optional Array of HTML attributes.
string
radio(string $fieldName, iterable $options = [], array<string, mixed> $attributes = []): string
Creates a set of radio widgets.
value
- Indicates the value when this radio button is checked.label
- Either false
to disable label around the widget or an array of attributes for the label tag. selected
will be added to any classes e.g. 'class' => 'myclass'
where widget is checkedhiddenField
- boolean|string. Set to false to not include a hidden input with a value of ''. Can also be a string to set the value of the hidden input. This is useful for creating radio sets that are non-continuous.disabled
- Set to true
or disabled
to disable all the radio buttons. Use an array of values to disable specific radio buttons.empty
- Set to true
to create an input with the value '' as the first option. When true
the radio label will be 'empty'. Set this option to a string to control the label value.string
$fieldName Name of a field, like this "modelname.fieldname"
iterable
$options optional Radio button options array.
array<string, mixed>
$attributes optional Array of attributes.
string
resetTemplates(): void
Restores the default values built into FormHelper.
This method will not reset any templates set in custom widgets.
void
search(string $fieldName, array $options = []): string
Creates input of type search.
string
$fieldName array
$options optional string
secure(array $fields = [], array<string, mixed> $secureAttributes = []): string
Generates a hidden field with a security hash based on the fields used in the form.
If $secureAttributes is set, these HTML attributes will be merged into the hidden input tags generated for the Security Component. This is especially useful to set HTML5 attributes like 'form'.
array
$fields optional If set specifies the list of fields to be added to FormProtector for generating the hash.
array<string, mixed>
$secureAttributes optional will be passed as HTML attributes into the hidden input elements generated for the Security Component.
string
select(string $fieldName, iterable $options = [], array<string, mixed> $attributes = []): string
Returns a formatted SELECT element.
multiple
- show a multiple select box. If set to 'checkbox' multiple checkboxes will be created instead.empty
- If true, the empty select option is shown. If a string, that string is displayed as the empty element.escape
- If true contents of options will be HTML entity encoded. Defaults to true.val
The selected value of the input.disabled
- Control the disabled attribute. When creating a select box, set to true to disable the select box. Set to an array to disable specific option elements.A simple array will create normal options:
$options = [1 => 'one', 2 => 'two']; $this->Form->select('Model.field', $options));
While a nested options array will create optgroups with options inside them.
$options = [ 1 => 'bill', 'fred' => [ 2 => 'fred', 3 => 'fred jr.' ] ]; $this->Form->select('Model.field', $options);
If you have multiple options that need to have the same value attribute, you can use an array of arrays to express this:
$options = [ ['text' => 'United states', 'value' => 'USA'], ['text' => 'USA', 'value' => 'USA'], ];
string
$fieldName Name attribute of the SELECT
iterable
$options optional Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the SELECT element
array<string, mixed>
$attributes optional The HTML attributes of the select element.
string
setConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this
Sets the config.
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
array<string, mixed>|string
$key The key to set, or a complete array of configs.
mixed|null
$value optional The value to set.
bool
$merge optional Whether to recursively merge or overwrite existing config, defaults to true.
$this
Cake\Core\Exception\CakeException
setRequiredAndCustomValidity(string $fieldName, array<string, mixed> $options): array<string, mixed>
Set required attribute and custom validity JS.
string
$fieldName The name of the field to generate options for.
array<string, mixed>
$options Options list.
array<string, mixed>
setTemplates(array<string> $templates): $this
Sets templates to use.
array<string>
$templates Templates to be added.
$this
setValueSources(array<string>|string $sources): $this
Sets the value sources.
You need to supply one or more valid sources, as a list of strings. Order sets priority.
array<string>|string
$sources A string or a list of strings identifying a source.
$this
InvalidArgumentException
setWidgetLocator(Cake\View\Widget\WidgetLocator $instance): $this
Set the widget locator the helper will use.
Cake\View\Widget\WidgetLocator
$instance The locator instance to set.
$this
submit(string|null $caption = null, array<string, mixed> $options = []): string
Creates a submit button element. This method will generate <input />
elements that can be used to submit, and reset forms by using $options. image submits can be created by supplying an image path for $caption.
type
- Set to 'reset' for reset inputs. Defaults to 'submit'templateVars
- Additional template variables for the input element and its container.string|null
$caption optional The label appearing on the button OR if string contains :// or the extension .jpg, .jpe, .jpeg, .gif, .png use an image if the extension exists, AND the first character is /, image is relative to webroot, OR if the first character is not /, image is relative to webroot/img.
array<string, mixed>
$options optional Array of options. See above.
string
templater(): Cake\View\StringTemplate
Returns the templater instance.
Cake\View\StringTemplate
text(string $fieldName, array $options = []): string
Creates input of type text.
string
$fieldName array
$options optional string
textarea(string $fieldName, array<string, mixed> $options = []): string
Creates a textarea widget.
escape
- Whether the contents of the textarea should be escaped. Defaults to true.string
$fieldName Name of a field, in the form "modelname.fieldname"
array<string, mixed>
$options optional Array of HTML attributes, and special options above.
string
time(string $fieldName, array<string, mixed> $options = []): string
Generate an input tag with type "time".
See dateTime() options.
string
$fieldName The field name.
array<string, mixed>
$options optional Array of options or HTML attributes.
string
unlockField(string $name): $this
Add to the list of fields that are currently unlocked.
Unlocked fields are not included in the form protection field hash.
string
$name The dot separated name for the field.
$this
validateValueSources(array<string> $sources): void
Validate value sources.
array<string>
$sources A list of strings identifying a source.
void
InvalidArgumentException
widget(string $name, array $data = []): string
Render a named widget.
This is a lower level method. For built-in widgets, you should be using methods like text
, hidden
, and radio
. If you are using additional widgets you should use this method render the widget without the label or wrapping div.
string
$name The name of the widget. e.g. 'text'.
array
$data optional The data to render.
string
year(string $fieldName, array<string, mixed> $options = []): string
Returns a SELECT element for years
empty
- If true, the empty select option is shown. If a string, that string is displayed as the empty element.order
- Ordering of year values in select options. Possible values 'asc', 'desc'. Default 'desc'value
The selected value of the input.max
The max year to appear in the select element.min
The min year to appear in the select element.string
$fieldName The field name.
array<string, mixed>
$options optional Options & attributes for the select elements.
string
Cake\View\Helper\HtmlHelper
Cake\View\Helper\UrlHelper
The View instance this helper is attached to
Cake\View\View
Runtime config
array<string, mixed>
Whether the config property has already been configured with defaults
bool
Context for the current form.
Cake\View\Form\ContextInterface|null
Context factory.
Cake\View\Form\ContextFactory|null
Default config for the helper.
array<string, mixed>
Default widgets
array<string, array<string>>
Grouped input types.
array<string>
A helper lookup table used to lazy load helper objects.
array<string, array>
Prefix for id attribute.
string|null
A list of id suffixes used in the current rendering.
array<string>
The action attribute value of the last created form. Used to make form/request specific hashes for form tampering protection.
string
Locator for input widgets.
Cake\View\Widget\WidgetLocator
StringTemplate instance.
Cake\View\StringTemplate|null
The default sources.
array<string>
Form protector
Cake\Form\FormProtector|null
Other helpers used by FormHelper
array
Defines the type of form being created. Set by FormHelper::create().
string|null
The supported sources that can be used to populate input values.
context
- Corresponds to ContextInterface
instances. data
- Corresponds to request data (POST/PUT). query
- Corresponds to request's query string.
array<string>
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.4/class-Cake.View.Helper.FormHelper.html