Skip to main content

Field Types

DataView provides built-in field types that handle data type mapping, input sanitization, and HTML input rendering.

Built-in Types

TypeHTML InputSanitizerDescription
stringtextsanitize_text_fieldSingle-line text
texttextareasanitize_textarea_fieldMulti-line text
emailemailsanitize_emailEmail address
urlurlesc_url_rawURL
integernumberintvalWhole numbers
booleancheckboxcustomTrue/false values
datedatesanitize_text_fieldDate (YYYY-MM-DD)
datetimedatetime-localsanitize_text_fieldDate and time
repeaterrepeaterJSON sanitizerCollection of sub-items

Defining Fields

Fields can be defined in two formats:

Simple Format

Use a string for the type:

'fields' => [
'name' => 'string',
'email' => 'email',
'count' => 'integer',
'notes' => 'text',
]

Complex Format

Use an array with a type key for additional configuration:

'fields' => [
'name' => [
'type' => 'string',
'label' => 'Full Name',
'placeholder' => 'Enter your name',
'description' => 'Your legal name',
],
'quantity' => [
'type' => 'integer',
'min' => 1,
'max' => 100,
],
]

The complex format is required for repeater fields and allows additional configuration for any type.

Field Configuration Options

Common options available in complex format:

OptionTypeDescription
typestringThe field type (required)
labelstringDisplay label (auto-generated from name if not set)
placeholderstringPlaceholder text for input
descriptionstringHelp text shown below the field

Type-specific options:

TypeOptionDescription
integerminMinimum value
integermaxMaximum value
textrowsNumber of textarea rows

Boolean Sanitization

The boolean sanitizer accepts various truthy values:

  • true, '1', 'true', 'yes', 'on'true
  • false, '0', '', 'no', any other value → false

Type Coercion

DataView automatically coerces values based on field type:

// Integer field with string input
$handler->create(['count' => '5']); // Stored as integer 5

// Boolean field with various inputs
$handler->create(['active' => 'yes']); // Stored as true
$handler->create(['active' => '0']); // Stored as false

Custom Field Types

You can register custom field types. See Custom Field Types for details.

$registry = $view->get_field_registry();

$registry->register_type('phone', [
'dataset' => DataSet::TYPE_STRING,
'sanitizer' => function($value) {
return preg_replace('/[^0-9+\-\s()]/', '', $value);
},
'schema' => ['type' => 'varchar', 'length' => 20],
'input' => 'tel',
]);