At each web site, contact page (Contact Us) is one of the pages needed for site visitors to contact/comment to the creator's web. From the business side of this page must be made to accommodate the client desires. From this page also a business transaction can be starts.
In silverstripe, we can create a contact page containing a form to be filled by the customer. Every customer who wants to contact or provide a message to the website must fill out a message on this form. Later messages are stored into the database or if it allows such messages will be immediately sent to the email the site creator. Page contact to be made in this post has a form that contains the field names, email, phone and message.
Silverstripe has provide facilities page comments form, but is it a standard form. If we want another form we have to make our own. The way is easy and fast just like when we create pages in Creating New Page Type.
This time we will create a new page type called ContactPage in which there will be a form called ContactForm. For that we need to create a page that is ContactPage and ContactForm. And also there will be a data object used to store data-entry in database named Contact.
ContactForm
Create a file ContactForm.php in the directory mysite/code/
mysite/code/ContactForm.php
<?
class ContactForm extends Form {
function __construct($controller, $name) {
$fields = new FieldSet(
new TextField("name", "Name"),
new TextField("Phone", "Phone"),
new EmailField("email", "E-mail"),
new TextareaField ("message","Message")
);
$actions = new FieldSet(new FormAction("sendContact", "Send"));
$validator = new RequiredFields("name", "email", "message");
parent::__construct($controller, $name, $fields, $actions, $validator);
}
function forTemplate() {
return $this->renderWith(array(
$this->class,
'Form'
));
}
function sendContact($data, $form) {
}
}
?>
ContactForm is a class derived from Form. Where the content of the initial fields of the form that will be made. The file also contains the action of ContactForm if the form submitted. Action named sendContact on ContactForm created using the ContactForm Class method. In this case action is empty. This meant that we could customize the action later.
View for ContactForm will have stored on themes/themes1/templates/Includes directory. For that we need to create a file in that directory ContactForm.ss.
themes/sslearn/templates/Includes/ContactForm.ss
<% if IncludeFormTag %>
<form $FormAttributes>
<% end_if %>
<% if Message %>
<p id="{$FormName}_error" class="error $MessageType">$Message</p>
<% else %>
<p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
<% end_if %>
<fieldset class="form">
<legend>$Legend</legend>
<% control Fields %>
<label><span>$title</span> $Field</label>
<% end_control %>
<div class="clear"><!-- --></div>
</fieldset>
<% if Actions %>
<div class="send">
<% control Actions %>
$Field
<% end_control %>
</div>
<% end_if %>
<% if IncludeFormTag %>
</form>
<% end_if %>
ContactPage
Create a file ContactPage.php in the directory mysite/code/
mysite/code/ContactPage.php
<?php
class ContactPage extends Page{
public function getCMSFields() {
$fields = parent::getCMSFields();
$contactEntry = new TableListField("contactEntry", "Contact", array("name"=>"Name", "phone"=>"Phone", "email"=>"Email","message"=>"Message"));
$fields->addFieldToTab("Root.Content.Contact", $contactEntry);
return $fields;
}
}
class ContactPage_Controller extends Page_Controller {
function ContactForm(){
if(Session::get('ContactFormEntry')) {
Session::clear('ContactFormEntry');
return "Your message has been sent";
}
$form = new ContactForm($this, "ContactForm");
return $form;
}
function sendContact($data,$form) {
Session::set('ContactFormEntry', true);
$Contact = new Contact;
$form->saveInto($Contact);
$Contact->write();
Director::redirectBack();
}
}
?>
ContactPage is a type of page that will be used to display ContactForm. ContactPage controller has a method to display ContactForm. From controller method, ContactPage also containing sendContact controller commands of the form. In sendContact method, data storage will be made into the data object Contact. For that we'll also have to create data objects Contact. Then after contact has been saved to database we can return to the previous page with instructions Director::redirectBack().
In the file ContactPage.php we see the contents getCMSField method more or less as follows:
public function getCMSFields() {
$fields = parent::getCMSFields();
$contactEntry = new TableListField("contactEntry", "Contact", array("name"=>"Name", "phone"=>"Phone", "email"=>"Email","message"=>"Message"));
$fields->addFieldToTab("Root.Content.Contact", $contactEntry);
return $fields;
}
In this method, we use an object TableListField to be used to display the contents of the Contact data objects that are used to store data from ContactForm entry.
After creating the file we have to make ContactPage.php view the contents ContactPage.ss approximately as follows:
themes/sslearn/templates/ContactPage.ss
<html>
<head>
<title>Themes Silverstripe 1</title>
</head>
<body>
<ul>
<% control Menu(1) %>
<li> <a href="$Link">$MenuTitle</a></li>
<% end_control %>
</ul>
<div>
<h1>$Title</h1>
$Content
$ContactForm
</div>
</body>
</html>
Contact Data Object
Contact data object used to store data entry from ContactForm action. This data object that will represent the database. For making this Contact data object we need to make a contact.php file whose contents form a class named Contact derived from the DataObject class. These files are stored in the directory mysite / code / with following contents:
<?php
class Contact extends DataObject{
static $db = array(
'name' => 'Varchar',
'phone' => 'Varchar',
'email' => 'Varchar',
'message' => 'Text'
);
}
?>
In the Contact class will have an array variable named $db. The contents of this variable is array containing field definition in the database that will be used to store data in the entry on ContactForm we have previously made. The name of his field-adjusted to the form that was created.
The last stage before we use this contact form is to flush the database to build a database and silverstripe form and page design that we make. Way by typing ttp://localhost/sslearn/db/build/?flush=1 in your browser.
Next ContactPage page type will appear in the combo box when we will add a new page in the admin page of our Silverstripe CMS.

And also the contact tab will appear on the form on the contact page CMS to see anyone who has entry our contact data. This tab will contain the contact tablelistfield we have previously defined.

The way is easy enough. Please try by yourself, if there is a problem please give comments.
Cant seem to get this working right :(
great article.
I need one help.if I want to give separate TextFiled line by line what i have to do ?
Name
Phone
email
message like this.
Hi, I new bee this sliverstripe.I did form implementation as it is but i am getting this error.Fatal error: Class 'ContactForm' not found in C:\wamp\www\tstart\mysite\code\ContactPage.php on line 20
plz help.
Thank you very much!
This tutorial helped me understand the original SilverStripe about customizing form templates.
Thank you very much!
Great article. Looks like a typo in:
new TextField("Phone", "Phone"),
That first "Phone" should be "phone".
One issue- when you create multiple contact pages they all share the same data, that is, Contact page 2 will display the same data collected from Contact page 1. How can you make the Contact pages unique?