Jump to:

3461 Posts in 1065 Topics by 740 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: 1220 Views
  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Making CMS-Fields required Link to this post

    In the short term you can set the validator manually when you create the GridField

    $grid = new GridField('MyObject', 'My Objects', $this->MyObjects(), GridFieldConfig_RecordEditor::create());
    $grid->getConfig()->getComponentByType('GridFieldDetailForm')
             ->setValidator(singleton('MyObject')->getCMSValidator());

    $fields->addFieldToTab('Root.Main', $grid);

    @HARVS1789UK, that seems slightly messy. A cleaner way would be a patch something like below to the detail form. If that patch makes everyone happy, I can push to framework.

    Note I've keep getCMSValidator as this is what ModelAdmin uses. Another suitable patch would be to introduce checking for both getCMSValidator and getValidator.

    diff --git a/forms/gridfield/GridFieldDetailForm.php b/forms/gridfield/GridFieldDetailForm.php
    index 108af99..188565c 100644
    --- a/forms/gridfield/GridFieldDetailForm.php
    +++ b/forms/gridfield/GridFieldDetailForm.php
    @@ -90,6 +90,12 @@ class GridFieldDetailForm implements GridField_URLHandler {
    $handler = Object::create($class, $gridField, $this, $record, $controller, $this->name);
    $handler->setTemplate($this->template);

    + // if no validator has been set on the GridField and the record has a
    + // CMS validator, use that.
    + if(!$this->getValidator() && method_exists($record, 'getCMSValidator')) {
    + $this->setValidator($record->getCMSValidator());
    + }
    +
    return $handler->handleRequest($request, DataModel::inst());
    }

  • Willr
    Avatar
    Forum Moderator
    5511 Posts
  • HARVS1789UK
    Avatar
    Community Member
    21 Posts

    Re: Making CMS-Fields required Link to this post

    Thanks Willr,

    I will go about setting my GridField validator manually for now. Thanks for making a pull request for a better fix, I'm pretty new to digging around in SilverStripe core code so apologies for the less than ideal suggestion.

    Cheers,

    HARVS1789UK

  • muskie9
    Avatar
    Community Member
    10 Posts

    Re: Making CMS-Fields required Link to this post

    I have a semi-related question to the CMS Validator: is there a function to call in a DataExtension for getCMSValidator much like getCMSFields has updateCMSFields?

    Thanks

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Making CMS-Fields required Link to this post

    Not currently single DataObject does not have a getCMSValidator default method. Perhaps worth submitting a PR which adds a default validator (and an extend call).

  • muskie9
    Avatar
    Community Member
    10 Posts

    Re: Making CMS-Fields required Link to this post

    I'll take a crack at it. Haven't done any PR's for framework/cms so I'll do my best. Would this require tests as well? I would need to get my environment setup with UnitTests.

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Making CMS-Fields required Link to this post

    No it wouldn't require unit tests as there wouldn't be much to test (patch would be something like this on DataObject)

    /**
    * @return RequiredFields
    */
    public function getCMSValidator() {
    $validator = new RequiredFields();

    $this->extend('updateCMSValidator', $validator);

    return $validator;
    }

    What you would need to do for a PR ideally is that code change, test the cms to ensure it doesn't break anything and as well document the method in the docs somewhere (to ensure other users can know about this). I'd recommend a note in data object.md under Scaffolding Form Fields, new heading for Validation and include a note about the method and the extend hook exists (like the documentation on updateCMSFields)

  • muskie9
    Avatar
    Community Member
    10 Posts

    Re: Making CMS-Fields required Link to this post

    Cool, I was going to reference getCMSFields/updateCMSFields as I figured it would be similar.

    Thanks for the direction!

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