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.

Widgets /

Discuss SilverStripe Widgets.

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

Contact form widget - Posting problems


Reply


3 Posts   3451 Views

Avatar
Thomashv

Community Member, 33 Posts

25 July 2009 at 5:32am

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!

Avatar
teejay

Community Member, 63 Posts

3 February 2010 at 9:23am

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

Avatar
Heike-san

Community Member, 52 Posts

14 February 2010 at 3:36am

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 ?