Guest User

Untitled

a guest
Apr 25th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. public function add()
  2. {
  3. // Setup the form field names
  4. $form = array
  5. (
  6. 'id' => '',
  7. 'firstname' => '',
  8. 'lastname' => '',
  9. 'phone' => '',
  10. 'email' => '',
  11. 'address' => '',
  12. 'city' => '',
  13. 'state' => '',
  14. 'zip' => ''
  15. );
  16.  
  17. // copy the form as errors, so the errors will be stored with
  18. // keys corresponding to the form field names
  19. $errors = $form;
  20.  
  21. if($post = $_POST)
  22. {
  23. // Generate a new model object
  24. $contact = ORM::factory('contact');
  25.  
  26. // If the form validates against the model show the new contact
  27. if($contact->validate($post))
  28. {
  29. url::redirect('contact/view/'.$contact->id);
  30. }
  31. // If not repopulate the form and show the errors
  32. else
  33. {
  34. // Repopulate the $form array
  35. $form = arr::overwrite($form, $post->as_array());
  36.  
  37. // Pass the error message and populate the errors
  38. $errors = arr::overwrite($errors, $post->errors('form_error_messages'));
  39. }
  40. }
  41.  
  42. // Pass our info to the template
  43. $this->template->content = new View('pages/contact_form');
  44. $this->template->content->form = $form;
  45. $this->template->content->errors = $errors;
  46. }
  47.  
  48. public function edit($contact_id)
  49. {
  50. // Load the contact info into an ORM object
  51. $contact = ORM::factory('contact', $contact_id);
  52.  
  53. // Setup the form field names and populate
  54. $form = array
  55. (
  56. 'id' => $contact->id,
  57. 'firstname' => $contact->firstname,
  58. 'lastname' => $contact->lastname,
  59. 'phone' => $contact->phone,
  60. 'email' => $contact->email,
  61. 'address' => $contact->address,
  62. 'city' => $contact->city,
  63. 'state' => $contact->state,
  64. 'zip' => $contact->zip
  65. );
  66.  
  67. // copy the form as errors, so the errors will be stored with
  68. // keys corresponding to the form field names
  69. $errors = $form;
  70.  
  71. if($post = $_POST)
  72. {
  73. // If the form validates against the model show the new contact
  74. if($contact->validate($post))
  75. {
  76. url::redirect('contact/view/'.$contact->id);
  77. }
  78. // If not repopulate the form and show the errors
  79. else
  80. {
  81. // Repopulate the $form array
  82. $form = arr::overwrite($form, $post->as_array());
  83.  
  84. // Pass the error message and populate the errors
  85. $errors = arr::overwrite($errors, $post->errors('form_error_messages'));
  86. }
  87. }
  88.  
  89. // Pass our info to the template
  90. $this->template->content = new View('pages/contact_form');
  91. $this->template->content->form = $form;
  92. $this->template->content->errors = $errors;
  93. }
  94. }
  95.  
  96. // model
  97.  
  98. <?php defined('SYSPATH') or die('No direct script access.');
  99.  
  100. class Contact_Model extends ORM
  101. {
  102. public function validate(array & $array, $save = TRUE)
  103. {
  104.  
  105. $array = Validation::factory($array)
  106. ->pre_filter('trim', TRUE)
  107. ->pre_filter('ucfirst', 'firstname')
  108. ->pre_filter('ucfirst', 'lastname')
  109. ->add_rules('firstname','required')
  110. ->add_rules('email','valid::email')
  111. ->add_rules('state','length[2]')
  112. ->add_rules('zip','length[5,10]');
  113.  
  114. return parent::validate($array, $save);
  115. }
  116. }
Add Comment
Please, Sign In to add comment