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!