Hi,
Is there a way to specify a maximum number of characters in a text field in the CMS? I can't imagine that no one has thought of this! I am looking for the equivalent to the "maxlength" HTML input attribute.
Thanks,
Garrett
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
Hi,
Is there a way to specify a maximum number of characters in a text field in the CMS? I can't imagine that no one has thought of this! I am looking for the equivalent to the "maxlength" HTML input attribute.
Thanks,
Garrett
Hi Garrett-
The $maxLength variable can be passed when you set up the TextField:
new TextField(
$name = 'Word',
$title = 'Short Word',
$value = $myPage->Word,
$maxLength = 4;
);
You can also check out the docs for more info:
http://api.silverstripe.com/forms/fields-basic/TextField.html#__construct
Hope that helps!
-John
Hi,
Thanks so much for your reply, but it's not working for me. I am still able to type in as many characters as I want, and it publishes successfully. Here's my code:
$fields->addFieldToTab("Root.Content.Main", new TextAreaField('ProjectChallenge', 'The Challenge', '' , 25), 'Content');
What's wrong with this picture??
Thanks again,
Garrett
Hi Garrett-
The code you're showing is asking for a TextAreaField. Try again with a TextField...
$fields->addFieldToTab(
'Root.Content.Main',
new TextField(
'ProjectChallenge',
'The Challenge',
'',
25
),
'Content'
);
Let me know how that works out for you!
Thanks,
John
That works, yeah, thanks! Sure wish you could use that for a TextAreaField, though....
//Garrett