| Form::Sensible::Reflector::MySQL - Create a Form::Sensible object from a MySQL schema |
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.
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.
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 the Form::Sensible::Field manpage 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|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).
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?
The generated the Form::Sensible::Field::Toggle manpage has on_value an off_value of 1 an 0, respectively.
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.
No Form::Sensible::Field::Trigger is added to the form. See the SYNOPSIS, above.
See lang under reflect_from.
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:
=over 4
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.
The name of the table upon which to reflect, if not supplied in 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.
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.
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.
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:
mysql5> USE information_schema;
mysql5> SELECT column_name, column_comment FROM COLUMNS WHERE table_name='foo';
Should L<Form::Sensible::Form|Form::Sensible::Form> one day support a C<display_name> attribute,
this method could supply it with the table's C<COMMENT>: at present, that value is stored in
the C<form_display_name> field of the caller.
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.
None.
Private method inherited from Form::Sensible::Reflector.
Moose, Form::Sensible::Reflector, Form::Sensible, DBD::mysql, DateTime::Format::MySQL, the Math::BigInt manpage.
The Math::Big* modules may help if you use numbers.
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.
=cut
package Form::Sensible::Reflector::MySQL::I18N::enGB;
Form::Sensible::Reflector::MySQL::I18N::enGB - error messages
This namesapce provides the default error messages for form validation as described in INTERNATIONALISATION in the Form::Sensible::Reflector::MySQL manpage.
Copyright (C) Lee Goddard, 2010-2011. All Rights Reserved.
Made available under the same terms as Perl.
| Form::Sensible::Reflector::MySQL - Create a Form::Sensible object from a MySQL schema |