Jump to:

2012 Posts in 1438 Topics by 621 members

Form Questions

SilverStripe Forums » Form Questions » Problem with Tutorial 3

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

Page: 1
Go to End
Author Topic: 409 Views
  • hzj
    Avatar
    Community Member
    1 Post

    Problem with Tutorial 3 Link to this post

    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?

    409 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.