DataMapper ORM


HTML Form Examples

No Longer Supported

As of Datamapper ORM 1.7.0, this extension is not supported. It is almost as complicated as Datamapper ORM itself, and I no longer have the time to maintain it. It is fully functional, but use at your own risk.

These examples show how the form was generated and processed to edit a bug in the example application, using the HTML Form and Array extensions.

Application / Models / Bug.php

This is the validation array that Bug uses. Notice how label is specified on all fields. Type has also been specified on non-text, non-relationship fields. Finally, because the priority column can only take a limited set of values, values was set on it to list the possible options.

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
var $validation = array( 'title' => array( 'label' => 'Title', 'rules' => array('required', 'trim', 'max_length' => 100) ), 'description' => array( 'label' => 'Description', 'rules' => array('required', 'xss_clean'), 'type' => 'textarea' ), 'priority' => array( 'label' => 'Priority', 'rules' => array('required', 'integer', 'min_size' => 0, 'max_size' => 3), 'type' => 'dropdown', 'values' => array( '0' => 'None', '1' => 'Low', '2' => 'Medium', '3' => 'High' ) ), 'creator' => array( 'label' => 'Creator', 'rules' => array('required') ), 'editor' => array( 'label' => 'Editor', 'rules' => array('required') ), 'status' => array( 'label' => 'Status', 'rules' => array('required') ), 'category' => array( 'label' => 'Categories' ), 'user' => array( 'label' => 'Assigned Users' ), 'dependency' => array( 'label' => 'This bug depends on' ), 'dependents' => array( 'label' => 'Other bugs that depend on this' ) );

Application / Controllers / Bugs.php

The controller sets up the form by specifying which fields to render. It also will process the form when submitted, and attempt a save. Unsuccessful saves will return to the form, and print out the errors.

The saving of the form is handled through the array from_array method. It handles processing all of the fields submitted by the form. However, because the creator and editor of the bug are not defined by the form (but by the logged in user), we handle these separately.

Note that this method is called by both the report and edit methods of the bugs controller, because they work almost exactly the same.

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/** * Called by the edit and report segments. * * @param string $title For the header * @param string $section For the header * @param object $bug Bug to edit or a blank bug * @param string $url The url to save on * @param boolean $save If TRUE, then attempt a save. */ function _edit($title, $section, $bug, $url, $save) { if($save) { // attempt to save the bug $bug->trans_start(); // Use the (already-loaded) array extension to process the POSTed values. $rel = $bug->from_array($_POST, array( 'title', 'description', 'priority', 'status', 'category', 'user' )); // We also have to specify the editor... $rel['editor'] = $this->login_manager->get_user(); if( ! $bug->exists()) { // ...and creator for new bugs $rel['creator'] = $this->login_manager->get_user(); } $exists = $bug->exists(); if($bug->save($rel)) { // saved successfully, so commit and redirect $bug->trans_complete(); // Store a message if($exists) { $this->session->set_flashdata('message', 'This bug was updated successfully.'); } else { $this->session->set_flashdata('message', 'This bug was created successfully.'); } redirect('bugs/view/' . $bug->id); } } // Load the htmlform extension, so we can generate the form. $bug->load_extension('htmlform'); // We want to limit the users to those who are assignable (not simply bug reporters) $users = new User(); $users->get_assignable(); // This is how are form will be rendered $form_fields = array( 'id', // Hidden id field 'title', // Title field 'description' => array( // multi-line field for description 'rows' => 6, // height and width could be specified using CSS instead 'cols' => 40 ), 'priority', // Priority (a dropdown containing 4 items) 'status', // Status (a dropdown with all known statuses) 'category', // A checkbox or select list of categories 'user' => array( // A checkbox or select list of users 'list' => $users // limit the users to the list above ) ); // Send the results to the views $this->output->enable_profiler(TRUE); $this->load->view('template_header', array('title' => $title, 'section' => $section)); $this->load->view('bugs/edit', array('bug' => $bug, 'form_fields' => $form_fields, 'url' => $url)); $this->load->view('template_footer'); }

Application / Views / Bugs / Edit.php

This view is very simple, although you could easily add instructions and other non-form information.

1 2 3 4 5
<?php echo $bug->render_form($form_fields, $url); ?>