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.

Data Model Questions /

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

[SOLVED] Extending Controller with Extension


Go to End
Reply


2 Posts   807 Views

Avatar
Optic Blaze

Community Member, 173 Posts

24 April 2014 at 8:07am

Hi there,

I have a module that makes use of a DataExtention on the model and a Extension on the controller. I am not getting the controller side of things to work properly (the model part of the code works fine) My code looks like this:

SidebarEmail.php
-----------------------------------------------------------
class SidebarEmail extends DataExtension {

static $db = array(
'Enquire'=>'Boolean' // Gives user the option to enable or disable enquiry form in CMS
);

public function getCMSFields() {
$this->extend('updateCMSFields', $fields);
return $fields;
}

public function updateCMSFields(FieldList $fields) {
$enquiredropdown = array('0'=>'No','1'=>'Yes');
$fields->addFieldToTab('Root.Main', new DropDownField('Enquire', 'Enable Enquiry Form?', $enquiredropdown),'Title');
return $fields;
}

}

class SidebarEmail_Controller extends Extension {

public static $allowed_actions = array (
'EnquiryForm'
);

public function init() {parent::init(); }

// ENQUIRY FORM
public function EnquiryForm(){
$fields = new FieldList(
new TextField('Name', 'Your name & surname:'),
new TextField('Tel', 'Your phone number:'),
new EmailField('Email', 'Your email address:'),
new TextAreaField('Comments', 'Your query/comment:'),
new RecaptchaField('MyCaptcha')
);
//Form Action
$actions = new FieldList(
new FormAction('SendContactForm', 'Submit')
);
//Create Validators
$validator = new RequiredFields('Name', 'Email', 'Comments');
//Create form
$form = new Form($this, 'EnquiryForm', $fields, $actions, $validator);
return $form;

}

}

_config.php
----------------------------------------------------------------------------------------
SiteTree::add_extension('SidebarEmail');
SiteTree::add_extension('Page_Controller', 'SidebarEmail_Controller');
----------------------------------------------------------------------------------------

Avatar
Optic Blaze

Community Member, 173 Posts

25 April 2014 at 9:59pm

Ok got this to work with some help from Pynyxk on IRC. I did not reference the parent object. So once i added in the following:
$this->owner it worked...see below:
......

// ENQUIRY FORM
public function EnquiryForm(){
$fields = new FieldList(
new TextField('Name', 'Your name & surname:'),
new TextField('Tel', 'Your phone number:'),
new EmailField('Email', 'Your email address:'),
new TextAreaField('Comments', 'Your query/comment:'),
new RecaptchaField('MyCaptcha')
);
//Form Action
$actions = new FieldList(
new FormAction('SendContactForm', 'Submit')
);
//Create Validators
$validator = new RequiredFields('Name', 'Email', 'Comments');
//Create form
$form = new Form($this->owner, 'EnquiryForm', $fields, $actions, $validator);
return $form;
.....