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.

Form Questions

Custom Form Template - How to display submitted data


Reply

6 Posts   1586 Views

Avatar
Jatinder

17 August 2010 at 6:25pm (Last edited: 17 August 2010 6:26pm), Community Member, 23 Posts

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

17 August 2010 at 9:10pm Community Member, 10 Posts

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

17 August 2010 at 9:49pm Community Member, 23 Posts

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

Avatar
Bambii7

18 August 2010 at 2:46pm Community Member, 254 Posts

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

18 August 2010 at 3:46pm Community Member, 23 Posts

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

19 October 2012 at 11:52pm Community Member, 1 Post

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