Jump to:

1969 Posts in 1272 Topics by 606 members

Form Questions

SilverStripe Forums » Form Questions » Validation and redirectback not working

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

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

    Validation and redirectback not working Link to this post

    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.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Validation and redirectback not working Link to this post

    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.

  • Mauro74
    Avatar
    Community Member
    30 Posts

    Re: Validation and redirectback not working Link to this post

    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!

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