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.

Customising the CMS /

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

Am I missing the obvious? Making field required in getCMSFields...


Go to End


5 Posts   4896 Views

Avatar
ImacSS

Community Member, 35 Posts

5 June 2010 at 2:13am

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

Avatar
Mad_Clog

Community Member, 78 Posts

5 June 2010 at 3:09am

Avatar
ImacSS

Community Member, 35 Posts

5 June 2010 at 3:39am

Edited: 05/06/2010 4:05am

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

	...
	...
}

Avatar
Mad_Clog

Community Member, 78 Posts

7 June 2010 at 8:46pm

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

Avatar
Ben_W

Community Member, 80 Posts

19 August 2010 at 6:19pm

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