# Fields

In HivePress, field types are implemented as PHP classes with properties and methods that determine the validation and rendering of the field HTML. For example, the `Email` field type renders a text input that accepts email addresses only. You can use any of the existing field types in forms or even create a new one if required.

### Creating field types

If you are developing a custom HivePress [extension](/developer-docs/tutorials/create-a-custom-hivepress-extension.md), you may need to create a new field type. To do this, create a new `class-{field-type}.php` file (use lowercase letters, numbers, and hyphens only) in the `includes/fields` extension subdirectory and HivePress will load it automatically.

{% hint style="info" %}
It’s good practice to re-use the existing HivePress field types and avoid creating new ones if possible. For example, a new type may be required if you need a completely new UI element, or if it requires some custom logic.
{% endhint %}

Then, define the field type PHP class. The class name should be based on the file name but with underscores instead of hyphens and no lowercase restriction (e.g. `Foo_Bar` class for the `class-foo-bar.php` file). Pick a name that is unique enough to avoid conflicts with other HivePress field types.

```php
<?php
namespace HivePress\Fields;

use HivePress\Helpers as hp;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

class Foo_Bar extends Field {

	// Declare the field properties.
	protected $max_length;

	// Set the property defaults.
	public function __construct( $args = [] ) {
		$args = hp\merge_arrays(
			[
				'max_length' => 123,
			],
			$args
		);

		parent::__construct( $args );
	}

	// Do something after the field is loaded.
	protected function boot() {
		if ( $this->max_length ) {
			$attributes['maxlength'] = $this->max_length;
		}

		parent::boot();
	}

	// Normalize the field value.
	public function normalize() {
		parent::normalize();

		if ( ! is_null( $this->value ) ) {
			$this->value = trim( wp_unslash( $this->value ) );
		}
	}

	// Sanitize the field value.
	public function sanitize() {
		$this->value = sanitize_text_field( $this->value );
	}

	// Validate the field value.
	public function validate() {
		if ( parent::validate() && strlen( $this->value ) > $this->max_length ) {
			$this->add_errors( 'Custom error message' );
		}

		return ! $this->errors;
	}

	// Render the field HTML.
	public function render() {
		return '<input type="text" name="' . esc_attr( $this->name ) . '" value="' . esc_attr( $this->value ) . '" ' . hp\html_attributes( $this->attributes ) . '>';
	}
}

```

The code example above implements a field type that normalizes values by trimming whitespace and removing slashes, then sanitizes them by removing HTML and special characters, and finally validates values based on its `max_length` property (set to `123` by default). The field itself is rendered as an HTML input of `text` type.

Also, a custom field type can be based on another type's PHP class, allowing you to re-use its properties and methods without repeating the code:

```php
<?php
namespace HivePress\Fields;

use HivePress\Helpers as hp;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

class Foo_Bar extends Number {

	// Override properties and methods of Number type.
}

```

In the same way, you can create custom field types for your HivePress extension.

### Customizing field types

You can customize any of the existing field types using [hooks](https://hivepress.github.io/hook-reference/). For example, the code below changes the default date format for the `Date` field type.

```php
add_filter(
	'hivepress/v1/fields/date',
	function( $field ) {
		$field['display_format'] = 'F j, Y';

		return $field;
	}
);
```

Similarly, you can customize any field type in HivePress or its extensions.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hivepress.io/developer-docs/framework/fields.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
