Guest User

Untitled

a guest
Jun 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. <?php
  2. //MyForm Sub Class, file name: MyForm.php
  3. class MyForm extends Form {
  4.  
  5. function __construct($controller, $name) {
  6. $fields = new FieldSet(
  7. new TextField('FirstName', 'First name'),
  8. new EmailField('Email', 'Email address')
  9. );
  10.  
  11. $actions = new FieldSet(
  12. new FormAction('submit', 'Submit')
  13. );
  14. $validator = new RequiredFields('FirstName', 'Email');
  15.  
  16.  
  17. parent::__construct($controller, $name, $fields, $actions, $validator);
  18. }
  19.  
  20. function forTemplate() {
  21. return $this->renderWith(array(
  22. $this->class,
  23. 'Form'
  24. ));
  25. }
  26.  
  27. function submit($data, $form) {
  28. // do stuff here
  29. $submission = new MyFormSubmission();
  30. $form->saveInto($submission);
  31. $submission->write();
  32.  
  33. Director::redirect('/home/');
  34. }
  35.  
  36. }
  37.  
  38. ?>
  39.  
  40.  
  41. //MyForm.ss
  42. <form $FormAttributes>
  43. <% if Message %>
  44. <p id="{$FormName}_error" class="message $MessageType">$Message</p>
  45. <% else %>
  46. <p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
  47. <% end_if %>
  48.  
  49. <fieldset>
  50. <div id="FirstName" class="field text">
  51. <label class="left" for="{$FormName}_FirstName">First name</label><br>
  52. $dataFieldByName(FirstName)
  53. </div>
  54.  
  55. <div id="Email" class="field email">
  56. <label class="left" for="{$FormName}_Email">Email :</label><br>
  57. $dataFieldByName(Email)
  58. </div>
  59.  
  60. $dataFieldByName(SecurityID)
  61. </fieldset>
  62.  
  63. <% if Actions %>
  64. <div class="Actions">
  65. <% control Actions %>$Field<% end_control %>
  66. </div>
  67. <% end_if %>
  68. </form>
  69.  
  70. //cotroller insider HomePage.php
  71. class HomePage_Controller extends Page_Controller {
  72.  
  73. function MyFirstForm(){
  74. return new MyForm($this, 'MyFirstForm');
  75. }
  76.  
  77. }
  78.  
  79.  
  80. <?php
  81.  
  82. class MyFormSubmission extends DataObject {
  83. static $db = array(
  84. 'FirstName' => 'Varchar(255)',
  85. 'Email' => 'Text'
  86. );
  87. }
  88.  
  89.  
  90. ?>
Add Comment
Please, Sign In to add comment