CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 6.0 API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 6.0
      • 6.0
      • 5.4
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • AttributeResolver
    • Cache
    • Collection
    • Command
    • Console
    • Container
    • Controller
      • Attribute
      • Component
      • Exception
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Lock
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

Class FormProtectionComponent

Protects against form tampering. It ensures that:

  • Form's action (URL) is not modified.
  • Unknown / extra fields are not added to the form.
  • Existing fields have not been removed from the form.
  • Values of hidden inputs have not been changed.
Namespace: Cake\Controller\Component

Constants

  • string
    DEFAULT_EXCEPTION_MESSAGE ¶
    'Form tampering protection token validation failed.'

    Default message used for exceptions thrown.

Property Summary

  • $_config public @property
    array{validate: bool, unlockedFields: array, unlockedActions: array, validationFailureCallback: ?\Closure}
  • $componentInstances protected
    array<string, \Cake\Controller\Component>

    Loaded component instances.

  • $components protected
    array<int|string, string|array<string, mixed>>

    Other Components this component uses.

  • $config protected
    array<string, mixed>

    Runtime config

  • $configInitialized protected
    bool

    Whether the config property has already been configured with defaults

  • $defaultConfig protected
    array<string, mixed>

    Default config

    • validate - Whether to validate request body / data. Set to false to disable for data coming from 3rd party services, etc.
    • unlockedFields - Form fields to exclude from validation. Fields can be unlocked either in the Component, or with FormHelper::unlockField(). Fields that have been unlocked are not required to be part of the POST and hidden unlocked fields do not have their values checked.
    • unlockedActions - Actions to exclude from POST validation checks.
    • validationFailureCallback - Callback to call in case of validation failure. Must be a valid Closure. Unset by default in which case exception is thrown on validation failure.

Method Summary

  • __construct() public

    Constructor

  • __debugInfo() public

    Returns an array that can be used to describe the internal state of this object.

  • __get() public

    Magic method for lazy loading $components.

  • configDelete() protected

    Deletes a single config key.

  • configRead() protected

    Reads a config key.

  • configShallow() public

    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']);
  • configWrite() protected

    Writes a config key.

  • deleteConfig() public

    Deletes a config key.

  • executeCallback() protected

    Execute callback.

  • getConfig() public

    Returns the config.

    Usage

    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');
  • getConfigOrFail() public

    Returns the config for this specific key.

    The config value for this key must exist, it can never be null.

  • getController() public

    Get the controller this component is bound to.

  • getSessionId() protected

    Get Session id for FormProtector Must be the same as in FormHelper

  • implementedEvents() public

    Events supported by this component.

  • initialize() public

    Constructor hook method.

    Implement this method to avoid having to overwrite the constructor and call parent.

  • log() public

    Convenience method to write a message to Log. See Log::write() for more information on writing to logs.

  • setConfig() public

    Sets the config.

    Usage

    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']);
  • startup() public

    Component startup.

    Token check happens here.

  • unlockActions() public

    Unlock actions from validation.

  • unlockFields() public

    Unlock fields from validation.

    Dot notation can be used to unlock nested fields. For example, user.name will unlock the name field in the user array.

  • validationFailure() protected

    Throws a 400 - Bad request exception or calls custom callback.

    If validationFailureCallback config is specified, it will use this callback by executing the method passing the argument as exception.

Method Detail

__construct() ¶ public

__construct(Cake\Controller\ComponentRegistry $registry, array<string, mixed> $config = [])

Constructor

Parameters
Cake\Controller\ComponentRegistry $registry

A component registry this component can use to lazy load its components.

array<string, mixed> $config optional

Array of configuration settings.

__debugInfo() ¶ public

__debugInfo(): array<string, mixed>

Returns an array that can be used to describe the internal state of this object.

Returns
array<string, mixed>

__get() ¶ public

__get(string $name): Cake\Controller\Component|null

Magic method for lazy loading $components.

Parameters
string $name

Name of component to get.

Returns
Cake\Controller\Component|null

A Component object or null.

configDelete() ¶ protected

configDelete(string $key): void

Deletes a single config key.

Parameters
string $key

Key to delete.

Returns
void
Throws
Cake\Core\Exception\CakeException
if attempting to clobber existing config

configRead() ¶ protected

configRead(string|null $key): $key is null ? array : mixed

Reads a config key.

Parameters
string|null $key

Key to read.

Returns
$key is null ? array : mixed

configShallow() ¶ public

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']);
Parameters
array<string, mixed>|string $key

The key to set, or a complete array of configs.

mixed|null $value optional

The value to set.

Returns
$this

configWrite() ¶ protected

configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void

Writes a config key.

Parameters
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.

Returns
void
Throws
Cake\Core\Exception\CakeException
if attempting to clobber existing config

deleteConfig() ¶ public

deleteConfig(string $key): $this

