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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

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

ModelAdmin: Length limitation on Varchar

Go to End

3 Posts   2976 Views


Community Member, 58 Posts

10 May 2010 at 3:27am

In my model I've defined: 'Title' => 'Varchar(32)',

In the ModelAdmin I can enter any number of characters and successfully save the object - if I've entered more than 32 characters in the title they are also displayed after saving.
If I reload the object, the title it is cut off after 32 characters (as the database simply doesn't store more than this). I would have expected / hoped for some kind of warning when trying to save more than the possible characters - currently they are lost without any kind of message.

Is this a bug, am I doing something wrong or how can this be fixed?


Forum Moderator, 5523 Posts

10 May 2010 at 10:44pm

I'm not sure ModelAdmin (or any of the CMS form fields) provide this sort of automatic validation for lengths of data. You can set the max length of the TextField (eg 32) in the constructor but don't think a nice error message will be generated (just won't allow any more characters) the form valldation is one of the areas on the todo list.


Community Member, 58 Posts

10 May 2010 at 11:37pm

Thanks for pointing that out. The following did the trick:

	function getCMSFields(){
		$fields = parent::getCMSFields();
		$fields->addFieldToTab('Root.Main', new TextField('Title', 'Title', '', 32), 'Subtitle');
		$fields->addFieldToTab('Root.Main', new TextField('Subtitle', 'Subtitle', '', 64), 'Text');
		return $fields;

Would be cool if SilverStripe could do that automagically as the current cutting of is definitely not nice.
I guess there is no point in filing a ticket as this is already on the roadmap?