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.

Form Questions /

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

Form action button failing when URL parameter supplied


Go to End


1606 Views

Avatar
MJA

Community Member, 21 Posts

17 December 2015 at 10:23pm

Edited: 17/12/2015 10:30pm

Hi Guys - got weird one here

I have a form that can be entered either with or without a pre-populated field, controlled by a command line parameter
(eventually this will supplied in the $_POST array but for test purposes I am using the $_GET array)

The form sits on it's own custom page type

If this is called normally (i.e. just the URL with no parameter) everything works fine. I can enter a value into the field, click the Submit button and the doSubmit action works correctly.

If it is called with a parameter (i.e. the URL with a query string to populate the $_GET array) the form displays correctly with the field pre-populated, but clicking on the submit button displays blank screen with the error message: Action "doSubmit" not allowed on form (Name: "SurveyForm").

Can anyone explain what is happening here, and how to get around this?
And help would be greatly appreciated.

Thanks in advance
Mike Armstrong
(Silvertoad Ltd, UK)

Code follows:

Controller

	class surveyPage_Controller extends Page_Controller {
		
		private static $allowed_actions = array (
			'SurveyForm'
		);
		
		public function SurveyForm() {
		
			Session::clear('SurveyModuleSessionData');
		
			// get data from post array
			var_dump ($_GET); echo '<br />';
			list($write_session, $session_data) = $this->getClientRecord($_GET);
			if ($write_session && $session_data)
				Session::set('SurveyModuleSessionData', $session_data);
				
			// and call the form
			return new clientSurveyForm($this, 'SurveyForm');
		}
		
		private function getClientRecord($data) {
			$session_data = null;
			$write_session = true;
			if (isset($data) && is_array($data)) {
				$client_id = isset($data['Reference']) ? trim($data['Reference']) : false;
				if ($client_id) {
					$client = responseClient::get()->filter(array('Reference' => $client_id));
					$num_records = $client->Count();
					switch ($num_records) {
						case 0:
							// need to create a new client record
							$client = new responseClient();
							$client->Reference = trim($data['Reference']);
							$client->write();
							$session_data = array('CurrentClient' => $client->ID);
							break;
						
						case 1:
							// this is the one we need
							$session_data = array('CurrentClient' => $client->first()->ID);
							break;
						
						default:
							// we have more than one record with this reference
							// should never happen, but does no harm to check
							$write_session = false;
							break;
					}
				}
			}
			return array($write_session, $session_data);
		}
		
	}	// end class surveyPage_Controller

Form

<?php
if (!class_exists('clientSurveyForm')) {
	class clientSurveyForm extends Form {
		protected	$controller		= null;
		private		$client			= null;
		
		// *********** start form definition ***********
		public function __construct ($controller, $name, $fields = null, $actions = null, $validation = null) {
			$this->controller = $controller;
			
			// *** get the client record if called with a client details package ***
			$session_data = Session::get('SurveyModuleSessionData');
			var_dump ($session_data); echo '<br />';
			if (isset($session_data) && is_array($session_data))
				$this->client = responseClient::get()->filter(array('ID' => $session_data['CurrentClient']))->first();
			
			// *** form fields ***
			if ($fields === null) {
				$fields = FieldList::create (
					TextField::create('Reference')
						->setTitle('Enter your unique reference')
						->setValue($this->client ? $this->client->Reference : '')
				);
			}
			
			// *** form action buttons ***
			if ($actions === null) {
				$actions = FieldList::create (
					FormAction::create('doSubmit')->setTitle('Submit')
				);
			}
			
			// *** form validation ***
			if ($validation === null) {
				$validation = RequiredFields::create ();
			}
			
			parent::__construct ($controller, $name, $fields, $actions, $validation);
		}
		// *********** end form definition ***********
		
		// *********** start form actions ***********
		public function doSubmit ($data, $form) {
			Session::clear('SurveyModuleSessionData');
			
			// find or create the client record
			$write_session = true;
			if (isset($data['Reference']) && $data['Reference'] != '') {
				$client = responseClient::get()->filter(array('Reference' => trim($data['Reference'])));
				$num_records = $client->Count();
				switch ($num_records) {
					case 0:
						// need to create a new client record
						$client = new responseClient();
						$client->Reference = trim($data['Reference']);
						$client->write();
						$session_data['CurrentClient'] = $client->ID;
						break;
					
					case 1:
						// this is the one we need
						$session_data['CurrentClient'] = $client->first()->ID;
						break;
					
					default:
						// we have more than one record with this reference
						// should never happen, but does no harm to check
						$form->addErrorMessage('Reference', "Opps, something's gone wrong - there appears to be two of these", 'bad');
						$write_session = false;
						break;
				}
			}
			
			// *** loop back to the current page ***
			if ($write_session)
				Session::set('SurveyModuleSessionData', $session_data);
			$this->controller->redirectBack();
		}
		// *********** end form actions ***********
		
	}	// end class clientSurveyForm
}
?>