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

Submit function is not called


Go to End


5 Posts   2168 Views

Avatar
Tread

Community Member, 8 Posts

24 March 2015 at 11:31pm

Edited: 24/03/2015 11:32pm

Hello Community,

I searched for an answere, but what I found, didn't solve my problem.
The problem is, that I have a form, but the responding action is not called, the page is just refreshed.
Here is my code:

	class InsertPage_Controller extends Page_Controller
	{
		private static $allowed_actions = array('doInsertCall', 'insertCall');
		
		public function insertCall($data = null, $form = null)
		{
			$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:', $this->getAgents())
					, new TextField('topic', 'Thema:')
			);
			$actions = new FieldList(
					new FormAction('doInsertCall', 'Eintragen')
			);
			return new Form($this, 'insertCall', $fields, $actions);
		}
		
		public function doInsertCall($data, $form)
		{
			$sub = new insertCallSubmission();
			$form->saveInto($sub);
			$sub->write();
			return $this->redirectBack();
		}
	}

I have to put this code in the insertCall function to write the data to the DB:

		public function insertCall($data = null, $form = null)
		{
			if ($data)
			{
				$x = $data->postVars();
				$submission = new insertCallSubmission();
				$submission->telefone = $x['telefone'];
				$submission->name = $x['name'];
				$submission->subject = $x['subject'];
				$submission->note = $x['note'];
				$submission->date = $x['date'];
				$submission->time = $x['time'];
				$submission->agent = $x['agent'];
				$submission->topic = $x['topic'];
				$submission->write();
			}
			$fields = new FieldList(
			...

Does anybody know where the problem is?

Avatar
wmk

Community Member, 87 Posts

24 March 2015 at 11:44pm

Hi Tread,

did you flush after you modified the $allowed_actions ?

Avatar
Tread

Community Member, 8 Posts

24 March 2015 at 11:47pm

Edited: 24/03/2015 11:48pm

Hello wmk,

flushed, /dev/bild, but to no avail.

Avatar
Tread

Community Member, 8 Posts

30 March 2015 at 11:59pm

Hello again,

I made a new install of SilverStripe, and it now works again.
Maybe something in the DB got mixed up.

Avatar
Pyromanik

Community Member, 419 Posts

31 March 2015 at 12:45am

I don't see anything wrong with the initial code, there must have been some manifest issue not present in the new install.
If in doubt you can always do a 'manual flush' as such by rm -rf silverstripe-cache (or otherwise applicable cache dir).

On a side note, as a couple of points of interest to help your learning:

  1. ClassName::Create($constructor, $args) is preferred over new Classname($constructor, $args) - the advantage here is dependency injection and the ability to chain methods after creation.
  2. $data is an array, so $data->postVars() should not work, unless the action is requested directly rather than via the form.
  3. Form actions take the parameters ($data, $form, $request), none of which are ever null, so it's not a good idea to supply default parameters (again, unless you're requesting it directly ie via GET, but that's getting a bit obfuscated).
  4. Even if $data->postVars() did work (it should be $request->postVars()), it is bad err... form. the Form will recieve the input, validate it, parse it, etc. ie. A DateField will give you valid date for the database [2015-02-01], rather than just a string in a random format [01/02/15] - just use the $data array directly. Keys are the field names, values the parsed data.
  5. A helper method that will save you a lot of time in your submission action is $form->saveInto($submission); - So long as the Model's field names match the form's field names, all will be populated for you.

    Hope this helps, man! :)