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 won't save to DB


Go to End


5 Posts   3194 Views

Avatar
Tread

Community Member, 8 Posts

12 December 2014 at 3:17am

Hallo,

I am new to SilverStripe, and try to code a simple ``callcenter'' page, where calls can be stored and viewed.
The Version of SilverStripe is 3.1.8., and I worked through the tutorials.
To save formdata into the db is handled in tutorial #3, and I thought, I did everything like it is said there,
but the when I submit the data, i just get an empty form as a result, and nothing is written to the db.

Here is my code:

InsertPage.php

<?php
	class InsertPage extends Page
	{
	}
	
	class InsertPage_Controller extends Page_Controller
	{
		private static $allowed_actions = array('insertCall');
		
		public function insertCall()
		{
			$fields = new FieldList(
					new TextField('telefone', 'Telefonnummer')
					, new TextField('name', 'Name')
					, new TextareaField('subject', 'Betreff')
					, new TextareaField('note', 'Anmerkung')
					, new TextField('date', 'Datum', date('d.m.Y', time()))
					, new TextField('time', 'Uhrzeit', date('H:m', time()))
					, new HiddenField('createDate')
					, new HiddenField('createUser')
					, new HiddenField('assignedAgent')
					, new HiddenField('isProcessed')
					, new DropdownField('agent', 'Verantwortlicher', array('heinz' => 'Heinz', 'uwe' => 'Uwe', 'karl' => 'Karl', 'henrette' => 'Henriette', 'walter' => 'Walter', 'eva' => 'Eva'))
					, new DropdownField('topic', 'Thema', array('order' => 'Bestellung', 'info' => 'Information', 'offer' => 'Angebot', 'private' => 'Privat'))
			);
			
			$actions = new FieldList(
					new FormAction('doInsertCall', 'Eintragen')
			);
			
			return new Form($this, 'insertCall', $fields, $actions);
		}
		
		public function doInsertCall($data, $form)
		{
			$submission = new insertCallSubmission();
			$submission->write();
			$form->saveInto($submission);
			$submission->write();
			
			return $this->redirectBack();
		}
	}
?>

insertCallSubmission.php:

<?php
	class insertCallSubmission extends DataObject
	{
		private static $db = array(
				'telefone' => 'Varchar'
				,'name' => 'Varchar'
				,'subject' => 'Text'
				,'note' => 'Text'
				,'date' => 'Date'
				,'time' => 'Time'
				,'createDate' => 'SS_Datetime'
				,'createUser' => 'Varchar'
				,'assignedAgent' => 'Varchar'
				,'isProcessed' => 'Varchar'
				,'agent' => 'Varchar'
				,'topic' => 'Varchar'
		);
	}
?>

What am I missing?
Thanks in advance for your help.

Avatar
Fred Condo

Community Member, 29 Posts

13 December 2014 at 7:27am

I copied your code, and it works. In the template, you need to put "$insertCall" -- this will render the form. Upon submission, it does save to the database. I have confirmed this by examining the table:

mysql> select telefone, name, subject from insertCallSubmission;
+----------+------------+---------+
| telefone | name       | subject |
+----------+------------+---------+
| 123      | Fred Condo | Fish    |
+----------+------------+---------+ 

The reason that you go back to the blank form is that you have explicitly requested this by calling $this->redirectBack().

You can display a confirmation by changing the last statement of doInsertCall to this:

return $this->customise(['insertCall' => 'Vielen Dank. Wir haben Ihre Daten erhalten haben.']);

Avatar
Tread

Community Member, 8 Posts

16 December 2014 at 12:19am

Thanks a lot for your answere Fred, but this seems to be no Form Question anymore.
I tried again to insert data with the form, and after an apache restart, it worked.
But after I searched the internet for validation in SilverStripe, and added required fields
it stopped working again, even when I removed the new code.
I have all PHP modules installed, that are required, and found nothing in the error.log.
The error must be somewhere else.

Avatar
dailnow8448138225

Community Member, 1 Post

22 December 2014 at 5:36am

I am also facing the problem. If you know the answer please reply me.

Avatar
Fred Condo

Community Member, 29 Posts

23 December 2014 at 8:23am

Please post your PHP and template code somewhere where others can view it, such as http://sspaste.com or in a Github gist.