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.

General Questions /

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

getCMSValidator() within DataObject?


Reply


3 Posts   475 Views

Avatar
George Botley

Community Member, 16 Posts

15 October 2013 at 2:13am

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.

Avatar
George Botley

Community Member, 16 Posts

16 October 2013 at 11:24am

bump.

Avatar
zenmonkey

Community Member, 528 Posts

16 October 2013 at 2:50pm

I tried it 3.1 and seems to work