Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Forms tutorial validation and redirect not working

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

Page: 1
Go to End
Author Topic: 860 Views
  • Mauro74
    Avatar
    Community Member
    30 Posts

    Forms tutorial validation and redirect not working Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Forms tutorial validation and redirect not working Link to this post

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

  • emp4180
    Avatar
    Community Member
    5 Posts

    Re: Forms tutorial validation and redirect not working Link to this post

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

  • emp4180
    Avatar
    Community Member
    5 Posts

    Re: Forms tutorial validation and redirect not working Link to this post

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

    860 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.