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;
extends 'HTML::FormHandler::Field::Text';

has 'my_attribute' => ( isa => 'Str', is => 'rw' );

apply [ { transform => sub {...}, message => '...' },
        { 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 validat 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
   

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'
html_name

The full_name plus the form name if 'html_prefix' is set.

Field data

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.

[% 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 Collection::Array metaclass. 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;

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
id    - Useful for javascript (default is form_name + field_name)
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 explicity 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)

Widget types for the provided field classes:

Widget      : Field classes 
------------:-----------------------------------
text        : Text, Integer
checkbox    : Checkbox, Boolean
radio_group : Select, Multiple, IntRange (etc) 
select      : Select, Multiple, IntRange (etc)
textarea    : TextArea, HtmlArea
compound    : Compound, Repeatable, DateTime
password    : Password
hidden      : Hidden
submit      : Submit

The default widget is 'text'.

Flags

password  - prevents the entered value from being displayed in the form
writeonly - The initial value is not taken from the database
clear     - Always set the database column to null.
noupdate  - Do not update this field in the database

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

has_field 'title' => ( isa => 'Str', set_validate => 'check_title' );
has_field 'subtitle' => ( isa => 'Str', set_validate => 'check_title' );
set_init

The name of the method in the form that provides a field's initial value

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 it's 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

A Hashref containing a transfrom 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 { } }

Trimming is performed before any other defined actions.

deflation

A coderef that will convert from an inflated value back to a flat data representation suitable for displaying in an HTML field

has_field 'my_date_time' => ( 
   type => 'Compound',
   apply => [ { transform => sub{ DateTime->new( $_[0] ) } } ],
   deflation => sub { { year => $_->year, month => $_->month, day => $_->day } },
   fif_from_value => 1,
);
has_field 'date_time_fif.year' => ( fif_from_value => 1 );
has_field 'date_time_fif.month';
has_field 'date_time_fif.day' => ( fif_from_value => 1 );

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.

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 ( ... );
    return 1;
}

AUTHORS

Gerda Shank, gshank@cpan.org

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.