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.

Customising the CMS /

ModelAdmin: Length limitation on Varchar


Reply


3 Posts   1936 Views

Avatar
xeraa

Google Summer of Code Hacker, 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?

Avatar
Willr

Forum Moderator, 5513 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.

Avatar
xeraa

Google Summer of Code Hacker, 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?