Form::Sensible::Reflector::MySQL - Create a Form::Sensible object from a MySQL schema
use Form::Sensible; use Form::Sensible::Reflector::MySQL; my $reflector = ReflectorMySQL->new(); my $form = $reflector->reflect_from($dbh, { form_name => $table_name, information_schema_dbh => DBI->connect( 'DBI:mysql:database=information_schema;host=localhost', 'root', 'password', ), # populate => 1, # only_columns => [qw[ id forename surname dob ] ], } ); $form->add_field( Form::Sensible::Field::Toggle->new( name => 'Submit form', ) ); my $renderer = Form::Sensible->get_renderer('HTML'); my $html = $renderer->render($form)->complete; exit;
This module provides to Form::Sensible the ability to simply create from MySQL tables forms whose fields and validators reflect the schema of the database table. Forms can be created empty, or from the value of a specified row. Joins are not supported.
This module is to be considered a pre-release, to gather test feedback and suggestions. It should not be considered stable for a production environment, and its implementation is subject to change.
Specifically, type checking of large numberics (specifically doubles) is not well tested, and Select fields (for ENUM and SET) seems to require some updating of Form::Sensible itself.
Select
ENUM
SET
Form::Sensible
Field names are taken from column names, or can be taken from column comments (see "information_schema_dbh" under "reflect_form").
Fields are marked as required if their database definition contains NOT NULL.
NOT NULL
Default values are, by default, taken from the database - see "no_db_defaults", below "reflect_from".
String and numeric types are mapped to the appropriate Form::Sensible::Field types, with the exceptions described below.
NB Numeric fields may only contain numbers with decimal points (.) but not commas. This outght to be locale dependant, but as far as I can tell, MySQL only accepts data in this format (http://dev.mysql.com/doc/refman/5.0/en/locale-support.html).
.
Set and enumeration columns are rendered as Select fields.
Boolean fields (Form::Sensible::Field::Toggle) are expected to be defined in the SQL schema as BIT(1) columns, based on the rationale outlined in comments on the MySQL website (http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html).
BIT(1)
NB This is in contradiction to the MySQL behaviour of interpreting the declaration of BOOLEAN columns as TINYINT(1). Support for the latter could be added as a Toggle field, but what would a NULL entry signfiy?
BOOLEAN
TINYINT(1)
Toggle
NULL
The generated Form::Sensible::Field::Toggle has on_value an off_value of 1 an 0, respectively.
on_value
off_value
1
0
These are currently converted to Text and LongText fields. That can of course be changed by the user after the form as been created, but I am open to suggestions on how to better handle them.
Text
LongText
No Form::Sensible::Field::Trigger is added to the form. See the SYNOPSIS, above.
See lang under "reflect_from".
lang
ReflectorMySQL->new->reflect_from( $dbh, $options );
Creates a form from a MySQL table schema, optionally populating it with data from the table.
In the above example, $dbh is a connected database handle, and $options a reference to a hash as follows:
$dbh
$options
In keeping with Form::Sensible::Reflector, this field can hold the name of the table, though that will be over-ridden by any value in table_name.
table_name
The name of the table upon which to reflect, if not supplied in form_name.
form_name
Optinal. If a true value, will cancel the default behaviour of populating the empty form with any DEFAULT values set in the schema (aside from CURRENT_TIMESTAMP.
DEFAULT
CURRENT_TIMESTAMP
An anonymous list of the names of columns to use, to override the default behaviour of using all columns.
Optional. If supplied, causes the created form to be populated with data from a row in the table. The value of populate should be a reference to a hash that defines column names and values to be used in the selection of a record. No error checking is performed.
populate
Optional DBH connected to the information_schema database. If supplied, each field's display_name will be set from the COMMENT recorded in the table definition, if available.
information_schema
display_name
COMMENT
Field comments are created thus:
CREATE TABLE foo ( forename VARCHAR(255) NOT NULL COMMENT 'First name' ) ALTER TABLE foo CHANGE COLUMN forename forename VARCHAR(255) NOT NULL COMMENT 'Forename'
Comments can be viewed with SHOW CREATE foo or as follows:
SHOW CREATE foo
mysql5> USE information_schema; mysql5> SELECT column_name, column_comment FROM COLUMNS WHERE table_name='foo';
Should Form::Sensible::Form one day support a display_name attribute, this method could supply it with the table's COMMENT: at present, that value is stored in the form_display_name field of the caller.
form_display_name
Defines the language of error messages that are not over-rideable using the default means supplied by Form::Sensible. The format should be that used by HTML, as described at http://www.w3.org/International/articles/language-tags/, without hyphenation.
The default language is enGB. To use another language, supply another string, and use it to create a namespace such as Form::Sensible::Reflector::MySQL::I18N::enGB that contains hash reference called $I18N. Please see the bottom of this file's source code for the required keys.
enGB
Form::Sensible::Reflector::MySQL::I18N::enGB
$I18N
None.
Private method inherited from Form::Sensible::Reflector.
Moose, Form::Sensible::Reflector, Form::Sensible, DBD::mysql, DateTime::Format::MySQL, Math::BigInt.
The Math::Big* modules may help if you use numbers.
Math::Big*
At the time of writing, the young Form::Sensible::Field::Select has a bug which prevents the correct setting of the selected entity, as well as preventing multiple selections. This has been reported and will be fiex in the next release.
Numeric fields may not contain commas, as described under "FIELD TYPES".
Please make use of CPAN's Request Tracker.
The MySQL Manual, Chater 10 Data Types.
Form::Sensible, Form::Sensible::Reflector, Form::Sensible::Reflector::DBIC, Form::Sensible::Field.
Copyright (C) Lee Goddard, 2010-2011. All Rights Reserved.
Made available under the same terms as Perl.
Form::Sensible::Reflector::MySQL::I18N::enGB - error messages
This namesapce provides the default error messages for form validation as described in "INTERNATIONALISATION" in Form::Sensible::Reflector::MySQL.
To install Form::Sensible::Reflector::MySQL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Form::Sensible::Reflector::MySQL
CPAN shell
perl -MCPAN -e shell install Form::Sensible::Reflector::MySQL
For more information on module installation, please visit the detailed CPAN module installation guide.