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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Forms tutorial validation and redirect not working


Reply

4 Posts   871 Views

Avatar
Mauro74

12 July 2011 at 1:55am Community Member, 30 Posts

Hi All, I'm doing diligently all the tutorials to fully understand the potential of SilverStripe. Now I'm stuck at tutorial 3 the one about the forms.
Basically the poll form works (I got the data in the database and I can see the poll results) except for the validation and the redirectback. here's my code:

class HomePage extends Page {
static $db = array(
);
static $has_one = array(
);
static $icon = "themes/tutorial/images/treeicons/home";
}

class HomePage_Controller extends Page_Controller {
function LatestNews($num=5) {
$news = DataObject::get_one("ArticleHolder");
return ($news) ? DataObject::get("ArticlePage", "ParentID = $news->ID", "Date DESC", "", $num) : false;
}

function BrowserPollForm() {

if(Session::get('BrowserPollVoted')) {
return false;
}

// Create fields
$fields = new FieldSet(
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 FieldSet(
new FormAction('doBrowserPoll', 'Submit')
);

// Create validator
$validator = new RequiredFields('Name', 'Browser');

return new Form($this, 'BrowserPollForm', $fields, $actions);
}

function doBrowserPoll($data, $form) {
$submission = new BrowserPollSubmission();
$form->saveInto($submission);
$submission->write();

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

Director::redirectBack();
}

function BrowserPollResults() {
$submissions = DataObject::get('BrowserPollSubmission');
$total = $submissions->Count();

$doSet = new DataObjectSet();
foreach($submissions->groupBy('Browser') as $browser => $data) {
$percentage = (int) ($data->Count() / $total * 100);
$record = array(
'Browser' => $browser,
'Percentage' => $percentage
);
$doSet->push(new ArrayData($record));
}

return $doSet;
}

Now if I use the redirect in this way: redirect('index.php'); it works but the redirectback() just doesn't work, after the form submission I see a blank page (the one where the function lives). The validation just doesn't kick in, like if it wasn't there. What am I doing wrong?
Thanks.

Avatar
Willr

12 July 2011 at 6:38pm Forum Moderator, 5511 Posts

return new Form($this, 'BrowserPollForm', $fields, $actions);

You're missing passing the validator into that. It should be like

return new Form($this, 'BrowserPollForm', $fields, $actions, $validator);

Avatar
emp4180

2 August 2011 at 1:24am Community Member, 5 Posts

But what about the redirection part? It's still not working.

Avatar
emp4180

2 August 2011 at 1:47am Community Member, 5 Posts

Ok I tweaked my code and got it to work. Here's my HomePage.php following tutorial 2.

<?php
/**
* Defines the HomePage page type
*/

class HomePage extends Page {
static $db = array(
);
static $has_one = array(
);
static $icon = "themes/tutorial/images/treeicons/home";
}

class HomePage_Controller extends Page_Controller {

function LatestNews($num=5) {
$news = DataObject::get_one("ArticleHolder");
return ($news) ? DataObject::get("ArticlePage", "ParentID = $news->ID", "Date DESC", "", $num) : false;
}
   
function BrowserPollForm() {
// Create fields
$fields = new FieldSet(
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 FieldSet(
new FormAction('doBrowserPoll', 'Submit')
);
      
      if(Session::get('BrowserPollVoted')) {
         return false;
      }else {
         $validator = new RequiredFields('Name', 'Browser');      
         return new Form($this, 'BrowserPollForm', $fields, $actions,$validator);         
      }
      
}
   
   function BrowserPollResults() {
      $submissions = DataObject::get('BrowserPollSubmission');
      $total = $submissions->Count();
      
      $doSet = new DataObjectSet();
      foreach($submissions->groupBy('Browser') as $browser => $data) {
         $percentage = (int) ($data->Count() / $total * 100);
         $record = array(
            'Browser' => $browser,
            'Percentage' => $percentage
         );
         $doSet->push(new ArrayData($record));
      }
      
      return $doSet;
   }

   function doBrowserPoll($data, $form) {
$submission = new BrowserPollSubmission();
$form->saveInto($submission);
$submission->write();
      
      Session::set('BrowserPollVoted', true);
      
Director::redirectBack();
}   
}