# How to implement validation for custom plugin fields?

**URL:** <https://forum.getkirby.com/t/how-to-implement-validation-for-custom-plugin-fields/17225>\
**Category:** Questions\
**Tags:** v3\
**Created:** [February 11, 2020, 3:22pm UTC](https://forum.getkirby.com/t/how-to-implement-validation-for-custom-plugin-fields/17225 "2020-02-11T15:22:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hdodov](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/hdodov/32/8171_2.png) [@hdodov](https://forum.getkirby.com/u/hdodov)\
**Post date:** [February 11, 2020, 3:22pm UTC](https://forum.getkirby.com/t/how-to-implement-validation-for-custom-plugin-fields/17225/1 "2020-02-11T15:22:24Z")

</div>

In GitHub, I can see that Kirby internally uses an `invalid` event to validate:

> <https://github.com/getkirby/kirby/blob/master/panel/src/components/Forms/Input/TextInput.vue#L92>

However, I can’t seem to figure out how it works. There’s [a thread](https://forum.getkirby.com/t/validation-in-custom-fields/13379) about that by @timoetting but it appears to cover back-end validation. I’m asking about front-end one? The brand new [cookbook recipe](https://getkirby.com/docs/cookbook/extensions/first-panel-field) by @texnixe regarding custom fields doesn’t seem to cover it as well.

Is there official documentation for how custom field validation should be implemented? My end goal is to add validation for my Link Field and fix [this issue](https://github.com/OblikStudio/kirby-link-field/issues/3).

---

<div class="post-metadata">

**Author:** ![N247S](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/n247s/32/9130_2.png) [@N247S](https://forum.getkirby.com/u/N247S)\
**Post date:** [October 17, 2020, 6:39pm UTC](https://forum.getkirby.com/t/how-to-implement-validation-for-custom-plugin-fields/17225/2 "2020-10-17T18:39:56Z")

</div>

Pretty old post, is there any documentation available yet?

Kinda need the specs/documentation on front-end validation managed by Kirby as well.

---

<div class="post-metadata">

**Author:** ![isaac](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/isaac/32/302_2.png) [@isaac](https://forum.getkirby.com/u/isaac)\
**Post date:** [December 7, 2022, 10:45am UTC](https://forum.getkirby.com/t/how-to-implement-validation-for-custom-plugin-fields/17225/3 "2022-12-07T10:45:23Z")

</div>

The linked post here ([this one](https://forum.getkirby.com/t/validation-in-custom-fields/13379)) pointed me in the right direction. It seems the validation check itself (which conditionally prevents a user from saving an entry) has to be defined in PHP (even for custom client-side plugins/fields).

The basic idea is to throw an exception if the validation does not pass.

Something like this:

```php
use Kirby\Exception\Exception;

// ...

'fields' => [
   'validations' => [
      'required' => function ($values) {
         $parsed = Json::decode($values);
         if (!$parsed || (!$parsed['parents'] && !$parsed['children'])) {
            throw new Exception('Please select at least one category');
         }
      },
   ],
   // ...
]

```
