statamic / ideas

đŸ’¡Discussions on ideas and feature requests for Statamic
https://statamic.dev
30 stars 1 forks source link

To restrict user input in fields according to Character Limit #1017

Closed jayrdi closed 11 months ago

jayrdi commented 11 months ago

I'm trying to restrict the number of chars a user can enter for an article summary to display on an index card.

I have the fieldtype "textarea" with the "character limit" set to 150.

If you go into the News Article it allows you to enter more than 150 chars though, and the char counter under the window just displays e.g. 167/150 in red.

I know that users will ignore this char counter and just enter whatever they like! So, could we implement this as a feature to restrict a user entering more characters than have been set my the limit in the Blueprint?

joshuablum commented 11 months ago

Hey @jayrdi,

That's what validation rules are for. Just set it to max:150 for the article summary in the Blueprint and your users won't be able to save the entry if the summary exceeds 150 characters.

image image
jayrdi commented 11 months ago

@joshuablum that's perfect, thanks a lot!

jayrdi commented 11 months ago

@joshuablum oh, actually I tried that and it still lets me save...

So, I have the fieldtype set to "character limit" of 150 and under validation I have set "max:150". In the article_summary the char counter goes red when it exceeds 150 but still lets me save. Is this because I'm running a local build at the moment?

joshuablum commented 11 months ago

@jayrdi Tried it locally (took the screenshots then) and it doesn't let me save and works as intended. Make sure to clear the caches and do a hard refresh in your browser.

That's the relevant part of the Blueprint:

handle: article_summary
    field:
        character_limit: '150'
        antlers: false
        type: textarea
        display: 'Article summary'
        icon: textarea
        listable: hidden
        instructions_position: above
        visibility: visible
        hide_display: false
        validate:
          - 'max:150'
jayrdi commented 11 months ago

@joshuablum got it, thanks. I added max:150 but didn't press enter to actually add it to the list of rules! đŸ™„

Thanks for your help!