Jump to:

23375 Posts in 18175 Topics by 2866 members

General Questions

SilverStripe Forums » General Questions » getCMSValidator() within DataObject?

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: 391 Views
  • George Botley
    Avatar
    Community Member
    16 Posts

    getCMSValidator() within DataObject? Link to this post

    Hi Guys,

    I'm constructing a news article module for my client making use of DataGrid's to house data. Now obviously when you select to add a new record to the DataGrid, in this case a new article, I want to be able to ensure that the fields needed are completed so no blank news articles are posted, or so no articles are posted that don't belong to a category.

    Here's my code for the extension of the dataobject..

    <?php

    /****************************************************************
    *                                                *
    *   ESTABLISH PARAMETERS FOR EACH NEWS ARTICLE               *
    *                                                *
    *****************************************************************/

    class NewsArticle extends DataObject {

    public static $db = array(   
        'SortOrder' => 'Int',
        'Title' => 'Varchar',
        'Category' => 'Int',
        'Content' => 'HTMLText'   
    );

    //One-to-one relationship with news page
    public static $has_one = array('News' => 'News');

    //Tidy up CMS by removing the fields we don't want users to modify.
    public function getCMSFields() {
          $fields = parent::getCMSFields();
          
          //Remove Title field and readd it with a new title
          $fields->removeFieldFromTab("Root.Main","Title");
          $fields->addFieldToTab("Root.Main", new TextField("Title", "Article Name"), "Content");
          
          //Add dropdown with category options
          $dropdown = new DropdownField('Category','Article Category',Dataobject::get("NewsCategory")->map("ID", "Title"));
          $dropdown->setEmptyString("(Select One)");
          $fields->addFieldToTab("Root.Main", $dropdown, "Content");
          
          //Remove fields that will confuse people
          $fields->removeFieldFromTab("Root.Main","NewsID");
          $fields->removeFieldFromTab("Root.Main","SortOrder");
          
          return $fields;      
    }

       //Set Required Fields
       public function getCMSValidator() {
          return new RequiredFields('Title');
       }

       

    // Tell the datagrid what fields to show in the table
    public static $summary_fields = array('ID', 'Title');

    }

    Now obviously, i've set getCMSValidator() to check the fields, as would work on an extension of Page and the SiteTree, but it does not work.

    Is there something obvious i'm missing here, i've trawled the web for hours to find the answer and its doing my head in.. Sorry if this is in the wrong section I'm new on here and couldn't quite guess which one to put it under.

    George.

  • George Botley
    Avatar
    Community Member
    16 Posts

    Re: getCMSValidator() within DataObject? Link to this post

    bump.

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: getCMSValidator() within DataObject? Link to this post

    I tried it 3.1 and seems to work

    391 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.