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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Form Questions /

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

Creating a custom form template from ContactPage.php


Go to End


3 Posts   4509 Views

Avatar
Samba Sam

Community Member, 85 Posts

9 March 2010 at 8:11am

Edited: 09/03/2010 8:13am

Hi,
I would like to convert the below code, adapted from a SSbits tutorial, into a format that allows me to specify my own custom form template as demonstrated here http://doc.silverstripe.org/doku.php?id=form#using_a_custom_template).

I am not entirely clear on what goes in the Myforms.php and what does in the ContactPage.php.

Any help would be greatly appreciated,
Sam

<?php

class ContactPage extends Page
{
	static $db = array(
		'Mailto' => 'Varchar(100)', //Email address to send submissions to
		'SubmitText' => 'HTMLText' //Text presented after submitting message
	);
	
	//CMS fields
	function getCMSFields() {
		$fields = parent::getCMSFields();
	
		$fields->addFieldToTab("Root.Content.OnSubmission", new TextField('Mailto', 'Email submissions to'));	
		$fields->addFieldToTab("Root.Content.OnSubmission", new HTMLEditorField('SubmitText', 'Text on Submission'));	
	
		return $fields;	
	}

}
/* *****************
 *  Controller
 ******************/
class ContactPage_Controller extends Page_Controller
{
	function ContactForm() {
      	// Create fields
		$Params = Director::urlParams();
		  
	    $fields = new FieldSet(
		    new TextField('Name', 'Name*'),
			new EmailField('Email', 'Email*'),
			new TextareaField('Comments','Comments*')
		);
	 	
	    // Create action
	    $actions = new FieldSet(
	    	new FormAction('SendContactForm', 'Send')
	    );
		// Create action
		$validator = new RequiredFields('Name', 'Email', 'Comments');
	
	  	$form = new Form($this, 'ContactForm', $fields, $actions, $validator);
		$protector = SpamProtectorManager::update_form($form, 'Captcha');
      	if($protector) $protector->setFieldMapping('Name', 'Email','Comments');
		return $form;
	}
	

 
	function SendContactForm($data) {
      
	 	//Set data
		$From = $data['Email'];
		$To = $this->Mailto;
		$Subject = "Website Contact message";  	  
		$email = new Email($From, $To, $Subject);
		//set template
		$email->setTemplate('ContactEmail');
		//populate template
		$email->populateTemplate($data);
		//send mail
		$email->send();
	  	//return to submitted message
		Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1");

	}

	public function Success()
	{
		return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
	}
?>

Avatar
Samba Sam

Community Member, 85 Posts

10 March 2010 at 4:13pm

Edited: 10/03/2010 4:13pm

So I'm almost there. But I can't get mollom to work with my custom form class. I get an error if I uncomment the line -- $form = new Form($this, '__construct', etc., etc. -- in the ContactForm.php. This is the code that i've seen mentioned in the forum. But it doesn't appear to work with custom form classes.

Any help is appreciated!
Sam

ContactForm.php:

<?php
class ContactForm extends Form {
 
   function __construct($controller, $name) {
       $Params = Director::urlParams();
       $fields = new FieldSet(
		new TextField('Name', 'Name*'),
		new EmailField('Email', 'Email*'),
		new TextareaField('Comments','Comments*'));
         
        $actions = new FieldSet(
                new FormAction("SendContactForm", "Submit"));
            
       $requiredFields = new RequiredFields('Name', 'Email', 'Comments');
            
 /*   $form = new Form($this, '__construct', $fields, $actions,$requiredFields);
	$protector = SpamProtectorManager::update_form($form, 'Captcha');
	if($protector) $protector->setFieldMapping('Name', 'Email','Comments');
	return $form;  */
     
	parent::__construct($controller, $name, $fields, $actions,$requiredFields);
   
} // END __construct
 
   function forTemplate() {
      return $this->renderWith(array(
         $this->class,
         'Form'
      ));
   }
}

Avatar
biapar

Forum Moderator, 435 Posts

16 October 2010 at 9:47pm