Skip to content

Generates markup for and processes input from HTML forms

Notifications You must be signed in to change notification settings

pjfl/p5-html-forms

Repository files navigation

Name

HTML::Forms - Generates markup for and processes input from HTML forms

Synopsis

{
   package HTML::Forms::Renderer;

   use Moo::Role;

   with 'HTML::Forms::Render::WithTT';

   sub _build_tt_include_path { [ 'share/templates' ] }
}

my $form = HTML::Forms->new_with_traits(
   name => 'test_tt', traits => [ 'HTML::Forms::Renderer' ],
);

$form->render;

Description

Configuration and Environment

Defines the following attributes;

  • Mutable booleans defaulting false.

    • did_init_obj - True when the result has been initialised
    • do_form_wrapper - If true wraps the form in a containing element
    • do_label_colon - If true a colon is appended to the label
    • do_label_colon_right - If true place the label colon on the right
    • do_label_right - If true place the label on the right if the field
    • processed - True when the form has been processed
    • render_js_after - If true render the JS after the form
    • use_init_obj_when_no_accessor_in_item - Self describing
    • verbose - When true emits diagnostics on stderr
  • Immutable booleans defaulting false.

    • html_prefix - If true the form name is prepended to field names
    • is_html5 - If true apply HTML5 attributes to fields
    • message_before_start - If true display messages before the form start
    • no_preload - If the true the result is not initialised on build
    • no_widgets - If true widget roles are not applied to the form
    • quote_bind_value - If true quote the bind values in messages
  • action

    URL for the action attribute on the form. A mutable string with no default

  • active

    A mutable array reference of active field names

    Handles; add_active, clear_active, and has_active via the array trait

  • context

    An optional mutable weak reference to the context object with clearer and predicate

  • default_locale

    If context is provided and has a config object use it's locale attribute, otherwise default to en. An immutable lazy string used as the default language in building the locale_handle

  • defaults

    A mutable hash reference of default values keyed by field name. These are applied to the field when the form is setup overriding the default value in the field definition

    Handles; clear_defaults and has_defaults via the hash trait

  • dependency

    A mutable array reference of array references. Each inner reference should contain two or more field names. If the first named field has a value then the subsequent fields are required

  • enctype

    A mutable string without default. Sets the encoding type on the form element

  • error_message

    A mutable string without default with clearer and predicate. This string (if set) is rendered either before or near the start of the form if the form result has_errors or has_form_errors

  • field_traits

    A lazy immutable array reference with an empty default. This list of HTML::Forms::Widget::Field::Trait roles are applied to all fields on the form

    Handles; add_field_trait and has_field_traits via the array trait

  • for_js

    A mutable hash reference with an empty default. Provides support for the Repeatable field type. Keyed by the repeatable field name contains a data structure used by the JS event handlers to add/remove repeatable fields to/from the form. Populated automatically by the Repeatable field type

    Handles; clear_for_js, has_for_js, and set_for_js via the hash trait

  • form_element_attr

  • form_element_class

  • form_wrapper_attr

  • form_wrapper_class

  • form_tags

  • http_method

    An immutable string with a default of post. The method attribute on the form element

  • inactive

  • index

  • info_message

  • init_object

  • language_handle

  • language_handle_class

  • locales

  • messages

  • name

    A mutable string with a random default. The name of the form element

  • no_update

    A mutable bool without default and with a clearer. If set to true the call in process to update the model will be skipped

  • params

    A mutable hash reference with an empty default. Should be set to the keys and values of the form when it is posted back. Parameters are munged by the trigger. See HTML::Forms::Params

    Handles; clear_params, get_param, has_params, and set_param via the hash trait

  • params_args

    An immutable array reference with an empty default. Arguments passed to the HTML::Forms::Params constructor

  • posted

    A mutable boolean without default with clearer and predicate. Should be set to true if the form was posted

  • result

  • style

  • success_message

  • title

  • update_field_list

  • use_defaults_over_obj

  • use_field_for_input_without_param

  • use_init_obj_over_item

  • widget_form

  • widget_name_space

  • widget_wrapper

Subroutines/Methods

Defines the following methods;

  • BUILDARGS

    Additionally allows for construction from either an item object instance or an item_id

  • BUILD

    Applies widget roles, builds the fields, sets the active field list, and initialises the result object. Will also dump the field definitions if verbose is true

    The methods before_build, and after_build are called either side of the above and are dummy methods in this class. Made for overriding in a form role

  • add_form_element_class( @args )

  • add_form_error( @message )

    Pushes the supplied message (after localising) onto form errors. Uses the form is invalid message if one is not supplied

  • add_form_wrapper_class( @args )

  • after_build

    Dummy method called by BUILD

  • after_update_model

  • attributes

    A proxy for form_element_attributes

  • before_build

    Dummy method called at the start of the BUILD method

  • build_active

    Called at build time it clears the inactive status of any active fields and sets the inactive status on any inactive fields

  • build_errors

  • build_language_handle

    Constructor for the language_handle attribute. Will use locales if available otherwise uses the environment variable LANGUAGE_HANDLE. Always appends default_locale to the returned list

  • build_result

  • clear

    Calls all the clearers defined on the form object. Sets processed and did_init_obj to false

  • fif( @args )

  • form

  • form_element_attributes

    Returns a hash reference of keys and values which are applied to the form element

    Also calls html_attributes with a 'type' of 'form_element' returning it's return hash reference if set. Allows for an overridden html_attributes to "fix things up" if required

  • form_wrapper_attributes

  • full_accessor

    Dummy method returns the null string

  • full_name

    Dummy method returns the null string

  • get_default_value

    Dummy method returns nothing

  • get_tag( $name )

  • has_flag( $flag_name )

  • html_attributes( $object, $type, $attrs, $result )

    Dummy method that returns the supplied attrs. Called by form_element_attributes. The type argument is one of; 'element', 'element_wrapper', 'form_element', 'form_wrapper', 'label', or 'wrapper'.

    Applied roles can modify this method to alter the attributes of the above list of form elements

  • init_value( $field, $value )

    Sets both the initial and current field values to the one supplied

  • localise( @message )

    Calls maketext on the language_handle to localise the supplied message. If localisation fails will substitute the placeholder variables and return that string

  • new_with_traits( %args )

    Either a class or object method. Returns a new instance of this class with the list of supplied traits in the args hash applied. This rest of the args hash is supplied to the constructor of the new object

  • process( @args )

  • set_active

  • setup_form( @args )

  • update_field( $field_name, $updates )

  • update_fields

  • validate

  • validate_form

  • values

Diagnostics

Set verbose to true to dump diagnostic information to stderr

Dependencies

Incompatibilities

There are no known incompatibilities in this module

Bugs and Limitations

There are no known bugs in this module. Please report problems to http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-Forms. Patches are welcome

Acknowledgements

Larry Wall - For the Perl programming language

Gerda Shank [email protected] - Author of HTML::FormHandler of which this is a Moo based copy

Author

Peter Flanigan, <[email protected]>

License and Copyright

Copyright (c) 2023 Peter Flanigan. All rights reserved

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic

This program is distributed in the hope that it will be useful, but WITHOUT WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE

About

Generates markup for and processes input from HTML forms

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published