NAME
HTML::FormHandler::Field - Base class for HTML::FormHandler Fields
SYNOPSIS
Instances of Field subclasses are generally built by HTML::FormHandler from 'has_field' declarations or the field_list, but they can also be constructed using new (usually for test purposes).
use HTML::FormHandler::Field::Text;
my $field = HTML::FormHandler::Field::Text->new( name => $name, ... );
In your custom field class:
package MyApp::Field::MyText;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Field::Text';
has 'my_attribute' => ( isa => 'Str', is => 'rw' );
apply [ { transform => sub {...} },
{ check => ['fighter', 'bard', 'mage' ], message => '....' }
];
1;
DESCRIPTION
This is the base class for form fields. The 'type' of a field class is used in the FormHandler field_list or has_field to identify which field class to load. If the type is not specified, it defaults to Text.
There are two rough categories of Field classes: those that do extra processing and those that are simple validators. The 'Compound', 'Repeatable', and 'Select' fields are fields that are functional.
A number of field classes are provided by the distribution. The basic for-validation (as opposed to 'functional') field types are:
Text
Integer
Boolean
These field types alone would be enough for most applications, since the equivalent of the others could be defined using field attributes, custom validation methods, and applied actions. There is some benefit to having descriptive names, of course, and if you have multiple fields requiring the same validation, defining a custom field class may be a good idea.
Inheritance hierarchy of the distribution's field classes:
Compound
Repeatable
Text
Money
Password
Integer
PosInteger
TextArea
HtmlArea
Select
Multiple
IntRange
Hour
Minute
MonthDay
Month
Second
Year
MonthName
Weekday
Boolean
Checkbox
DateMDY
DateTime
Email
PrimaryKey
See the documentation or source for the individual fields.
Many field classes contain only a list of constraints and transformations to apply. Some use the 'validate' method, which is called after the actions are applied. Some build a custom select list using 'build_options'.
ATTRIBUTES
Names, types, accessor
- name
-
The name of the field. Used in the HTML form. Often a db accessor. The only required attribute.
- type
-
The class or type of the field. The 'type' of HTML::FormHandler::Field::Money is 'Money'. Classes that you define yourself are prefixed with '+'.
- accessor
-
If the name of your field is different than your database accessor, use this attribute to provide the accessor.
- full_name
-
The name of the field with all parents:
'event.start_date.month'
- full_accesor
-
The field accessor with all parents
- html_name
-
The full_name plus the form name if 'html_prefix' is set.
- input_param
-
By default we expect an input parameter based on the field name. This allows you to look for a different input parameter.
Field data
- inactive
-
Set this attribute if this field is inactive. This provides a way to define fields in the form and selectively set them to inactive. There is also an '_active' attribute, for internal use to indicate that the field has been activated by the form's 'active' attribute.
- input
-
The input string from the parameters passed in.
- value
-
The value as it would come from or go into the database, after being acted on by transforms. Used to construct the
$form->values
hash. Validation and constraints act on 'value'. - fif
-
Values used to fill in the form. Read only. Use a deflation to get from 'value' to 'fif' if the an inflator was used.
[% form.field('title').fif %]
- init_value
-
Initial value populated by init_from_object. You can tell if a field has changed by comparing 'init_value' and 'value'. Read only.
- input_without_param
-
Input for this field if there is no param. Needed for checkbox, since an unchecked checkbox does not return a parameter.
Form, parent
- form
-
A reference to the containing form.
- parent
-
A reference to the parent of this field. Compound fields are the parents for the fields they contain.
Errors
- errors
-
Returns the error list for the field. Also provides 'num_errors', 'has_errors', 'push_errors' and 'clear_errors' from Array trait. Use 'add_error' to add an error to the array if you want to use a MakeText language handle. Default is an empty list.
- add_error
-
Add an error to the list of errors. If $field->form is defined then process error message as Maketext input. See $form->language_handle for details. Returns undef.
return $field->add_error( 'bad data' ) if $bad;
- error_fields
-
Compound fields will have an array of errors from the subfields.
Attributes for creating HTML
label - Text label for this field. Defaults to ucfirst field name.
title - Place to put title for field.
style - Place to put field style string
css_class - For a css class name (string; could be several classes,
separated by spaces or commas)
id - Useful for javascript (default is html_name. to prefix with
form name, use 'html_prefix' in your form)
disabled - for the HTML flag
readonly - for the HTML flag
javascript - for a Javascript string
order - Used for sorting errors and fields. Built automatically,
but may also be explicitly set
widget
The 'widget' attribute is not used by base FormHandler code. It is intended for use in generating HTML, in templates and the rendering roles, and is used in HTML::FormHandler::Render::Simple. Fields of different type can use the same widget.
This attribute is set in the field classes, or in the fields defined in the form. If you want a new widget type, use a new name and provide a 'widget_<name>'
method in your copy of Render::Simple or in your form class. If you are using a template based rendering system you will want to create a widget template. (see HTML::FormHandler::Manual::Templates) If you are using the widget roles, you can specify the widget with the short class name instead.
Widget types for the provided field classes:
Widget : Field classes
---------------:-----------------------------------
text (Text) : Text, Integer
checkbox (Checkbox) : Checkbox, Boolean
radio_group
(RadioGroup) : Select, Multiple, IntRange (etc)
select (Select) : Select, Multiple, IntRange (etc)
checkbox_group
(CheckboxGroup) : Multiple select
textarea (Textarea) : TextArea, HtmlArea
compound (Compound) : Compound, Repeatable, DateTime
password (Password) : Password
hidden (Hidden) : Hidden
submit (Submit) : Submit
reset (Reset) : Reset
no_render (NoRender) :
upload (Upload) : Upload
Widget roles are automatically applied to field classes unless they already have a 'render' method. Render::Simple will fall back to doing $field->render
if the corresponding widget method does not exist.
Flags
password - prevents the entered value from being displayed in the form
writeonly - The initial value is not taken from the database
noupdate - Do not update this field in the database (does not appear in $form->value)
Form methods for fields
These provide the name of a method in a form (not the field ) which will act on a particular field.
- set_validate
-
Specify a form method to be used to validate this field. The default is
'validate_' . $field->name
. Periods in field names will be replaced by underscores, so that the field 'addresses.city' will use the 'validate_addresses_city' method for validation.has_field 'title' => ( isa => 'Str', set_validate => 'check_title' ); has_field 'subtitle' => ( isa => 'Str', set_validate => 'check_title' );
- set_default
-
The name of the method in the form that provides a field's default value. Default is
'default_' . $field->name
. Periods replaced by underscores. - default
-
Provide an initial value just like the 'set_default' method, except in the field declaration:
has_field 'bax' => ( default => 'Default bax' );
Constraints and Validations
Constraints set in attributes
- required
-
Flag indicating whether this field must have a value
- required_message
-
Error message text added to errors if required field is not present The default is "Field <field label> is required".
- unique
-
Flag to initiate checks in the database model for uniqueness.
- unique_message
-
Error message text added to errors if field is not unique
- range_start
- range_end
-
Field values are validated against the specified range if one or both of range_start and range_end are set and the field does not have 'options'.
The IntRange field uses this range to create a select list with a range of integers.
In a FormHandler field_list
age => { type => 'Integer', range_start => 18, range_end => 120, }
apply
Use the 'apply' keyword to specify an ArrayRef of constraints and coercions to be executed on the field at validate_field time.
has_field 'test' => (
apply => [ 'MooseType', { check => sub {...}, message => { } } ],
);
In general the action can be of three types: a Moose type (which is represented by its name), a transformation (which is a callback called on the value of the field), or a constraint ('check') which performs a 'smart match' on the value of the field. Currently we implement the smart match in our code - but in the future when Perl 5.10 is more widely used we'll switch to the core http://search.cpan.org/~rgarcia/perl-5.10.0/pod/perlsyn.pod#Smart_matching_in_detail smart match operator.
The Moose type action first tries to coerce the value - then it checks the result, so you can use it instead of both constraints and tranformations - TIMTOWTDI. For most constraints and transformations it is your choice as to whether you use a Moose type or use a 'check' or 'transform'.
All three types define a message to be presented to the user in the case of failure. Messages are passed to Locale::MakeText, and can either be simple strings or an array suitable for MakeText, such as:
message => ['Email should be of the format [_1]',
'someuser@example.com' ]
Transformations and coercions are called in an eval to catch the errors. Warnings are trapped in a sigwarn handler.
All the actions are called in the order that they are defined, so that you can check constraints after transformations and vice versa. You can weave all three types of actions in any order you need. The actions specified with 'apply' will be stored in an 'actions' array.
To declare actions inside a field class use HTML::FormHandler::Moose and 'apply' sugar:
package MyApp::Field::Test;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Field;
apply [ 'SomeConstraint', { check => ..., message => .... } ];
1;
Actions specified with apply are cumulative. Actions may be specified in field classes and additional actions added in the 'has_field' declaration.
You can see examples of field classes with 'apply' actions in the source for HTML::FormHandler::Field::Money and HTML::FormHandler::Field::Email.
Moose types for constraints and transformations
Moose types can be used to do both constraints and transformations. If a coercion exists it will be applied, resulting in a transformation. You can use type constraints form MooseX::Types> libraries or defined using Moose::Util::TypeConstraints.
A Moose type defined with Moose::Util::TypeConstraints: subtype 'MyStr' => as 'Str' => where { /^a/ };
This is a simple constraint checking if the value string starts with the letter 'a'.
Another Moose type: subtype 'MyInt' => as 'Int'; coerce 'MyInt' => from 'MyStr' => via { return $1 if /(\d+)/ };
This type contains a coercion.
You can use them in a field like this (types defined with MooseX::Types would not be quoted):
has_field 'some_text_to_int' => (
apply => [ 'MyStr', 'MyInt' ]
);
This will check if the field contains a string starting with 'a' - and then coerce it to an integer by extracting the first continuous string of digits.
If the error message returned by the Moose type is not suitable for displaying in a form, you can define a different error message by using the 'type' and 'message' keys in a hashref:
apply => [ { type => 'MyStr', message => 'Not a valid value' } ];
Non-Moose checks and transforms
A simple 'check' constraint uses the 'check' keyword pointing to a coderef, a regular expression, or an array of valid values, plus a message.
A 'check' coderef will be passed the current value of the field. It should return true or false:
has_field 'this_num' => (
apply => [
{
check => sub { if ( $_[0] =~ /(\d+)/ ) { return $1 > 10 } },
message => 'Must contain number greater than 10',
}
);
A 'check' regular expression:
has_field 'some_text' => (
apply => [ { check => qr/aaa/, message => 'Must contain aaa' } ],
);
A 'check' array of valid values:
has_field 'more_text' => (
aply => [ { check => ['aaa', 'bbb'], message => 'Must be aaa or bbb' } ]
);
A simple transformation uses the 'transform' keyword and a coderef. The coderef will be passed the current value of the field and should return a transformed value.
has_field 'sprintf_filter' => (
apply => [ { transform => sub{ sprintf '<%.1g>', $_[0] } } ]
);
trim
An action to trim the field. By default this contains a transform to strip beginning and trailing spaces. Set this attribute to null to skip trimming, or supply a different transform.
trim => { transform => sub {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
} }
trim => { type => MyTypeConstraint }
Trimming is performed before any other defined actions.
deflation, deflate
A 'deflation' is a coderef that will convert from an inflated value back to a flat data representation suitable for displaying in an HTML field. If deflation is defined for a field it is automatically used for data that is taken from the database. For the fill-in-form value (fif) usually the fif string is taken straight from the input string if it exists, so if you want to use a deflated value instead, set the 'fif_from_value' flag on the field. Normally you'd only need to do that if you want to 'canonicalize' the entered data, such as if a user enters '09' for the year and you want to re-display it as '2009'.
has_field 'my_date_time' => (
type => 'Compound',
apply => [ { transform => sub{ DateTime->new( $_[0] ) } } ],
deflation => sub { { year => $_[0]->year, month => $_[0]->month, day => $_[0]->day } },
fif_from_value => 1,
);
has_field 'my_date_time.year' => ( fif_from_value => 1 );
has_field 'my_date_time.month';
has_field 'my_date_time.day' => ( fif_from_value => 1 );
You can also use a 'deflate' method in a custom field class. See the Date field for an example. If the deflation requires data that may vary (such as a format) string and thus needs access to 'self', you would need to use the deflate method since the deflation coderef is only passed the current value of the field
Processing and validating the field
validate_field
This is the base class validation routine. Most users will not do anything with this. It might be useful for method modifiers, if you want code that executed before or after the validation process.
validate
This field method can be used in addition to or instead of 'apply' actions in custom field classes. It should validate the field data and set error messages on errors with $field->add_error
.
sub validate {
my $field = shift;
my $value = $field->value;
return $field->add_error( ... ) if ( ... );
}
AUTHORS
HTML::FormHandler Contributors; see HTML::FormHandler
Initially based on the original source code of Form::Processor::Field by Bill Moseley
COPYRIGHT
This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself.