Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //MyForm Sub Class, file name: MyForm.php
- class MyForm extends Form {
- function __construct($controller, $name) {
- $fields = new FieldSet(
- new TextField('FirstName', 'First name'),
- new EmailField('Email', 'Email address')
- );
- $actions = new FieldSet(
- new FormAction('submit', 'Submit')
- );
- $validator = new RequiredFields('FirstName', 'Email');
- parent::__construct($controller, $name, $fields, $actions, $validator);
- }
- function forTemplate() {
- return $this->renderWith(array(
- $this->class,
- 'Form'
- ));
- }
- function submit($data, $form) {
- // do stuff here
- $submission = new MyFormSubmission();
- $form->saveInto($submission);
- $submission->write();
- Director::redirect('/home/');
- }
- }
- ?>
- //MyForm.ss
- <form $FormAttributes>
- <% if Message %>
- <p id="{$FormName}_error" class="message $MessageType">$Message</p>
- <% else %>
- <p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
- <% end_if %>
- <fieldset>
- <div id="FirstName" class="field text">
- <label class="left" for="{$FormName}_FirstName">First name</label><br>
- $dataFieldByName(FirstName)
- </div>
- <div id="Email" class="field email">
- <label class="left" for="{$FormName}_Email">Email :</label><br>
- $dataFieldByName(Email)
- </div>
- $dataFieldByName(SecurityID)
- </fieldset>
- <% if Actions %>
- <div class="Actions">
- <% control Actions %>$Field<% end_control %>
- </div>
- <% end_if %>
- </form>
- //cotroller insider HomePage.php
- class HomePage_Controller extends Page_Controller {
- function MyFirstForm(){
- return new MyForm($this, 'MyFirstForm');
- }
- }
- <?php
- class MyFormSubmission extends DataObject {
- static $db = array(
- 'FirstName' => 'Varchar(255)',
- 'Email' => 'Text'
- );
- }
- ?>
Add Comment
Please, Sign In to add comment