Class ArrayContext
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 messageschema
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,
],
];
Constants
-
array<string>
VALID_ATTRIBUTES ¶['length', 'precision', 'comment', 'null', 'default']
Property Summary
-
$_context protected
array<string, mixed>
Context data for this object.
Method Summary
-
__construct() public
Constructor.
-
attributes() public
Get an associative array of other attributes for a field name.
-
error() public
Get the errors for a given field
-
fieldNames() public
Get the field names of the top level object in this context.
-
getMaxLength() public
Get field length from validation
-
getPrimaryKey() public
Get the fields used in the context as a primary key.
-
getRequiredMessage() public
Gets the default "required" error message for a field
-
hasError() public
Check whether a field has an error attached to it
-
isCreate() public
Returns whether this form is for a create operation.
-
isPrimaryKey() public
Returns true if the passed field name is part of the primary key for this context
-
isRequired() public
Check if a given field is 'required'.
-
primaryKey() public deprecated
Get the fields used in the context as a primary key.
-
stripNesting() protected
Strips out any numeric nesting
-
type() public
Get the abstract field type for a given field name.
-
val() public
Get the current value for a given field.
Method Detail
__construct() ¶ public
__construct(array $context)
Constructor.
Parameters
-
array
$context Context info.
attributes() ¶ public
attributes(string $field): array
Get an associative array of other attributes for a field name.
Parameters
-
string
$field A dot separated path to get additional data on.
Returns
array
error() ¶ public
error(string $field): array
Get the errors for a given field
Parameters
-
string
$field A dot separated path to check errors on.
Returns
array
fieldNames() ¶ public
fieldNames(): array<string>
Get the field names of the top level object in this context.
Returns
array<string>
getMaxLength() ¶ public
getMaxLength(string $field): int|null
Get field length from validation
In this context class, this is simply defined by the 'length' array.
Parameters
-
string
$field A dot separated path to check required-ness for.
Returns
int|null
getPrimaryKey() ¶ public
getPrimaryKey(): array<string>
Get the fields used in the context as a primary key.
Returns
array<string>
getRequiredMessage() ¶ public
getRequiredMessage(string $field): string|null
Gets the default "required" error message for a field
Parameters
-
string
$field
Returns
string|null
hasError() ¶ public
hasError(string $field): bool
Check whether a field has an error attached to it
Parameters
-
string
$field A dot separated path to check errors on.
Returns
bool
isCreate() ¶ public
isCreate(): bool
Returns whether this form is for a create operation.
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.
Returns
bool
isPrimaryKey() ¶ public
isPrimaryKey(string $field): bool
Returns true if the passed field name is part of the primary key for this context
Parameters
-
string
$field
Returns
bool
isRequired() ¶ public
isRequired(string $field): bool|null
Check if a given field is 'required'.
In this context class, this is simply defined by the 'required' array.
Parameters
-
string
$field A dot separated path to check required-ness for.
Returns
bool|null
primaryKey() ¶ public
primaryKey(): array<string>
Get the fields used in the context as a primary key.
Returns
array<string>
stripNesting() ¶ protected
stripNesting(string $field): string
Strips out any numeric nesting
For example users.0.age will output as users.age
Parameters
-
string
$field A dot separated path
Returns
string
type() ¶ public
type(string $field): string|null
Get the abstract field type for a given field name.
Parameters
-
string
$field A dot separated path to get a schema type for.
Returns
string|null
See Also
val() ¶ public
val(string $field, array<string, mixed> $options = []): mixed
Get the current value for a given field.
This method will coalesce the current data and the 'defaults' array.
Parameters
-
string
$field A dot separated path to the field a value is needed for.
-
array<string, mixed>
$options optional Options:
Returns
mixed