Deletes a config key.

Parameters
string $key

Key to delete. It can be a dot separated string to delete nested keys.

Returns
$this

executeCallback() ¶ protected

executeCallback(Closure $callback, Cake\Controller\Exception\FormProtectionException $exception): Cake\Http\Response|null

Execute callback.

Parameters
Closure $callback

Callback

Cake\Controller\Exception\FormProtectionException $exception

Exception instance.

Returns
Cake\Http\Response|null

getConfig() ¶ public

getConfig(string|null $key = null, mixed $default = null): $key is null ? array : mixed

Returns the config.

Usage

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');
Parameters
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.

Returns
$key is null ? array : mixed

Configuration data at the named key or null if the key does not exist.

getConfigOrFail() ¶ public

getConfigOrFail(string $key): mixed

Returns the config for this specific key.

The config value for this key must exist, it can never be null.

Parameters
string $key

The key to get.

Returns
mixed

Configuration data at the named key

Throws
InvalidArgumentException

getController() ¶ public

getController(): Cake\Controller\Controller

Get the controller this component is bound to.

Returns
Cake\Controller\Controller

The bound controller.

getSessionId() ¶ protected

getSessionId(): string

Get Session id for FormProtector Must be the same as in FormHelper

Returns
string

implementedEvents() ¶ public

implementedEvents(): array<string, mixed>

Events supported by this component.

Returns
array<string, mixed>

initialize() ¶ public

initialize(array<string, mixed> $config): void

Constructor hook method.

Implement this method to avoid having to overwrite the constructor and call parent.

Parameters
array<string, mixed> $config

The configuration settings provided to this component.

Returns
void

log() ¶ public

log(Stringable|string $message, string|int $level = LogLevel::ERROR, array|string $context = []): bool

Convenience method to write a message to Log. See Log::write() for more information on writing to logs.

Parameters
Stringable|string $message

Log message.

string|int $level optional

Error level.

array|string $context optional

Additional log data relevant to this message.

Returns
bool

Success of log write.

setConfig() ¶ public

setConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this

Sets the config.

Usage

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']);
Parameters
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.

Returns
$this
Throws
Cake\Core\Exception\CakeException
When trying to set a key that is invalid.

startup() ¶ public

startup(Cake\Event\EventInterface<Cake\Controller\Controller> $event): Cake\Http\Response|null

Component startup.

Token check happens here.

Parameters
Cake\Event\EventInterface<Cake\Controller\Controller> $event

An Event instance

Returns
Cake\Http\Response|null

unlockActions() ¶ public

unlockActions(array<string>|string $actions, bool $merge = true): $this

Unlock actions from validation.

Parameters
array<string>|string $actions

Action or list of actions to unlock.

bool $merge optional

Whether to merge with existing unlocked actions or replace them.

Returns
$this

unlockFields() ¶ public

unlockFields(array<string>|string $fields, bool $merge = true): $this

Unlock fields from validation.

Dot notation can be used to unlock nested fields. For example, user.name will unlock the name field in the user array.

Parameters
array<string>|string $fields

Field or list of fields to unlock.

bool $merge optional

Whether to merge with existing unlocked fields or replace them.

Returns
$this

validationFailure() ¶ protected

validationFailure(Cake\Form\FormProtector $formProtector): Cake\Http\Response|null

Throws a 400 - Bad request exception or calls custom callback.

If validationFailureCallback config is specified, it will use this callback by executing the method passing the argument as exception.

Parameters
Cake\Form\FormProtector $formProtector

Form Protector instance.

Returns
Cake\Http\Response|null

If specified, validationFailureCallback's response, or no return otherwise.

Throws
Cake\Controller\Exception\FormProtectionException

Property Detail

$_config ¶ public @property

Type
array{validate: bool, unlockedFields: array, unlockedActions: array, validationFailureCallback: ?\Closure}

$componentInstances ¶ protected

Loaded component instances.

Type
array<string, \Cake\Controller\Component>

$components ¶ protected

Other Components this component uses.

Type
array<int|string, string|array<string, mixed>>

$config ¶ protected

Runtime config

Type
array<string, mixed>

$configInitialized ¶ protected

Whether the config property has already been configured with defaults

Type
bool

$defaultConfig ¶ protected

Default config

  • validate - Whether to validate request body / data. Set to false to disable for data coming from 3rd party services, etc.
  • unlockedFields - Form fields to exclude from validation. Fields can be unlocked either in the Component, or with FormHelper::unlockField(). Fields that have been unlocked are not required to be part of the POST and hidden unlocked fields do not have their values checked.
  • unlockedActions - Actions to exclude from POST validation checks.
  • validationFailureCallback - Callback to call in case of validation failure. Must be a valid Closure. Unset by default in which case exception is thrown on validation failure.
Type
array<string, mixed>
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs