Question about validate: max:

Hi!

I’m doing my first steps with the blueprints and I tryed out that:

  title:
    label: Title
    type:  text
    validate:
      max: 21

If I enter on the kirbiy panel page in the field Title (or my custom text field) a text with 21 characters or less everything works fine. When I enter only digits like 12345 I get the message that I cant save this page.

I know a title consisting only out of digits is very unlikely but who knows.

I hope this helps you to improve kirby! :sunglasses:

Hi @sandbox
If you want to validate the maximum length of a text field you have two options:
max = number of characters or value if only numeric characters.
maxwords = number of words

I hope this makes sense!
Read more about validators in the docs here: https://getkirby.com/docs/cheatsheet/#validators

NOTE: this post was edited to avoid misleading users coming here from another search.

1 Like

Hi @Thiousi

Thank you for the explanation but unfortunately doesn’t seems correct.

If I use max: 21 then I can see a counter in the upper right corner above the text field. And if I use size: 21 I get no counter and the same error (Can’t save the page.) when I enter only digits.

Did you tried it? You shoud :wink:

Huh that’s interesting! I can’t test this right now but I’ll give it a shot tomorrow!

Looking at the code https://github.com/getkirby/toolkit/blob/7a26c83a9e22b4d71a6d4f646152de94e434cdc8/lib/v.php#L99

1 Like

Ah I found it!
The size() function used in max() checks if the value is numeric and in this case returns its value instead of its length.

I think it’s safe to say this is by design. I’ll let the support crew confirm.

Note: If a title is mix characters, you’re safe with max!
I’mm editing my first post given that I was completely wrong and don’t want to mislead people :slight_smile:

1 Like

Ah interesting!

I had already figured it out, that if I add any other character than a digit then it works. It’s a rare case but if you want a titel or in my case a menu item with only digits then you run into it. :grin:

The way I see it, I would expect the field to check the string length even if it just consisted of numbers, so I’d regard this as a bug.

2 Likes

I created an issue on GitHub.

1 Like

This is debatable… I think that separating the length and value would be a better option to give more flexibility. Thoughts?

If you want to validate a number, you should use a number field. IMHO, a text field is a string, so it should check string length. Although we might need another validator.

1 Like

maxlength and minlength would be more obvious.

I’m trying to think of cases where a text field could be used for a number. I think a few can come up.

Well, these cases are probably not that numerous, but think of years as titles, 1984 or whatever.

You’re right I think! It doesn’t look like the validators have an understanding of the type of field they’re in. Or am I missing something?

Exactly, the validator does not take into account the type of field it validates but only the value of the field.

Alright! I love digging into the code to understand what causes the issue like that. Mostly because the Kirby code is easy to read. I would never have done that with another CMS… Thanks for walking along :wink:

2 Likes

I’ll mark this as solved as we have found the cause of the issue and there is now an issue on GitHub.

1 Like

As this will not be fixed before Kirby 2.4, you could write your own custom validator.

Edit: Lukas will try to add this to 2.3.2.

1 Like

And done. Kirby 2.3.2 will be published this week and it will contain the fix for this. Just in time. :slight_smile:

Please note that you need to change your blueprint to benefit from the new behavior:

  title:
    label: Title
    type:  text
    validate:
      maxLength: 21
4 Likes

Thank you in advance! :grinning: But why is it a good idea to fix it with a new term, one with an uppercase character in it? I’m only curious.

The current max validator is used for both text and number fields, it is field type agnostic. Therefore, we need a new validator for strings only. CamelCase is for readability.

1 Like