Jump to:

5540 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Am I missing the obvious? Making field required in getCMSFields...

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

Page: 1
Go to End
Author Topic: 2461 Views
  • ImacSS
    Avatar
    Community Member
    35 Posts

    Am I missing the obvious? Making field required in getCMSFields... Link to this post

    Anyone know how I can enforce a field to be required when overriding the getCMSFields method?

  • Mad_Clog
    Avatar
    Community Member
    78 Posts
  • ImacSS
    Avatar
    Community Member
    35 Posts

    Re: Am I missing the obvious? Making field required in getCMSFields... Link to this post

    Fantastic! Thanks Mad_Clog - that's it...you rock!

    ...well, you still rock but...unfortunately it does not appear to be working in my situation.

    When I added the getCMSValidator, and then attempt to post the new page without the required fields, it just goes ahead and posts without complaint. I am using SS 2.3.7.

    Here is the code I am using...

    class GatedContent extends Page
    {
       static $db = array(
          'Date' => 'Date',
          'FormID' => 'Varchar(4)',
          'DeployNum' => 'Varchar(10)'
       );

       function getCMSFields()
       {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab('Root.Content.Main', new DatePickerField('Date'), 'Content');
          $fields->addFieldToTab('Root.Content.Main', new TextField('FormID','Form ID (Required)'), 'Content');
          $fields->addFieldToTab('Root.Content.Main', new TextField('DeployNum', 'Deployment Number (Required)'), 'Content');

          // Remove the widget holders for this content type
          $fields->removeFieldFromTab('Root.Content', 'RelatedMenu');

          return $fields;
       }

       // Enforce FormID and DeployNum as being required
       function getCMSValidator()
       {
          return new RequiredFields('FormID','DeployNum');
       }

       ...
       ...
    }

  • Mad_Clog
    Avatar
    Community Member
    78 Posts

    Re: Am I missing the obvious? Making field required in getCMSFields... Link to this post

    I just tried it out on the default Page class and everything works as expected

    <?php
    class Page extends SiteTree {

       public static $db = array(
       );

       public static $has_one = array(
       );

       /**
        * Add a custom validator
        *
        * @access public
        * @return RequiredFields
        */
       public function getCMSValidator() {
          return new RequiredFields('Title', 'MenuTitle');
       }
    }
    class Page_Controller extends ContentController {

       /**
        * An array of actions that can be accessed via a request. Each array element should be an action name, and the
        * permissions or conditions required to allow the user to access it.
        *
        * <code>
        * array (
        * 'action', // anyone can access this action
        * 'action' => true, // same as above
        * 'action' => 'ADMIN', // you must have ADMIN permissions to access this action
        * 'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
        * );
        * </code>
        *
        * @var array
        */
       public static $allowed_actions = array (
       );

       public function init() {
          parent::init();

          // Note: you should use SS template require tags inside your templates
          // instead of putting Requirements calls here. However these are
          // included so that our older themes still work
          Requirements::themedCSS('layout');
          Requirements::themedCSS('typography');
          Requirements::themedCSS('form');
       }
    }

  • Ben_W
    Avatar
    Community Member
    80 Posts

    Re: Am I missing the obvious? Making field required in getCMSFields... Link to this post

    I think Mad_Clog is using 2.4 above version. ImacSS is using 2.3.7, so give this a try.

    in the /cms/code/CMSMain.php line 416 add in following code

    //$form = new Form($this, "EditForm", $fields, $actions);
    $validator = ($record->hasMethod('getCMSValidator')) ? $record->getCMSValidator() : null;
    $form = new Form($this, "EditForm", $fields, $actions, $validator);

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