Jump to:

23355 Posts in 17691 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Only allow 20 characters for Varchar in admin area

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1478 Views
  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Only allow 20 characters for Varchar in admin area Link to this post

    In a tab, there is a Varchar field that I want to contain a maximum of 20 characters.

    <input type="text" maxlength="20" />

    <?php
    class IndexPage extends Page {
    static $db = array(
    'Banner1Title' => 'Varchar''
    );

    static $has_one = array(
    );
       
    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.Banner", new TextField('Banner1Title','Banner 1 Title'));
    return $fields;
    }
    }

    class IndexPage_Controller extends Page_Controller {
    }
    ?>

  • PGiessler
    Avatar
    Community Member
    47 Posts

    Re: Only allow 20 characters for Varchar in admin area Link to this post

    Hi hknight,

    if you want to set a maximum of 20 characters. You have to use this source code:

    <?php
    class IndexPage extends Page {
    static $db = array(
    'Banner1Title' => "Varchar(20)"
    );

    ...

    Best regards,

    Pascal

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Only allow 20 characters for Varchar in admin area Link to this post

    What PGiessler posted is not entirely correct.
    This would only change the amount of characters allowed in your DB Field. AFAIK this will not put any restriction on your TextField, therefore it would be possible to enter more than 20 characters, but only 20 would be stored in the Database.

    To limit the characters on a TextField, pass the max number of characters as 4th parameter to the TextField Constructor. Eg.

    /*
    1) field-name
    2) title
    3) value
    4) max-length
    */
    new TextField('Banner1Title', 'Banner 1 Title', '', 20);

    Having a look at the source or API Docs would help in these cases...

  • PGiessler
    Avatar
    Community Member
    47 Posts

    Re: Only allow 20 characters for Varchar in admin area Link to this post

    Hi banal,

    thank you for your post. Your approach is the right one. First of all you have to put restrictions on the textfield and then you can put this in the database. But I think, you doesn't need the restrictions in the database; at most for security reasons.

    Best regards,

    Pascal

    1478 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.