Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

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.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Assigning a Maximum Length to Textfields in the CMS


Go to End


5 Posts   3871 Views

Avatar
Garrett

Community Member, 245 Posts

26 July 2008 at 3:14am

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

Avatar
BLU42 Media

Community Member, 71 Posts

30 July 2008 at 3:59am

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

Avatar
Garrett

Community Member, 245 Posts

1 August 2008 at 5:40am

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

Avatar
BLU42 Media

Community Member, 71 Posts

1 August 2008 at 6:57am

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

Avatar
Garrett

Community Member, 245 Posts

1 August 2008 at 7:16am

That works, yeah, thanks! Sure wish you could use that for a TextAreaField, though....

//Garrett