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

Validation and redirectback not working


Reply

3 Posts   1088 Views

Avatar
Mauro74

12 July 2011 at 7:08am Community Member, 30 Posts

Hi guys,
I'm really desperate! I'm new to SilversStripe and I've just completed all the tutorials. In the forms tutorial the form itself works, I mean the data are submitted to the database correctly. The thing that I don't understand is why the validation and the redirectback is not working. The html and the javascript outputted in the page for the validation seems alright to me:

<form id="Form_BrowserPollForm" action="/SilverStripe/index.php/home/BrowserPollForm" method="post" enctype="application/x-www-form-urlencoded">
...
<input type="text" class="text" id="Form_BrowserPollForm_Name" name="Name" value="" />
...
</form>

...
#Form_BrowserPollForm input' : {
      initialise: function() {
         if(!this.old_onblur) this.old_onblur = function() { return true; }
         if(!this.old_onfocus) this.old_onfocus = function() { return true; }
      },
      onblur : function() {
         if(this.old_onblur()) {
            // Don't perform instant validation for CalendarDateField fields; it creates usability wierdness.
            if(this.parentNode.className.indexOf('calendardate') == -1 || this.value) {
               return $('Form_BrowserPollForm').validate(this);
            } else {
               return true;
            }
         }
      }
   }
...

and that's the code in the HomePage.php:

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;
}
}

As per the redirectback() it's just not working. When I submit the form I see the blank page with the name of the function been called at the end of the url. If I use the redirect like this: redirect('index.php') it works fine.

The code is taken form the tutorial as it is, so it's supposed to work. I'm using OS X 10.6.8, PHP 5.3.4 and Apache as webserver.
Thanks in advance.

Avatar
swaiba

12 July 2011 at 9:12am Forum Moderator, 1796 Posts

Hi,

I don't know why it isn't working, but how about...

Director::redirect($this->AbsoluteLink().'?submitted='.$submission->ID);

instead of...

Director::redirectBack();

then you could do something differnt with the page if "submitted" is posted, or even better use a URLParam action / id to pass the information and control the redirect.

Avatar
Mauro74

12 July 2011 at 9:49am Community Member, 30 Posts

Hi Swaiba, once again it was just me not paying much attention to the code: in the "return new Form($this, 'BrowserPollForm', $fields, $actions, $validator);" I had wrongly copied from the tutorial omitting the $validator parameter. Since I corrected the code the redirectback works fine! :)