# invalid()

Runs a number of validators on a set of data and checks if the data is invalid

****

- Source: <https://github.com/getkirby/kirby/tree/5.5.3/config/helpers.php#L251>

****

```php
invalid(array $data = [ ], array $rules = [ ], array $messages = [ ]): array
```

## Parameters

| Name | Type | Default | Description |
| ---- | ---- | ---- | ---- |
| `$data` | `array` | `[ ]` | – |
| `$rules` | `array` | `[ ]` | – |
| `$messages` | `array` | `[ ]` | – |

## Return type

`array`

## Examples

```php
$data = [
  'username' => 'Homer',
  'fname'    => 'Homer',
  'lname'    => 'Simpson',
  'email'    => 'home@simpsonscom',
  'gender'   => 'male'
];

$rules = [
  'username' => ['required'],
  'fname'    => ['required', 'minLength' => 3],
  'lname'    => ['required', 'maxLength' => 30],
  'email'    => ['required', 'email'],
  'gender'   => ['in' => [['male', 'female', 'non-binary', 'not specified']]]
];

$messages = [
  'fname' => 'Please enter a valid first name',
  'lname' => 'Please enter a valid last name',
  'email' => 'Please enter a valid email address'
];

if ($invalid = invalid($data, $rules, $messages)) {
  dump($invalid);
} else {
  dump('Nice data!');
}
```

You can change these rules based on the type of data you want to obtain and use <a href="https://getkirby.com/docs/reference/system/validators">Kirby&#039;s validators</a> or your own <a href="https://getkirby.com/docs/reference/plugins/extensions/validators">custom validators</a>.

The syntax for the `$rules` array supports the following variants:

- To use a validator without parameters, use the validator name as the array value: `['required']`
- To pass additional parameters to a validator, use the validator's name as the array key, and the parameter(s) as the value.
  - Scalar values can be passed directly:
  `['maxLength' => 20]`
  - You can pass multiple parameters to the validator as an array:
  `['between' => [10, 100]]`
  - To pass an array as a single parameter, use a nested array:
  `['in' => [['foo', 'bar']]]`

You can also separately define a message for each validation rule:

```php
$messages = [
  'fname' => 'Please enter a valid first name',
  'lname' => 'Please enter a valid last name',
  'email' => [
    'Please enter an email address',
    'The email address is invalid'
  ]
];
```

If your site is set up to support multiple languages, you use <a href="https://getkirby.com/docs/guide/languages/custom-language-variables">custom language variables</a> to translate the messages. Reference your translations using the <a href="https://getkirby.com/docs/reference/templates/helpers/t">`t()`-method.</a> We recommend you set a fallback when using `t()`.

```php
$messages = [
    'fname' => t('fname', 'Please enter a valid first name'),
    'lname' => t('lname', 'Please enter a valid last name'),
    'email' => t('email', 'Please enter a valid email address')
];
```

You can find an example of `invalid()` used to create pages from frontend in <a href="https://getkirby.com/docs/cookbook/forms/creating-pages-from-frontend">this recipe</a>.