Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function add()
- {
- // Setup the form field names
- $form = array
- (
- 'id' => '',
- 'firstname' => '',
- 'lastname' => '',
- 'phone' => '',
- 'email' => '',
- 'address' => '',
- 'city' => '',
- 'state' => '',
- 'zip' => ''
- );
- // copy the form as errors, so the errors will be stored with
- // keys corresponding to the form field names
- $errors = $form;
- if($post = $_POST)
- {
- // Generate a new model object
- $contact = ORM::factory('contact');
- // If the form validates against the model show the new contact
- if($contact->validate($post))
- {
- url::redirect('contact/view/'.$contact->id);
- }
- // If not repopulate the form and show the errors
- else
- {
- // Repopulate the $form array
- $form = arr::overwrite($form, $post->as_array());
- // Pass the error message and populate the errors
- $errors = arr::overwrite($errors, $post->errors('form_error_messages'));
- }
- }
- // Pass our info to the template
- $this->template->content = new View('pages/contact_form');
- $this->template->content->form = $form;
- $this->template->content->errors = $errors;
- }
- public function edit($contact_id)
- {
- // Load the contact info into an ORM object
- $contact = ORM::factory('contact', $contact_id);
- // Setup the form field names and populate
- $form = array
- (
- 'id' => $contact->id,
- 'firstname' => $contact->firstname,
- 'lastname' => $contact->lastname,
- 'phone' => $contact->phone,
- 'email' => $contact->email,
- 'address' => $contact->address,
- 'city' => $contact->city,
- 'state' => $contact->state,
- 'zip' => $contact->zip
- );
- // copy the form as errors, so the errors will be stored with
- // keys corresponding to the form field names
- $errors = $form;
- if($post = $_POST)
- {
- // If the form validates against the model show the new contact
- if($contact->validate($post))
- {
- url::redirect('contact/view/'.$contact->id);
- }
- // If not repopulate the form and show the errors
- else
- {
- // Repopulate the $form array
- $form = arr::overwrite($form, $post->as_array());
- // Pass the error message and populate the errors
- $errors = arr::overwrite($errors, $post->errors('form_error_messages'));
- }
- }
- // Pass our info to the template
- $this->template->content = new View('pages/contact_form');
- $this->template->content->form = $form;
- $this->template->content->errors = $errors;
- }
- }
- // model
- <?php defined('SYSPATH') or die('No direct script access.');
- class Contact_Model extends ORM
- {
- public function validate(array & $array, $save = TRUE)
- {
- $array = Validation::factory($array)
- ->pre_filter('trim', TRUE)
- ->pre_filter('ucfirst', 'firstname')
- ->pre_filter('ucfirst', 'lastname')
- ->add_rules('firstname','required')
- ->add_rules('email','valid::email')
- ->add_rules('state','length[2]')
- ->add_rules('zip','length[5,10]');
- return parent::validate($array, $save);
- }
- }
Add Comment
Please, Sign In to add comment