Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Making CMS-Fields required

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

Page: 1 2 3
Go to End
Author Topic: 673 Views
  • henrik_
    Avatar
    Community Member
    5 Posts

    Making CMS-Fields required Link to this post

    Hey there,

    I have a banal problem but didn't find a working solution though googling it...
    I have a simple DataObject and want to make sure that people using the CMS fill every field.

    class ProgramItem extends DataObject {

       public static $has_one = array('AdditionalProgram' => 'AdditionalProgram');

       public static $db = array('Headline' => "Varchar", 'Description' => "HTMLText", 'ArchivingDate' => "Date");

       function getCMSValidator() {
          return new RequiredFields(array('Headline', 'Description', 'ArchivingDate'));
       }

    }

    I tried overwriting the getCMSValidator()-Function but it didn't work. How does it work using Silverstripe 3.1 ?

    Regards,
    henrik

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Making CMS-Fields required Link to this post

    getCMSValidator is the correct function so that should work. Is your editing interface a ModelAdmin subclass?

  • henrik_
    Avatar
    Community Member
    5 Posts

    Re: Making CMS-Fields required Link to this post

    Hey,
    thanks for your reply!

    No, I think its not... I'm creating/editing the ProgramItems through a GridField. Might this be the problem?

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: Making CMS-Fields required Link to this post

    It seems GridFieldDetailForm (#400) calls $this->component->getValidator() instead of $this->component->getCMSValidator()

    Probably because the GridField isn't CMS perse?

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Making CMS-Fields required Link to this post

    That is what GridField API method is called, but in ModelAdmin it calls the getCMSValidator.

    https://github.com/silverstripe/silverstripe-framework/blob/3.1/admin/code/ModelAdmin.php#L152

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: Making CMS-Fields required Link to this post

    @willr: Yes, but when you use a gridfield in, say, your pages getCMSFields(), getCMSValidator() wouldn't work I think. And that might be a bit confusing...

    Does the gridfield 'know' if it's in the CMS? In tthat case could it be made to select the CMSValidator() instead of getValidator() do you think? Or am I totally wrong here?

  • HARVS1789UK
    Avatar
    Community Member
    14 Posts

    Re: Making CMS-Fields required Link to this post

    I have also been having the same issue as henrik_ for the last couple of hours and it had been driving me insane, so I am glad to have found this post.

    As Willr and Martimiz has discussed it seems to be an issue caused by GridFieldDetailForm not giving any consideration to wether we are in the context of the CMS or not and/or wether the DataObject record being added/edited has a getCMSValidator() method (DataObject doesn't have one to fall back to as a default).

    @Willr and @Martimiz what do you think to my suggested fix below, will it have any negative knock on effects when GridField is used outside of the CMS? If it looks OK to both of you I will make a pull request and register the issue as a bug.

    Suggested Fix

          $toplevelController = $this->getToplevelController();
          if(
           $toplevelController &&
           $toplevelController instanceof LeftAndMain &&
           $this->record->hasMethod('getCMSValidator')
          ) {
           $validator = $this->record->getCMSValidator();
          } else {
           $validator = $this->component->getValidator() ;
          }
          
          $form = new Form(
             $this,
             'ItemEditForm',
             $fields,
             $actions,
             $validator
          );

    Current Code

          $form = new Form(
             $this,
             'ItemEditForm',
             $fields,
             $actions,
             $this->component->getValidator()
          );

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: Making CMS-Fields required Link to this post

    I really needed to educate myself a bit more on the GridField, triggered by Will's comments What ModelAdmin does is just configuring the GridField to use the proper validator. The GridFieldDetailForm doesn't actually do any validation out of the box so you need to tell it which validator to use.

    So this is on a Page class:

       public function getCMSFields() {
          
          $fields = parent::getCMSFields();

          $fields->addFieldsToTab(
             "Root.Griddy", array(
                $gridField = new GridField(
                   'MyObjects',
                   'My Objects',
                   $this->MyObjects(),
                   GridFieldConfig_RecordEditor::create()
                )
          ));

          // add the CMS validator
          if (singleton('MyObject')->hasMethod('getCMSValidator')) {
             $gridField->getConfig()
                 ->getComponentByType('GridFieldDetailForm')
                 ->setValidator(singleton('MyObject')->getCMSValidator());
          }
          return $fields;
       }

    Basically this is how configuring a GridField would work anyway. So I'm not sure we should want to set the GridFieldDetailForm to a fixed validator based on the context. Creating a GridFieldConfig_RecordEditor extension to use in the CMS would be an option...

    673 Views
Page: 1 2 3
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.