21489 Posts in 5783 Topics by 2621 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 572 Views |
-
Forms tutorial validation and redirect not working

12 July 2011 at 1:55am
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. -
Re: Forms tutorial validation and redirect not working

12 July 2011 at 6:38pm
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);
-
Re: Forms tutorial validation and redirect not working

2 August 2011 at 1:24am
But what about the redirection part? It's still not working.
-
Re: Forms tutorial validation and redirect not working

2 August 2011 at 1:47am
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();
}
}
| 572 Views | ||
|
Page:
1
|
Go to Top |



