---
title: "Constraints"
version: "4.0"
permalink: "https://docs.typo3.org/permalink/ameos/ameos_form:constraints@4.0"
source: "Constraints/Index.rst"
rendered: "2026-11-13T14:41:18+00:00"
---

# Constraints {#constraints}

<!-- TODO: no Markdown rendering for "only" -->

**Table of content:**

## Required {#constraints-required}

Set field mandatory. You can add this contraint like this.

```
$form->addConstraint('my_field', 'required', 'My field is required');

```

## Email {#constraints-email}

If set, the field must be filled with valid email. You can add this contraint like this.

```
$form->addConstraint('my_field', 'email', 'My field must be an email');

```

## Sameas {#constraints-sameas}

If set, the field must be filled with the same value as a other field. For password or email confirmation for example. You can add this contraint like this.

```
$form->addConstraint('my_field_confirmation', 'sameas', 'Confirmation is not valid', array('sameas' => 'my_field'));

```

## Unique {#constraints-unique}

If set, the field must be filled with an unique value. You can add this contraint like this.

```
$form->addConstraint('username', 'unique', 'Username must be unique', array('repository' => $this->frontenduserRepository));

```

## Custom {#constraints-custom}

Custom contraint. You can add this contraint like this.

```
$form->addConstraint('username', 'custom', 'Custom validation not valid', array('method' => function($value, $form) {
	// your code here. return true if result is valid. Else, false
}));

```

`method` is executed as is: it must be a callable written in your code (closure, `[$this, 'method']`),
never a value built from request, records or any other external data.

## Filesize {#constraints-filesize}

For upload element. Check the upload file max size. You can add this contraint like this.

```
$form->addConstraint('file', 'filesize', 'File too big', array('maxsize' => '2M'));

```

## Fileextension {#constraints-fileextention}

For upload element. Check the upload file max size. You can add this contraint like this.

```
$form->addConstraint('file', 'fileextention', 'Your file must be a picture', array('allowed' => 'jpg,gif,png'));
```
