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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

SOLVED Trouble with $allowed_actions in SS3.1


Go to End


3 Posts   1052 Views

Avatar
Harley

Community Member, 165 Posts

28 October 2013 at 2:02pm

Hi there,

I've done this a ton of times and never had the problem before although I have updated to SS3.1. I have a home page type with a contact form within it. When I submit the form I am getting a blank page. Here is my code

<?php
class HomePage extends SiteTree {

	private static $db = array(
		'Mailto' => 'Varchar(100)',
		'Telephone' => 'Varchar(100)',
		'SubmitText' => 'HTMLText',
		"AddToFooterMenu" => "Boolean",
		"MissionStatement" => "HTMLText",
		"ContactContent" => "HTMLText"
	);

	private static $has_one = array(
		"CVPDF" => "File"
	);

	private static $defaults = array(
		'ContactContent' => '<p>If you would like to get in touch please leave your details and we will get back to you.</p><p>Alternatively you can call me on +44 (0) xxxxxxxxx</p><p>I look forward to hearing from you.</p>',
		'SubmitText' => 'Thank you for your email! You will recieve a reply shortly.'

	);

	function getSettingsFields() {
		$fields = parent::getSettingsFields();
		$fields->addFieldToTab("Root.Settings",
			new CheckboxField(
				'AddToFooterMenu',
				_t('Page.ADDTOFOOTERMENU', 'Add this page to the footer menu')
			)
		);
		return $fields;
	}

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

		$fields->addFieldToTab("Root.Contact.OnSubmission", new TextField('Mailto', 'Email submissions to'));
		$fields->addFieldToTab("Root.Contact.Telephone", new TextField('Telephone', 'Telephone number'));
		$fields->addFieldToTab("Root.Contact.OnSubmission", new HTMLEditorField('SubmitText', 'Text on Submission'));

		$fields->removeFieldFromTab("Root.Main", "Content");
		$fields->insertBefore(new Tab("ItemContentTop", 'Item content top'), 'Metadata');
        $fields->addFieldToTab("Root.Main.ItemContentTop", new UploadField("CVPDF","Upload a CV"));
		$fields->addFieldToTab("Root.Main.ItemContentTop", new HTMLEditorField("MissionStatement", "Mission statement"));
		$fields->addFieldToTab("Root.Main.ItemContentTop", new HTMLEditorField("ContactContent", "Contact content"));

		return $fields;
	}
}

class HomePage_Controller extends PageController {

	static $allowed_actions = array(
		"ContactForm" => true,
		"SendContactForm" => true,
		"Success" => true
	);

	public function init() {
		parent::init();

		// ******************** concatonate all javascript and css ******************** //

		$theme_folder = sprintf('themes/%s', SSViewer::current_theme());

		$js_files = array(
			sprintf('%s/js/jquery-1.10.2.min.js', $theme_folder),
			sprintf('%s/js/jquery-ui-1.10.3.custom.min.js', $theme_folder),
			sprintf('%s/js/helper.js', $theme_folder),
			sprintf('%s/js/common.js', $theme_folder),
		);

		$css_files = array(
			sprintf('%s/css/core.css', $theme_folder),
		);
		foreach($js_files as $js) {
			Requirements::javascript($js);
		}

		foreach($css_files as $css) {
			Requirements::css($css);
		}

		Requirements::combine_files("js.js", $js_files);
		Requirements::insertHeadTags("<!--[if lt IE 9]><script type='text/javascript' src='" . $theme_folder . "/js/selectivizr.js'></script><![endif]-->");

		Requirements::combine_files("css.css", $css_files);
		Requirements::css($theme_folder.'css/print.css', 'print');
		Requirements::insertHeadTags("<!--[if lt IE 9]><style type='text/css'>@import url(" . $theme_folder . "/css/ie.css);</style><![endif]-->");
		Requirements::process_combined_files();
	}

	function footerMenu() {
		$whereStatement = "AddToFooterMenu = 1";
		return DataObject::get("Page", $whereStatement);
	}

	public function ContactForm() {

      	// create fields
	    $fields = new FieldList(
		    new TextField('Name', 'Name'),
			new EmailField('Email', 'Email'),
			new TextField('Company', 'Company'),
			new TextareaField(
				$name = "Comments",
				$title = "Message",
				$value = ""
			)
		);

	    // create action
	    $actions = new FieldList(
	    	new FormAction('SendContactForm', 'Send')
	    );
		// required fields
		$validator = new RequiredFields('Name', 'Email', 'Comments');

		// return the form
	    return new Form($this, 'ContactForm', $fields, $actions, $validator);
	}

	public function SendContactForm($data) {

	 	// set data
		$From = $data['Email'];
		$To = $this->Mailto;
		$Subject = "Website Contact message";
		$email = new Email($From, $To, $Subject);
		// set template
		$email->setTemplate('ContactEmail');
		// populate template
		$email->populateTemplate($data);
		// send mail
		$email->send();
	  	// return to submitted message
		Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1");
	}

	public function Success(){
		return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
	}
}

Can anyone spot where it is going wrong?

Cheers

Avatar
Bereusei

Community Member, 96 Posts

30 October 2013 at 12:19am

Try this: instead of

static $allowed_actions = array( 
      "ContactForm" => true, 
      "SendContactForm" => true, 
      "Success" => true 
   );

use this:

private static $allowed_actions = array(
     'ContactForm'
);

SS3 is a little bit pedantic with "private", "public" things.

Avatar
Harley

Community Member, 165 Posts

30 October 2013 at 3:42pm

I cracked it!

The culprit was in fact the redirect. Instead of

Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1"); 

I needed to have

$this->redirect($this->URLSegment . "/?success=1");

Thanks for the help anyway!