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

Custom Form Template - How to display submitted data


Go to End


6 Posts   2564 Views

Avatar
Jatinder

Community Member, 23 Posts

17 August 2010 at 6:25pm

Edited: 17/08/2010 6:26pm

Hello,

I have implemented a custom form with custom template. Everything works (email gets sent, data it stored in database).

But I can't figure out how to display the submitted data. How do I list or display the submitted data?

Please see below for my code:

RegisterPage.php

class RegisterPage extends Page {

    static $db = array(
    );

	static $has_many = array(
		"Submissions" => "RegisterInterestSubmission"
    );
}

class RegisterPage_Controller extends Page_Controller {

	function init() {
        parent::init();
    }
	
	function RegisterInterestForm() {
		return new RegisterForm($this, 'RegisterInterestForm');
	}
}

RegisterForm.php - My Custom Form

class RegisterForm extends Form {

	function __construct($controller, $name) {
		// Create fields
		$fields = new FieldSet(
			new TextField('FirstName', 'First Name'),
			new TextField('Surname', 'Surname'),
			new TextField('Email', 'Email')			
		);
	
		// Create actions
		$actions = new FieldSet(
			new FormAction('doSubmit', 'Submit')
		);
		
		//Validations
		$validator = new RequiredFields('FirstName', 'Surname', 'Email');
	
		//return new Form($this, $name, $fields, $actions, $validator);
		parent::__construct($controller, $name, $fields, $actions, $validator);
	}
	
	function doSubmit($data, $form) {
		$submission = new RegisterInterestSubmission();
		$form->saveInto($submission);
		$submission->write();
	}
	
	function forTemplate() {
		return $this->renderWith(array(
			$this->class,
			'Form'
		));
	}
}

RegisterInterestSubmission.php - Data object to store submissions

class RegisterInterestSubmission extends DataObject {
	static $db = array(
		'FirstName' => 'Text',
		'Surname' => 'Text',
		'Email' => 'Text'
	);
}

Avatar
Puppy

Community Member, 10 Posts

17 August 2010 at 9:10pm

Please note I have little experience in Silverstripe so there may be better options from the community.

From what I know I would suggest you would want to create a new page type which lists the data from the db/session/cookie and use a redirect on the submission page.

Director::redirect('thanks-for-your-registration/');

You could then display the data in an acceptable form.

Avatar
Jatinder

Community Member, 23 Posts

17 August 2010 at 9:49pm

Thanks for the response but I am try to displaying the submitted data on the admin side. That is, on the CMS.

Avatar
Bambii7

Community Member, 254 Posts

18 August 2010 at 2:46pm

Have you checked out the ModelAdmin class. If you make a class in mysite/code that extends model admin and add in the managed modules array
RegisterInterestSubmission
It should give you CRUD and a nice export to csv option ;)

Avatar
Jatinder

Community Member, 23 Posts

18 August 2010 at 3:46pm

Thank you for the information. But I am not trying to add or edit data here. I simply want to display the data already stored in the database.

I added a new tab "Submission" and then tried using a ComplexTableField to list the data from the data object. That works partially. I get the list of submissions just fine. But when I try to view a row's details, I get an error.

Any pointers or ideas?

Avatar
RoMay

Community Member, 1 Post

19 October 2012 at 11:52pm

Similar problem: no errors, but list is empty.
Any advice is appreciated!