NAME
Tk::DBI::Form
SYNOPSIS
my $mw = MainWindow->new;
my $tkdbi = $mw->DBIForm(
-dbh => $dbh,
-table => 'Inventory',
-editId => 'yes',
-readonly => {
changed_by => 'xpix',
created => 'NOW',
...
},
-required => {
name => 1,
state => 1,
owner => 1,
...
},
-test_cb => {
type_id => sub{
my ($save, $name) = @_;
if($save->{type_id} and $save->{type_id} !~ /^\d+$/) {
$dbh->do(sprintf("INSERT INTO Type (name) VALUES ('%s')", $save->{type_id}));
$save->{type_id} = $dbh->{'mysql_insertid'};
}
return undef; # Alles ok!
},
...
},
-link => {
type_id => {
table => 'Type',
display => 'name',
id => 'id',
},
...
},
-validate_cb => {
serial_no => sub {
my ($entry, $save, $input) = @_;
$save->{id} = 0 unless(defined $save->{id});
$entry->configure(
-bg => ( exists $SERIAL->{$input} && $save->{id} != $SERIAL->{$input}->{id} ? 'red' : 'green' ),
-fg => ( exists $SERIAL->{$input} && $save->{id} != $SERIAL->{$input}->{id} ? 'white' : 'black' ),
);
return 1 ;
},
...
},
-images => {
id => $pics{F1},
parent_id => $pics{F2},
...
},
-events => {
'<KeyRelease-F1>' => sub {
$DBIFORM->{entrys}->{id}->focus;
},
...
},
-addButtons => {
Logs => {
-type => ['update'],
-callback => sub{
my ($save, $name) = @_;
&launch_browser_log($save->{id});
},
},
...,
},
-alternateTypes => {
filename => {
type => 'file',
directory => $DOCUPATH,
},
...
},
-debug => 1,
);
my $ok = $tkdbi->editRecord($row->{id});
DESCRIPTION
Tk::DBI::Form is a Megawidget to allow edit, delete or insert a record from a table.
OPTIONS
-dbh
The database handle to get the information from Database.
-table
The table where you will change the data.
-debug => 1
Switch the debug output at the standart console on.
-edit_id => 1
This allows to edit the ID-Number, this is normaly a unique and autoincrement Field for every column.
-update => [qw(id col1 col2 ...)]
This is the list to allow update following Fields. Only this fields is display in the update Form.
-insert => [qw(id col1 col2 ...)]
This is the list to allow insert following Fields. Only this fields is display in the insert Form.
-link => { col1 => {table => tablename, display => col2, id => idcol, where => 'WHERE col3 = 1'}, ... }
This is a special Feature for Fields in a another Table. Often we use other datas from tables, this data have a id number and a description. The id number from this table is mostly in the table to edit as id number. Here you can display the Description for this id and the user can change this choice. I.e.:
-link => {
parent_id => {
table => 'Inventory',
display => 'name',
where => 'WHERE type_id = 1',
id => 'id',
},
type_id => {
table => 'Type',
display => 'name',
id => 'id',
},
}
Ok, here we have two linktables. This will display a Listwidget, thes have the column 'name' to display in this Listbox. But the form write the id in the original column.
-required => { col1 => 1, col2 = 1, ...}
Here you can mark the required field for the Form, the form will display a error MessageBox to display 'col1 is a required field!'.
-required => {
changed_by => 1,
deadline => 1,
Server => 1,
}
-readonly => { col1 => 'text', col2 = number, ...}
This will display the values and the user can't change this data.
-readonly => {
changed_by => $USER,
deadline => 'NOW',
Server => $HOST,
}
-default => { col1 => 'text', col2 = number, ...}
This will display default values for the form. I.e.:
-default => {
changed_by => $USER,
deadline => 'NOW',
Server => $HOST,
}
-images => { col1 => ImageObj, col2 = ImageObj, ...}
This will display a little Icon next to the input or whatever widget.
-alternateTypes => { col1 => ImageObj, col2 = ImageObj, ...}
Here you can get a alternativeType to display. I.E.:
-alternateTypes => {
filename => {
type => 'file',
directory => $DOCUPATH,
},
password => {
type => 'password',
},
},
- file
-
This will display a entry and button, the user can click on this button and form will display a Fileselector for get the right file and path.
- password
-
This will display a entry with hidden letters as stars..
-events => { Event => sub{}, Event => sub{}, ...}
This will add your personal events whatever you need. I.E.:
-events => {
'<KeyRelease-F1>' => sub {
$DBIFORM->{entrys}->{id}->focus;
},
-validate_cb => { col1 => sub{}, col2 => sub{}, ...}
Here you can add a callback to test the input from the user in realtime. The parameter for the subroutine is the entry, save hash with data from the Form ans the input from the User. I.E.:
serial_no => sub {
my ($entry, $save, $input) = @_;
$save->{id} = 0 unless(defined $save->{id});
$entry->configure(
-bg => ( exists $SERIAL->{$input} && $save->{id} != $SERIAL->{$input}->{id} ? 'red' : 'green' ),
-fg => ( exists $SERIAL->{$input} && $save->{id} != $SERIAL->{$input}->{id} ? 'white' : 'black' ),
);
return 1 ;
},
This change the foreground and background color from the entry when the serial number exist in the table. The subroutine can return a undef value, then the widget ignoring this Userinput. I.e.:
only_numbers => sub {
my ($entry, $save, $input) = @_;
return undef unless($input =~ /[^0-9]+/);
return 1 ;
},
-test_cb => { col1 => sub{}, col2 => sub{}, ...}
Here you can add a callback to test the user input AFTER submit this form. The parameter for the subroutine is the save hash and the name from this field. I.E.:
-test_cb => {
id => sub{
my ($save, $name) = @_;
if($DBIFORM->type() eq 'insert' and $save->{id}) {
my $answer = qsure($top,sprintf('You will REPLACE row <%s>?', $save->{id}));
return 'NOMESSAGE' unless($answer); # Back without message
}
return undef; # All OK ...
},
parent_id => sub{
my ($save, $name) = @_;
my $pid = sprintf('%010d', $save->{parent_id});
unless(exists $INV->{$pid}) {
my $msg = sprintf('Parent ID %s not exists', $pid);
return $msg;
}
return undef; # All OK!
},
}
The first example will pop up a MessageBox when the User make a Insert with a id number (replace). The second example will reformat the parent_id Number to 0000000012. If the parent_id doesnt exist in the Hash, the will return a Errormessage (MessageBox) with the returned text. 'NOMESSAGE' as return doesnt pop up a MessageBox. Return undef, all ok.
-cancel_cb => sub{ }
Here you can add a callback when the User activate the Cancel Button.
-addButtons => { ButtonName => {-type => ['update', 'insert'], -callback => sub{} }
Here you can add a Button to the FormBox. The -type option will only display the button in the following state (insert, update or delete). The callback has one parameter. The save hash. I.e.:
-addButtons => {
Logs => {
-type => ['update'],
-callback => sub{
my ($save, $name) = @_;
&launch_browser_log($save->{id});
},
},
},
The example will display a logbrowser when the user click on the Button 'Logs'.
METHODS
editRecord(id);
This will display the update form with the following id number for update.
newRecord([id]);
This will display the insert form with the following id number for Replace.
my $datahash = $DBH->selectall_hashref(select * from table where id = 12);
delete $datahash->{id};
$DBIFORM->newRecord(
{
default => $datahash,
},
);
Here you see a trick to copy a column, also display a insert form with the values from column 12.
deleRecord(id);
This will display the delete form with the following id number for Delete.
Table_is_Change(last_time, 'tablename');
This return true when the table is changed at the last_time (seconds at epoche).
ADVERTISED WIDGETS
The Widgets in the form is advertised with 'wi_namecolumn'.
AUTHOR
xpix@netzwert.ag
Copyright (C) 2003 , Frank (xpix) Herrmann. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
KEYWORDS
Tk::JBrowseEntry, Tk::XDialogBox, Tk::NumEntry, Tk::Date, Tk::LabFrame, Tk::FBox