How to implement validation for custom plugin fields?

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

However, I can’t seem to figure out how it works. There’s a thread about that by @timoetting but it appears to cover back-end validation. I’m asking about front-end one? The brand new cookbook recipe 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.

1 Like

Pretty old post, is there any documentation available yet?

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

The linked post here (this one) 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:

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');
         }
      },
   ],
   // ...
]