Skip to content
This repository
Browse code

MINOR: Forms, navigation howto plus adjustments to tutorial one (#6367 )

* Created "How to make a simple contact form"
* Created "Create a navigation menu"
* Adjusted tutorial one from Andy's notes
  • Loading branch information...
commit 6d8976ec87669d55c66dce81fab50567633daaeb 1 parent 11c71e1
Naomi Guyer authored July 19, 2012 chillu committed August 07, 2012
BIN  docs/en/_images/howto_contactForm.jpg
59  docs/en/howto/navigation-menu.md
Source Rendered
... ...
@@ -0,0 +1,59 @@
  1
+#How to create a navigation menu
  2
+
  3
+To create a navigation menu, we will create a new template file: Navigation.ss. Put this file inside the templates/Includes folder in your theme.
  4
+
  5
+    :::ss
  6
+    <ul> 
  7
+    	<% loop Menu(1) %>	  
  8
+    		<li>
  9
+          <a href="$Link" title="Go to the $Title page" class="$LinkingMode">
  10
+           <span>$MenuTitle</span>
  11
+          </a>
  12
+        </li> 
  13
+     	<% end_loop %> 
  14
+    </ul>
  15
+
  16
+To include this file in your main template, use the 'include' control code. The include control code will insert a template from the Includes folder into your template. The code for including our navigation menu looks like this:
  17
+
  18
+    :::ss
  19
+    <% include Navigation %>
  20
+
  21
+Add this to the templates/Page.ss file where you want the menu to render. The template code in Menu1.ss is rendered as an unordered list in HTML; let's break down this file to see how this works.
  22
+
  23
+The first and last lines of the file are HTML tags to open and close an unordered list. 
  24
+
  25
+    :::ss
  26
+    <ul> 
  27
+    	<% loop Menu(1) %>	  
  28
+    		<li>
  29
+                <a href="$Link" title="Go to the $Title page" class="$LinkingMode">
  30
+                    <span>$MenuTitle</span>
  31
+                </a>
  32
+            </li> 
  33
+        <% end_loop %> 
  34
+    </ul>
  35
+
  36
+Line 2 and 4 use a template code called a loop. A loop iterates over a DataObjectSet; for each DataObject inside the set, everything between the <% loop %> and <% end_loop %> tags are repeated. Here we iterate over the Menu(1) DataObjectSet and this returns the set of all pages at the top level. (For a list of other controls you can use in your templates, see the [templates page](../reference/templates) .)
  37
+
  38
+    :::ss
  39
+    <ul> 
  40
+     	<% loop Menu(1) %>	  
  41
+      		<li>
  42
+                <a href="$Link" title="Go to the $Title page" class="$LinkingMode">
  43
+                    <span>$MenuTitle</span>
  44
+                </a>
  45
+            </li> 
  46
+       	<% end_loop %> 
  47
+    </ul>
  48
+
  49
+Line 3 is where we insert the list item for each menu item. It is sandwiched by the list item opening and closing tags, <li> and </li>. Inside we have a link, using some template codes to fill in the information for each page:
  50
+
  51
+* $Link – the link to the page  
  52
+* $Title – the full title of the page (this is a field in the CMS)  
  53
+* $MenuTitle – the menu title of the page (this is a field in the CMS)  
  54
+* $LinkingMode – which returns one of three things used as a CSS class to style each scenario differently.  
  55
+    * current – this is the page that is currently being rendered
  56
+    * section – this page is a child of the page currently being rendered
  57
+    * link – this page is neither current nor section  
  58
+
  59
+
116  docs/en/howto/simple-contact-form.md
Source Rendered
... ...
@@ -0,0 +1,116 @@
  1
+# How to make a simple contact form
  2
+
  3
+To make a contact form, begin by making a page type for the form to live on – we don't want to see the contact form on every page. Here we have the skeleton code for a ContactPage page type:
  4
+
  5
+	:::php
  6
+	<?php
  7
+	class ContactPage extends Page {
  8
+		static $db = array(
  9
+		);
  10
+	}
  11
+	class ContactPage_Controller extends Page_Controller {
  12
+
  13
+	}
  14
+
  15
+To create a form, we create a Form object on a function on our page controller. We'll call this function 'Form()' on our ContactPage_Controller class. It doesn't matter what you call your function, but it's standard practice to name the function Form() if there's only a single form on the page. Below is the function to create our contact form:
  16
+
  17
+	:::php
  18
+	function Form() { 
  19
+		$fields = new FieldSet( 
  20
+			new TextField('Name'), 
  21
+			new EmailField('Email'), 
  22
+			new TextareaField('Message')
  23
+		); 
  24
+		 
  25
+		$actions = new FieldSet( 
  26
+			new FormAction('submit', 'Submit') 
  27
+		); 
  28
+		 
  29
+		return new Form($this, 'Form', $fields, $actions); 
  30
+	}
  31
+
  32
+There's quite a bit in this function, so we'll step through one piece at a time.
  33
+
  34
+	:::php
  35
+	$fields = new FieldSet(
  36
+		new TextField('Name'),
  37
+		new EmailField('Email'),
  38
+		new TextareaField('Message')
  39
+	);
  40
+
  41
+First we create all the fields we want in the contact form, and put them inside a FieldSet. You can find a list of form fields available on the `[api:FormField]` page.   
  42
+
  43
+	:::php
  44
+	$actions = FieldSet(
  45
+		new FormAction('submit', 'Submit')
  46
+	);
  47
+
  48
+We then create a `[api:FieldSet]` of the form actions, or the buttons that submit the form. Here we add a single form action, with the name 'submit', and the label 'Submit'. We'll use the name of the form action later.
  49
+
  50
+	:::php
  51
+	return new Form('Form', $this, $fields, $actions);
  52
+
  53
+Finally we create the Form object and return it. The first argument is the name of the form – this has to be the same as the name of the function that creates the form, so we've used 'Form'. The second argument is the controller that the form is on – this is almost always $this. The third and fourth arguments are the fields and actions we created earlier.
  54
+
  55
+To show the form on the page, we need to render it in our template. We do this by appending $ to the name of the form – so for the form we just created we need to add $Form. Add $Form to the themes/currenttheme/Layout/Page.ss template, below $Content.
  56
+
  57
+The reason it's standard practice to name the form function 'Form' is so that we don't have to create a separate template for each page with a form. By adding $Form to the generic Page.ss template, all pages with a form named 'Form' will have their forms shown.
  58
+
  59
+If you now create a ContactPage in the CMS (making sure you have rebuilt the database and flushed the templates /dev/build?flush=all) and visit the page, you will now see a contact form.
  60
+
  61
+![](../_images/howto_contactForm.jpg)
  62
+
  63
+
  64
+Now that we have a contact form, we need some way of collecting the data submitted. We do this by creating a function on the controller with the same name as the form action. In this case, we create the function 'submit' on the ContactPage_Controller class.
  65
+
  66
+	:::php
  67
+	function submit($data, $form) { 
  68
+		$email = new Email(); 
  69
+		 
  70
+		$email->setTo('siteowner@mysite.com'); 
  71
+		$email->setFrom($data['Email']); 
  72
+		$email->setSubject("Contact Message from {$data["Name"]}"); 
  73
+		 
  74
+		$messageBody = " 
  75
+			<p><strong>Name:</strong> {$data['Name']}</p> 
  76
+			<p><strong>Website:</strong> {$data['Website']}</p> 
  77
+			<p><strong>Message:</strong> {$data['Message']}</p> 
  78
+		"; 
  79
+		$email->setBody($messageBody); 
  80
+		$email->send(); 
  81
+		return array(
  82
+			'Content' => '<p>Thank you for your feedback.</p>',
  83
+			'Form' => ''
  84
+		);
  85
+	}
  86
+
  87
+Any function that receives a form submission takes two arguments: the data passed to the form as an indexed array, and the form itself. In order to extract the data, you can either use functions on the form object to get the fields and query their values, or just use the raw data in the array. In the example above, we used the array, as it's the easiest way to get data without requiring the form fields to perform any special transformations.
  88
+
  89
+This data is used to create an email, which you then send to the address you choose.
  90
+
  91
+The final thing we do is return a 'thank you for your feedback' message to the user. To do this we override some of the methods called in the template by returning an array. We return the HTML content we want rendered instead of the usual CMS-entered content, and we return false for Form, as we don't want the form to render.
  92
+
  93
+
  94
+##How to add form validation
  95
+
  96
+All forms have some basic validation built in – email fields will only let the user enter email addresses, number fields will only accept numbers, and so on. Sometimes you need more complicated validation, so you can define your own validation by extending the Validator class.
  97
+
  98
+The Sapphire framework comes with a predefined validator called 'RequiredFields', which performs the common task of making sure particular fields are filled out. Below is the code to add validation to a contact form:
  99
+
  100
+	function Form() { 
  101
+		$fields = new FieldSet( 
  102
+			new TextField('Name'), 
  103
+			new EmailField('Email'), 
  104
+			new TextareaField('Message')
  105
+		); 
  106
+		 
  107
+		$actions = new FieldSet( 
  108
+			new FormAction('submit', 'Submit') 
  109
+		); 
  110
+		$validator = new RequiredFields('Name', 'Message');
  111
+		 
  112
+		return new Form($this, 'Form', $fields, $actions, $validator); 
  113
+	}
  114
+
  115
+We've created a RequiredFields object, passing the name of the fields we want to be required. The validator we have created is then passed as the fifth argument of the form constructor. If we now try to submit the form without filling out the required fields, JavaScript validation will kick in, and the user will be presented with a message about the missing fields. If the user has JavaScript disabled, PHP validation will kick in when the form is submitted, and the user will be redirected back to the Form with messages about their missing fields.
  116
+
3  docs/en/tutorials/1-building-a-basic-site.md
Source Rendered
@@ -97,8 +97,7 @@ You should ensure the URL for the home page is *home*, as that's the page Silver
97 97
 ## Templates
98 98
 
99 99
 All pages on a SilverStripe site are rendered using a template. A template is an file 
100  
-with a special `*.ss` file extension, containing HTML augmented with some
101  
-control codes. Because of this, you can have as much control of your site’s HTML code as you like.
  100
+with a special `*.ss` file extension, containing HTML augmented with some control codes.  Through the use of templates, you can have as much control over your site’s HTML code as you like. In SilverStripe, these files and others for controlling your sites appearance – the CSS, images, and some javascript – are collectively described as a theme. Themes live in the 'themes' folder of your site.
102 101
 
103 102
 Every page in your site has a **page type**. We will briefly talk about page types later, and go into much more detail
104 103
 in tutorial two; right now all our pages will be of the page type *Page*. When rendering a page, SilverStripe will look

0 notes on commit 6d8976e

Please sign in to comment.
Something went wrong with that request. Please try again.