[PHPLIB-DEV] Some ideas on tpl_form From: Michael Haertl (mhaertl <email protected>)
Date: 12/16/99

Hi all,

i've spent some thoughts on the usage of tpl_form. I'm not shure if they
are in sense of the author of this class, so i would appreciate your
opinion on this.

Here we go:

I use php to manipulate all kinds of data in a db via a browser.
In most cases i use a form in a structure like this:

Let's assume we have a table with addresses:

- The user gets a search form to enter a search string
   for example "Enter cities to search for:"

- After form submission, there's a database query and
   i show a "list page" of all names in this town.
   Every name on this list is linked to something
   like "edit.php3?id=5"
   Somewhere you got a button for new entry which is a link
   to edit.php3?id=new.

- When the user clicks on a name on this list there appears
   a form where he can edit and save the address data.

- When the data is valid, the updated list is shown again
   otherwise the form will be displayed.

So i think when a form has to be displayed there are 3 common cases how
the default form values should be set:

1. new entry: this is simple. Just show an empty form
               All default_values = ""

2. edit entry: get the default values from the database and
               set as default values
               default_values = db_values

3. error in form validation:
               show user input values as default
               default_values = variable_values

So what i missed in tpl_form is a function which gathers the default
values from a db just like get_default_values() does from the submitted
variable values.

To get things clear a simplified schematic example of edit.php3

var $f new tpl_form;

if ( <ERROR_IN_VALIDATION> ) {
  $def_values = $f->get_default_values();
} elseif ( <EDIT_ENTRY> ) {
  $def_values = $f->get_db_values($id);
}
$f->set_default_values( $def_values );
$f->display();

If neither a error occured nor a record should be edited a empty form is
shown for a new entry. Yes i know, there's no validation here but it's
just to show the principle.

Here my idea of the additional method "get_db_values()" in tpl_form:

----------------------------------------------------
class tpl_form {

[...]

  var $db_assoc = array( # In this array i associate
      "name" => "ed_name", # every field in the table
      "street" => "ed_strasse", # with the name of the according
      "city" => "ed_stadt" # form field.
                                    # This is just an example
                                    # and should be overridden!!
  );

  var $db_tbl = "addresses"; # To define the table to use.
                                # Maybe a set_table() method
                                # would be more professional

  var $db_idx = "id"; # Defines the primary key field
                                # in the table.
                                # Maybe a set_idx() method
                                # would be better

[...]
  # Like get_default_values() but gathers the value array
  # from a database
  function get_db_values($id) {
    if (! is_object($this->form_data)) {
      $this->setup();
    }
    $fv = array();
    $d = new My_DB;
    reset($this->db_assoc);
    while ( list($dbn,$vrn) = each($this->db_assoc) ) {
      $d->query("SELECT $dbn FROM ".$this->db_tbl."WHERE"
                .$this->db_idx."=$id");
      $d->next_record();
      $fv[$vrn] = $d->f($dbn);
    }
    return $fv;
  }
----------------------------------------------------

I hope you got my point. If so, what do you think about it?

If it's completely off topic sorry for this longsome posting.

bye
Mike
-
PHPLIB Developers Mailing List. Send messages to <phplib-dev <email protected>>.
To unsubscribe, send "unsubscribe" to <phplib-dev-request <email protected>> in
the body, not the subject, of your message.