|
NAMEHTML::FormHandler::Field - base class for fieldsVERSIONversion 0.40068SYNOPSISInstances 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 for test purposes (since there's no standard way to add a field to a form after construction).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; DESCRIPTIONThis 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 from the 'field_name_space' (or directly, when prefixed with '+'). If the type is not specified, it defaults to Text.See HTML::FormHandler::Manual::Fields for a list of the fields and brief descriptions of their structure. ATTRIBUTESNames, types, accessor
Field data
Form, parent
Errors
Attributes for creating HTMLThe 'element_attr' hashref attribute can be used to set arbitrary HTML attributes on a field's input tag.has_field 'foo' => ( element_attr => { readonly => 1, my_attr => 'abc' } ); Note that the 'id' and 'type' attributes are not set using element_attr. Use the field's 'id' attribute (or 'build_id_method') to set the id. The 'label_attr' hashref is for label attributes, and the 'wrapper_attr' is for attributes on the wrapping element (a 'div' for the standard 'simple' wrapper). A 'javascript' key in one of the '_attr' hashes will be inserted into the element as-is. The following are used in rendering HTML, but are handled specially. label - Text label for this field. Defaults to ucfirst field name. build_label_method - coderef for constructing the label wrap_label_method - coderef for constructing a wrapped label id - Useful for javascript (default is html_name. to prefix with form name, use 'html_prefix' in your form) build_id_method - coderef for constructing the id render_filter - Coderef for filtering fields before rendering. By default changes >, <, &, " to the html entities disabled - Boolean to set field disabled The order attribute may be used to set the order in which fields are rendered. order - Used for sorting errors and fields. Built automatically, but may also be explicitly set The following are discouraged. Use 'element_attr', 'label_attr', and 'wrapper_attr' instead. title - instead use element_attr => { title => '...' } style - instead use element_attr => { style => '...' } tabindex - instead use element_attr => { tabindex => 1 } readonly - instead use element_attr => { readonly => 'readonly' } Rendering of the various HTML attributes is done by calling the 'process_attrs' function (from HTML::FormHandler::Render::Util) and passing in a method that adds in error classes, provides backward compatibility with the deprecated attributes, etc. attribute hashref class attribute wrapping method ================= ================= ================ element_attr element_class element_attributes label_attr label_class label_attributes wrapper_attr wrapper_class wrapper_attributes element_wrapper_class element_wrapper_attributes ('element_wrapper' is for an inner div around the input element, not including the label. Used for Bootstrap3 rendering, but also available in the Simple wrapper.) The slots for the class attributes are arrayrefs; they will coerce a string into an arrayref. In addition, these 'wrapping methods' call a hook method in the form class, 'html_attributes', which you can use to customize and localize the various attributes. (Field types: 'element', 'wrapper', 'label') sub html_attributes { my ( $self, $field, $type, $attr ) = @_; $attr->{class} = 'label' if $type eq 'label'; return $attr; } The 'process_attrs' function will also handle an array of strings, such as for the 'class' attribute. tagsA hashref containing flags and strings for use in the rendering code. The value of a tag can be a string, a coderef (accessed as a method on the field) or a block specified with a percent followed by the blockname ('%blockname').Retrieve a tag with 'get_tag'. It returns a '' if the tag doesn't exist. This attribute used to be named 'widget_tags', which is deprecated. html5_type_attr [string]This string is used when rendering an input element as the value for the type attribute. It is used when the form has the is_html5 flag on.widgetThe 'widget' attribute is used in rendering, so if you are not using FormHandler's rendering facility, you don't need this attribute. It is used in generating HTML, in templates and the rendering roles. 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, create a widget role, such as MyApp::Form::Widget::Field::MyWidget. Provide the name space in the 'widget_name_space' attribute, and set the 'widget' of your field to the package name after the Field/Form/Wrapper: has_field 'my_field' => ( widget => 'MyWidget' ); 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 some of the provided field classes: Widget : Field classes -----------------------:--------------------------------- Text : Text, Integer Checkbox : Checkbox, Boolean RadioGroup : Select, Multiple, IntRange (etc) Select : Select, Multiple, IntRange (etc) CheckboxGroup : Multiple select TextArea : TextArea Compound : Compound, Repeatable, DateTime Password : Password Hidden : Hidden Submit : Submit Reset : Reset NoRender : Upload : Upload Widget roles are automatically applied to field classes unless they already have a 'render' method, and if the 'no_widgets' flag in the form is not set. You can create your own widget roles and specify the namespace in 'widget_name_space'. In the form: has '+widget_name_space' => ( default => sub { ['MyApp::Widget'] } ); If you want to use a fully specified role name for a widget, you can prefix it with a '+': widget => '+MyApp::Widget::SomeWidget' For more about widgets, see HTML::FormHandler::Manual::Rendering. Flagspassword - 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) DefaultsSee also the documentation on "Defaults" in HTML::FormHandler::Manual::Intro.
Constraints and ValidationsSee also HTML::FormHandler::Manual::Validation.Constraints set in attributes
applyUse 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 => { } }, { transform => sub { ... lc(shift) ... } } ], ); See more documentation in HTML::FormHandler::Manual::Validation. trimAn 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. Inflation/deflationThere are a number of methods to provide finely tuned inflation and deflation:
Normally if you have a deflation, you will need a matching inflation. There are two different flavors of inflation/deflation: one for inflating values to a format needed for validation and deflating for output, the other for inflating the initial provided values (usually from a database row) and deflating them for the 'values' returned. See HTML::FormHandler::Manual::InflationDeflation. Processing and validating the fieldvalidate_fieldThis 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.validateThis 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 ( ... ); } validate_method, set_validateSupply a coderef (which will be a method on the field) with 'validate_method' or the name of a form method with 'set_validate' (which will be a method on the form). If not specified and a form method with a name of "validate_<field_name>" exists, it will be used.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 'my_foo' => ( validate_method => \&my_foo_validation ); sub my_foo_validation { ... } has_field 'title' => ( isa => 'Str', set_validate => 'check_title' ); AUTHORFormHandler Contributors - see HTML::FormHandlerCOPYRIGHT AND LICENSEThis software is copyright (c) 2017 by Gerda Shank.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Visit the GSP FreeBSD Man Page Interface. |