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

Problem with Tutorial 3


Go to End


1276 Views

Avatar
hzj

Community Member, 1 Post

14 August 2013 at 2:22am

Edited: 14/08/2013 2:32am

Hi all

I am evaluating SS 3.0.5 and the tutorials. Now i have a Problem with tutorial 3 (http://doc.silverstripe.org/framework/en/tutorials/3-forms). Neither validation exception nor the redirectBack is working. Submitting with missing fields just Returns to the page without any message. And when submitting both fields, the result is not shown.

Here is my code from mysite/code/HomePage.php

<?php
class HomePage extends Page {

}



class HomePage_Controller extends Page_Controller {
   public function LatestNews($num=1) {
      $holder = ArticleHolder::get()->First();
      return ($holder) ? ArticlePage::get()->filter('ParentID', $holder->ID)->sort('Date DESC')->limit($num) : false;
   }
   
   static $allowed_actions = array('BrowserPollForm');

   public function BrowserPollForm() {
      if(Session::get('BrowserPollVoted') == true)
         return false;

      // Create fields
      $fields = new FieldList(
         new TextField('Name'),
         new OptionsetField('Browser', 'Your Favourite Browser', array(
                            'Firefox' => 'Firefox',
                            'Chrome' => 'Chrome',
                            'Internet Explorer' => 'Internet Explorer',
                            'Safari' => 'Safari',
                            'Opera' => 'Opera',
                            'Lynx' => 'Lynx'
         ))
      );
         
      // Create actions
      $actions = new FieldList(
         new FormAction('doBrowserPoll', 'Submit')
      );

      $validator = new RequiredFields('Name', 'Browser');
      return new Form($this, 'BrowserPollForm', $fields, $actions, $validator);
   }
    
   public function doBrowserPoll($data, $form) {
      $submission = new BrowserPollSubmission();
      $form->saveInto($submission);
      $submission->write();

      Session::set('BrowserPollVoted', true);

      return $this->redirectBack();
   }


   public function BrowserPollResults() {
      $submissions = new GroupedList(BrowserPollSubmission::get());
      $total = $submissions->Count();

      $list = new ArrayList();
      foreach($submissions->groupBy('Browser') as $browserName => $browserSubmissions) {
         $percentage = (int) ($browserSubmissions->Count() / $total * 100);
         $list->push(new ArrayData(array(
                           'Browser' => $browserName,
                           'Percentage' => $percentage
                      ))
                    );
      }
      return $list;
   }      
}

I hope somebody may help me.
Tia
Hans

Nota
using return $this->redirect(""); would make the submit work. but what's the difference?