How to limit checkboxes field

I have this blueprint:

pages:
  label: Pages
  type: checkboxes
  options: children

It returns a string with all options checked:

name1, name2, name3, name4, name5

How can I limit the number of options checked. For example limit to only 3:

name1, name2, name3 <= no more

edited: Sorry, edited to hide wrong answer and to not mislead more people :slight_smile:

@philipp: Unfortunately, that is not possible. $page->categories()->split() returns an array, not a collection. And you cannot use the limit() method on an array. You would have to use array_slice(), see PHP Manual.

@heyallan: As far as I know there is no validator for the blueprint. Not sure if you could use a custom validator, though.

1 Like

That would hide the results, but wouldn’t limit the options to the user.
I didn’t want to take that approach, but I’m afraid there is no other way.

BTW I also tried with structure field, but had similar experience: structure field can limit the number of values per entry, but not the number of entries.

What do you think about validating the string with regex?
Something like:

pages:
  label: Pages
  type: checkboxes
  validate:
    match: /regex/

Thanks.

I don’t think that would work. The validator checks the result before the content is written to the content file. And you don’t have a single input field that you could validate.

I get it.

Is there a way I can let the user choose an arbitrary number of pages?

If you mean a “specific arbitrary number”: What about three select fields that each have a width of ⅓? Then it’s clear that you can always select exactly three pages.

3 Select fields is a functional alternative.
That would give me 3 separate fields instead of 1 though.

Trying ‘checkboxes’, ‘structure’, or ‘page’ fields sounded more logical to me at first,
but very different limitations each:

Checkboxes:
Very handy UI, but can’t limit the number of values per field

Structure:
Very handy UI, but can’t limit the number of fields (duplicates)

Page:
‘Page’ field is more like a ‘search’ field, if the user doesn’t remember the name of the page she’s looking for from the top of her head it won’t be very helpful. ‘Search’ and ‘select’ are very different approaches. And if I remember correctly it only accepts 1 value per field

I really liked the ‘checkboxes’ approach, I think I’ll stick with it, I’ll hide any extra values when I catch them at the template part.