Anyone know how I can enforce a field to be required when overriding the getCMSFields method?
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.
Have a look at this thread:
http://silverstripe.org/data-model-questions/show/257241
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');
}
...
...
}
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');
}
}
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);