ArrayContext
in package
implements
ContextInterface
Provides a basic array based context provider for FormHelper.
This adapter is useful in testing or when you have forms backed by simple array data structures.
Important keys:
-
data
Holds the current values supplied for the fields. -
defaults
The default values for fields. These values will be used when there is no data set. Data should be nested following the dot separated paths you access your fields with. -
required
A nested array of fields, relationships and boolean flags to indicate a field is required. The value can also be a string to be used as the required error message -
schema
An array of data that emulate the column structures that Cake\Database\Schema\Schema uses. This array allows you to control the inferred type for fields and allows auto generation of attributes like maxlength, step and other HTML attributes. If you want primary key/id detection to work. Make sure you have provided a_constraints
array that containsprimary
. See below for an example. -
errors
An array of validation errors. Errors should be nested following the dot separated paths you access your fields with.
Example
$article = [
'data' => [
'id' => '1',
'title' => 'First post!',
],
'schema' => [
'id' => ['type' => 'integer'],
'title' => ['type' => 'string', 'length' => 255],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
],
'defaults' => [
'title' => 'Default title',
],
'required' => [
'id' => true, // will use default required message
'title' => 'Please enter a title',
'body' => false,
],
];
Table of Contents
Interfaces
- ContextInterface
- Interface for FormHelper context implementations.
Properties
- $_context : array<string, mixed>
- Context data for this object.
Methods
- __construct() : mixed
- Constructor.
- attributes() : array<string|int, mixed>
- Get an associative array of other attributes for a field name.
- error() : array<string|int, mixed>
- Get the errors for a given field
- fieldNames() : array<string|int, string>
- Get the field names of the top level object in this context.
- getMaxLength() : int|null
- Get field length from validation
- getPrimaryKey() : array<string|int, string>
- Get the fields used in the context as a primary key.
- getRequiredMessage() : string|null
- Gets the default "required" error message for a field
- hasError() : bool
- Check whether a field has an error attached to it
- isCreate() : bool
- Returns whether this form is for a create operation.
- isPrimaryKey() : bool
- Returns true if the passed field name is part of the primary key for this context
- isRequired() : bool|null
- Check if a given field is 'required'.
- primaryKey() : array<string|int, string>
- Get the fields used in the context as a primary key.
- type() : string|null
- Get the abstract field type for a given field name.
- val() : mixed
- Get the current value for a given field.
- stripNesting() : string
- Strips out any numeric nesting
Properties
$_context
Context data for this object.
protected
array<string, mixed>
$_context
Methods
__construct()
Constructor.
public
__construct(array<string|int, mixed> $context) : mixed
Parameters
- $context : array<string|int, mixed>
-
Context info.
attributes()
Get an associative array of other attributes for a field name.
public
attributes(string $field) : array<string|int, mixed>
Parameters
- $field : string
-
A dot separated path to get additional data on.
Return values
array<string|int, mixed> —An array of data describing the additional attributes on a field.
error()
Get the errors for a given field
public
error(string $field) : array<string|int, mixed>
Parameters
- $field : string
-
A dot separated path to check errors on.
Return values
array<string|int, mixed> —An array of errors, an empty array will be returned when the context has no errors.
fieldNames()
Get the field names of the top level object in this context.
public
fieldNames() : array<string|int, string>
Tags
Return values
array<string|int, string> —A list of the field names in the context.
getMaxLength()
Get field length from validation
public
getMaxLength(string $field) : int|null
In this context class, this is simply defined by the 'length' array.
Parameters
- $field : string
-
A dot separated path to check required-ness for.
Return values
int|nullgetPrimaryKey()
Get the fields used in the context as a primary key.
public
getPrimaryKey() : array<string|int, string>
Return values
array<string|int, string>getRequiredMessage()
Gets the default "required" error message for a field
public
getRequiredMessage(string $field) : string|null
Parameters
- $field : string
-
A dot separated path to the field name
Tags
Return values
string|nullhasError()
Check whether a field has an error attached to it
public
hasError(string $field) : bool
Parameters
- $field : string
-
A dot separated path to check errors on.
Return values
bool —Returns true if the errors for the field are not empty.
isCreate()
Returns whether this form is for a create operation.
public
isCreate() : bool
For this method to return true, both the primary key constraint must be defined in the 'schema' data, and the 'defaults' data must contain a value for all fields in the key.
Return values
boolisPrimaryKey()
Returns true if the passed field name is part of the primary key for this context
public
isPrimaryKey(string $field) : bool
Parameters
- $field : string
-
A dot separated path to the field a value is needed for.
Tags
Return values
boolisRequired()
Check if a given field is 'required'.
public
isRequired(string $field) : bool|null
In this context class, this is simply defined by the 'required' array.
Parameters
- $field : string
-
A dot separated path to check required-ness for.
Return values
bool|nullprimaryKey()
Get the fields used in the context as a primary key.
public
primaryKey() : array<string|int, string>
Return values
array<string|int, string>type()
Get the abstract field type for a given field name.
public
type(string $field) : string|null
Parameters
- $field : string
-
A dot separated path to get a schema type for.
Tags
Return values
string|null —An abstract data type or null.
val()
Get the current value for a given field.
public
val(string $field[, array<string, mixed> $options = [] ]) : mixed
This method will coalesce the current data and the 'defaults' array.
Parameters
- $field : string
-
A dot separated path to the field a value is needed for.
- $options : array<string, mixed> = []
-
Options:
-
default
: Default value to return if no value found in data or context record. -
schemaDefault
: Boolean indicating whether default value from context's schema should be used if it's not explicitly provided.
-
stripNesting()
Strips out any numeric nesting
protected
stripNesting(string $field) : string
For example users.0.age will output as users.age
Parameters
- $field : string
-
A dot separated path
Return values
string —A string with stripped numeric nesting