Jump to:

791 Posts in 369 Topics by 303 members

Widgets

SilverStripe Forums » Widgets » Contact form widget - Posting problems

Discuss SilverStripe Widgets.

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

Page: 1
Go to End
Author Topic: 3318 Views
  • Thomashv
    Avatar
    Community Member
    32 Posts

    Contact form widget - Posting problems Link to this post

    I have made a contact form based on this document: http://doc.silverstripe.com/doku.php?id=widgets:forms&s=widget%20form.

    I have this code in the file ContactWidget:

    <?php

    class ContactWidget extends Widget {
       static $db = array(
          "Text" => "Varchar"
       );
       
       static $title = "Kontakt oss";
       static $cmsTitle = "Contact";
       static $description = "Contact form in sidebar.";
       
       function Form() {
          $controller = new ContactWidget_Controller($this);
          return $controller->Form();
       }

       function getCMSFields() {
          return new FieldSet(
             new TextField("Text", "Tekst")
          );
       }
    }

    class ContactWidget_Controller extends Controller {
       protected $widget;
       
       function __construct($widget = null) {
          if($widget) $this->widget = $widget;
       }
       function widget() {
          if($this->widget) return $this->widget;
          else if(is_numeric($this->urlParams['ID'])) return $this->widget = DataObject::get_by_id('Widget', $this->urlParams['ID']);
          else user_error('No widget selected', E_USER_ERROR);
       }
       function Link() {
          return $this->class;
       }
       
       function Form() {
             // ... This can be whatever form you like ...
             $widgetform = new WidgetForm($this, 'Form', new FieldSet(
             new DropdownField('Browser', 'Hva er du interissert i?', array(
    'Annonsestyring' => 'Annonsestyring',
    'Publiseringsløsning' => 'Publiseringsløsning')),
             new TextField('Name', 'Navn'),
       new TextField('Email', 'E-post')),
             new FieldSet(new FormAction('doAction', 'Submit')));
             
             $widgetform->setWidget($this->widget);
             return $widgetform;
          }
       
       function doAction($data, $form) {
          // ... Do your thing, just like a normal SilverStripe form

          // ... This is a good way of giving feedback to the user about the submission. A message will be shown above the form.

          $this->Form()->sessionMessage("Thanks for submitting my form", "good");
          Director::redirectBack();
       }
    }

    class WidgetForm extends Form {
       protected $widget;
       
       static $url_handlers = array(
          'POST $WidgetID' => 'httpSubmission',
          'GET $WidgetID' => 'httpSubmission'
       );
       
       function FormAction(){
          if($this->widget){
             return parent::FormAction()."/".$this->widget->ID;
          }
          return parent::FormAction();
       }
       
       function setWidget($wgt){
          if($wgt != null){
             $this->widget = $wgt;
          }
       }
       
       function getWidget(){
          return $this->widget;
       }   
    }

    ?>

    The form is displayed as it should in the sidebar, but when I submit the form I get this error message:

    Website Error
    There has been an error

    The website server has not been able to respond to your request.

    I suppose the form should be posted on the same page that it's displayed, but it is posting to http://www.mysite.no/Form/8. I just can't see what I have done wrong. Hope some some of you experts out there can see it!? Then I will be really grateful!

  • teejay
    Avatar
    Community Member
    63 Posts

    Re: Contact form widget - Posting problems Link to this post

    Did u solve ur issue ? because I have the same issue !

  • Heike-san
    Avatar
    Community Member
    52 Posts

    Re: Contact form widget - Posting problems Link to this post

    I also have the same problem, by editing the function FormAction in WidgetForm.php you might modify where the action is redirected.
    However, I can't get the doAction function to work, it seems that it never get's there...
    Can someone help ?

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