Rose::HTML::Form::Field::DateTime::Range - Compound field for date ranges with separate text fields for the minimum and maximum dates.
$field = Rose::HTML::Form::Field::DateTime::Range->new( label => 'Date', name => 'date', default => [ '1/2/2003', '4/5/2006' ]); my($min, $max) = $field->internal_value; # DateTime objects print $min->strftime('%Y-%m-%d'); # "2003-01-02" print $max->strftime('%Y-%m-%d'); # "2006-04-05" $field->input_value('5/6/1980 3pm to 2003-01-06 20:19:55'); my $dates = $field->internal_value; print $dates->[0]->hour; # 15 print $dates->[1]->hour; # 20 print $dates->[0]->day_name; # Tuesday print $field->html; ...
Rose::HTML::Form::Field::DateTime::Range is a compound field that represents a date range. It is made up of two subfields: a Rose::HTML::Form::Field::DateTime::StartDate field and a Rose::HTML::Form::Field::DateTime::EndDate field.
The internal value of this field is a list (in list context) or reference to an array (in scalar context) of two DateTime objects. The first object is the start date and the second is the end date. If either of fields are not filled in or are otherwise invalid, then the internal value is undef.
The input value can be a reference to an array of DateTime objects, or strings that can be inflated into DateTime objects by the Rose::HTML::Form::Field::DateTime::StartDate and Rose::HTML::Form::Field::DateTime::EndDate classes. The input value can also be a concatenation of two such strings, joined by a string that matches the field's range_separator_regex.
This class is a good example of a compound field whose internal value consists of more than one object. See below for more compound field examples.
It is important that this class inherits from Rose::HTML::Form::Field::Compound. See the Rose::HTML::Form::Field::Compound documentation for more information.
Get or set the string used to join the output values of the start and end date subfields in order to produce this field's output value. The default string is "#". Example:
$field->input_value([ '1/2/2003', '4/5/2006' ]); # "2003-01-02 00:00:00#2006-04-05 00:00:00" print $field->output_value;
Get or set the regular expression used to split an input string into start date and end date portions. The default value is qr(#|\s+to\s+). Example:
qr(#|\s+to\s+)
$field->input_value('2005-04-20 8pm to 1/7/2006 3:05 AM'); my($min, $max) = $field->internal_value; print $min->day_name; # Wednesday print $max->day_name; # Saturday # Change regex, adding support for " - " $field->range_separator_regex(qr(#|\s+(?:to|-)\s+)); $field->input_value('2005-04-20 8pm - 1/7/2006 3:05 AM'); ($min, $max) = $field->internal_value; print $min->day_name; # Wednesday print $max->day_name; # Saturday
Note that the range_separator_regex must match the range_separator string.
range_separator_regex
range_separator
When setting range_separator_regex, you should use the qr operator to create a pre-compiled regex (as shown in the example above) If you do not, then the regex will be recompiled each time it's used.
qr
Other examples of custom fields:
A text field that only accepts valid email addresses.
Uses inflate/deflate to coerce input into a fixed format.
Uses inflate/deflate to convert input to a DateTime object.
A simple compound field that coalesces multiple subfields into a single value.
A compound field that uses inflate/deflate convert input from multiple subfields into a DateTime object.
A compound field that includes other compound fields and uses inflate/deflate convert input from multiple subfields into a DateTime object.
John C. Siracusa (siracusa@gmail.com)
Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Rose::HTML::Objects, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Rose::HTML::Objects
CPAN shell
perl -MCPAN -e shell install Rose::HTML::Objects
For more information on module installation, please visit the detailed CPAN module installation guide.