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.

Archive /

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

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

Assigning a Maximum Length to Textfields in the CMS


Reply


5 Posts   2237 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:

[url]http://api.silverstripe.com/forms/fields-basic/TextField.html#__construct[/url]

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