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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Making CMS-Fields required


Go to End


17 Posts   9222 Views

Avatar
Willr

Forum Moderator, 5523 Posts

11 January 2014 at 8:42am

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());
        }
 

Avatar
Willr

Forum Moderator, 5523 Posts

11 January 2014 at 4:04pm

Avatar
HARVS1789UK

Community Member, 31 Posts

11 January 2014 at 10:27pm

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

Avatar
muskie9

Community Member, 24 Posts

16 January 2014 at 9:15am

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

Avatar
Willr

Forum Moderator, 5523 Posts

16 January 2014 at 7:43pm

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

Avatar
muskie9

Community Member, 24 Posts

16 January 2014 at 8:50pm

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.

Avatar
Willr

Forum Moderator, 5523 Posts

16 January 2014 at 9:11pm

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)

Avatar
muskie9

Community Member, 24 Posts

16 January 2014 at 9:16pm

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

Thanks for the direction!