\Cake\View\FormEntityContext

Provides a form context around a single entity and its relations.

It also can be used as context around an array or iterator of entities.

This class lets FormHelper interface with entities or collections of entities.

Important Keys:

  • entity The entity this context is operating on.
  • table Either the ORM\Table instance to fetch schema/validators from, an array of table instances in the case of a form spanning multiple entities, or the name(s) of the table. If this is null the table name(s) will be determined using naming conventions.
  • validator Either the Validation\Validator to use, or the name of the validation method to call on the table object. For example 'default'. Defaults to 'default'. Can be an array of table alias=>validators when dealing with associated forms.

Summary

Methods
Properties
Constants
tableLocator()
setTableLocator()
getTableLocator()
__construct()
primaryKey()
isPrimaryKey()
isCreate()
val()
entity()
isRequired()
getRequiredMessage()
getMaxLength()
fieldNames()
type()
attributes()
hasError()
error()
No public properties found
No constants found
_prepare()
_schemaDefault()
_extractMultiple()
_getProp()
_getValidator()
_getTable()
$_tableLocator
$_request
$_context
$_rootName
$_isCollection
$_tables
$_validator
N/A
No private methods found
No private properties found
N/A

Properties

$_context

$_context : array

Context data for this object.

Type

array

$_rootName

$_rootName : string

The name of the top level entity/table object.

Type

string

$_isCollection

$_isCollection : boolean

Boolean to track whether or not the entity is a collection.

Type

boolean

$_tables

$_tables : array

A dictionary of tables

Type

array

$_validator

$_validator : array<mixed,\Cake\Validation\Validator>

Dictionary of validators.

Type

array<mixed,\Cake\Validation\Validator>

Methods

tableLocator()

tableLocator(\Cake\ORM\Locator\LocatorInterface|null  $tableLocator = null) : \Cake\ORM\Locator\LocatorInterface

Sets the table locator.

If no parameters are passed, it will return the currently used locator.

Parameters

\Cake\ORM\Locator\LocatorInterface|null $tableLocator

LocatorInterface instance.

Returns

\Cake\ORM\Locator\LocatorInterface

setTableLocator()

setTableLocator(\Cake\ORM\Locator\LocatorInterface  $tableLocator) : $this

Sets the table locator.

Parameters

\Cake\ORM\Locator\LocatorInterface $tableLocator

LocatorInterface instance.

Returns

$this

__construct()

__construct(\Cake\Http\ServerRequest  $request, array  $context) 

Constructor.

Parameters

\Cake\Http\ServerRequest $request

The request object.

array $context

Context info.

primaryKey()

primaryKey() : array

Get the primary key data for the context.

Gets the primary key columns from the root entity's schema.

Returns

array

isPrimaryKey()

isPrimaryKey(string  $field) : boolean

Returns true if the passed field name is part of the primary key for this context

Parameters

string $field

A dot separated path to the field a value is needed for.

Returns

boolean

isCreate()

isCreate() : boolean

Check whether or not this form is a create or update.

If the context is for a single entity, the entity's isNew() method will be used. If isNew() returns null, a create operation will be assumed.

If the context is for a collection or array the first object in the collection will be used.

Returns

boolean

val()

val(string  $field, array  $options = array()) : mixed

Get the value for a given path.

Traverses the entity data and finds the value for $path.

Parameters

string $field

The dot separated path to the value.

array $options

Options:

  • default: Default value to return if no value found in request data or entity.
  • schemaDefault: Boolean indicating whether default value from table schema should be used if it's not explicitly provided.

Returns

mixed —

The value of the field or null on a miss.

entity()

entity(array|null  $path = null) : \Cake\Datasource\EntityInterface|\Traversable|array|boolean

Fetch the leaf entity for the given path.

This method will traverse the given path and find the leaf entity. If the path does not contain a leaf entity false will be returned.

Parameters

array|null $path

Each one of the parts in a path for a field name or null to get the entity passed in constructor context.

Throws

\RuntimeException

When properties cannot be read.

Returns

\Cake\Datasource\EntityInterface|\Traversable|array|boolean

isRequired()

isRequired(string  $field) : boolean

Check if a field should be marked as required.

In this context class, this is simply defined by the 'required' array.

Parameters

string $field

The dot separated path to the field you want to check.

Returns

boolean

getRequiredMessage()

getRequiredMessage(  $field) 

{@inheritDoc}

Parameters

$field

getMaxLength()

getMaxLength(string  $field) : integer|null

Get field length from validation

Parameters

string $field

The dot separated path to the field you want to check.

Returns

integer|null

fieldNames()

fieldNames() : array

Get the field names from the top level entity.

If the context is for an array of entities, the 0th index will be used.

Returns

array —

Array of fieldnames in the table/entity.

type()

type(string  $field) : null|string

Get the abstract field type for a given field name.

Parameters

string $field

A dot separated path to get a schema type for.

Returns

null|string —

An abstract data type or null.

attributes()

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 —

An array of data describing the additional attributes on a field.

hasError()

hasError(string  $field) : boolean

Check whether or not a field has an error attached to it

Parameters

string $field

A dot separated path to check errors on.

Returns

boolean —

Returns true if the errors for the field are not empty.

error()

error(string  $field) : array

Get the errors for a given field

Parameters

string $field

A dot separated path to check errors on.

Returns

array —

An array of errors.

_prepare()

_prepare() : void

Prepare some additional data from the context.

If the table option was provided to the constructor and it was a string, TableLocator will be used to get the correct table instance.

If an object is provided as the table option, it will be used as is.

If no table option is provided, the table name will be derived based on naming conventions. This inference will work with a number of common objects like arrays, Collection objects and ResultSets.

Throws

\RuntimeException

When a table object cannot be located/inferred.

_schemaDefault()

_schemaDefault(array  $parts) : mixed

Get default value from table schema for given entity field.

Parameters

array $parts

Each one of the parts in a path for a field name

Returns

mixed

_extractMultiple()

_extractMultiple(array|\Traversable  $values, array  $path) : array|null

Helper method used to extract all the primary key values out of an array, The primary key column is guessed out of the provided $path array

Parameters

array|\Traversable $values

The list from which to extract primary keys from

array $path

Each one of the parts in a path for a field name

Returns

array|null

_getProp()

_getProp(mixed  $target, string  $field) : mixed

Read property values or traverse arrays/iterators.

Parameters

mixed $target

The entity/array/collection to fetch $field from.

string $field

The next field to fetch.

Returns

mixed

_getValidator()

_getValidator(array  $parts) : \Cake\Validation\Validator

Get the validator associated to an entity based on naming conventions.

Parameters

array $parts

Each one of the parts in a path for a field name

Returns

\Cake\Validation\Validator

_getTable()

_getTable(array  $parts, boolean  $fallback = true) : \Cake\ORM\Table|boolean

Get the table instance from a property path

Parameters

array $parts

Each one of the parts in a path for a field name

boolean $fallback

Whether or not to fallback to the last found table when a non-existent field/property is being encountered.

Returns

\Cake\ORM\Table|boolean —

Table instance or false