Word Count instead of Character count for field validation?

Good morning everyone!

Is it possible to validate for wordd count instead of character count in blueprints?
There was a PR last year, was it included? For larger descriptions and SEO a word count validation would make sense :slight_smile:!

All the Best

I’m not sure, but you could add your own validator, can’t find it in the docs though …

The PR was included in Kirby 2.3 as far as I can tell, but it is not about validation by number of words but just about a visible counter in the Panel.

As @texnixe wrote, you can use a custom validator for this. Place this code into a plugin in site/plugins:

v::$validators['maxWords'] = function($value, $max) {
  return v::max(explode(' ', $value), $max);

You can then use the validator in your blueprint:

    label: Description
    type: text
      maxWords: 20
1 Like

@lukasbestle Is there anything in the docs about custom validators? I was looking for it a while ago and could not find anything?

1 Like

I don’t think so. I will add a page about them to the new developer guide.


I didn’t even know this was possible! Great feature.

I just checked the PR again, it was in fact a counter for characters that was added.

Thanks for your solution Lukas! Don’t you think it would make sense to add word count to the core validator? So it is possible to switch between character- and word count? excerpt( ) also features two $modes called 'chars' and 'words'.

Yes, that’s actually a great idea.
I have created a PR for this, but this will most likely not be included in Kirby 2.3 because we are already in bug fixing stage.

I have also documented custom validators now. The documentation will soon be on the Kirby site.