HTML::FormHandler Bryan Beeley Reach Systems www.reachsystems.com
Quick Moose Review Overview Functional Examples Real World Examples Limitations
Quick Moose Review Object Framework Simple Attribute/Accessor Creation Method Modifiers
Object Framework   package MooseOb; use Moose; ... no Moose; package DerivedOb; use Moose; extends 'MooseOb'; ... no Moose...
Moose Simple Attribute/Accessor Creation has 'attribute' => ( # Properties is => 'rw',  # Attribute is read/write isa => '...
Moose Method Modifiers package Base; use Moose; sub method { print “inside
”; } package Derived; use Moose; extends 'Base'...
HTML::FormHandler Overview HFH and MVC Forms Simple Example Object Creation Parameter Processing Validation Failed Validat...
Model View Controller Form? HFH and MVC
Forms <ul><li>Fields </li><ul><li>Type
Name
Value </li><ul><li>Initial
Input
Validated </li></ul></ul></ul>
Forms – Top level object <ul><li>Fields – Objects within a form </li><ul><li>Type – The class of the field object
Name – An attribute of the field object
Value – Attributes of the field object </li><ul><li>Initial – Separate Attribute
Input – Separate Attribute (associted with a result object)
Validated – Separate Attribute </li></ul></ul></ul>
package Form; use HTML::FormHandler::Moose; extends 'HTML::FormHandler'; has_field 'column_name' => ( type => 'Text' ); ha...
package Form; use HTML::FormHandler::Moose; extends 'HTML::FormHandler::Model::DBIC'; use namespace::autoclean; has_field ...
Object Creation <ul><ul><li>No attributes and no model required </li><ul><li>my $form = Form->new; </li></ul></ul><li>Attr...
item => $row_ob </li></ul><li>); </li></ul></ul><ul><li>Dynamic form </li><ul><li>my $form = HTML::FormHandler->new( </li>...
number => { type => 'Integer' }, </li></ul><li>], </li></ul><li>); </li></ul></ul></ul>
Upcoming SlideShare
Loading in …5
×

HTML::FormHandler

4,752 views
4,561 views

Published on

An overview of HTML::FormHandler, a Moose-based form rendering, processing and validation framework for Perl.

Published in: Technology
2 Comments
2 Likes
Statistics
Notes
  • Here is the Catalyst project, including the HTML::FormHandler code, that I used for my functional example:

    http://github.com/boghead/stockgrubber

    Unfortunately I can't post my real-world example because it belongs to my employer.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • I didn't mention it in the presentation, but Gerda Shank is the original author and current maintainer of HTML::FormHandler. She deserves mention here.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
4,752
On SlideShare
0
From Embeds
0
Number of Embeds
38
Actions
Shares
0
Downloads
21
Comments
2
Likes
2
Embeds 0
No embeds

No notes for slide

HTML::FormHandler

  1. 1. HTML::FormHandler Bryan Beeley Reach Systems www.reachsystems.com
  2. 2. Quick Moose Review Overview Functional Examples Real World Examples Limitations
  3. 3. Quick Moose Review Object Framework Simple Attribute/Accessor Creation Method Modifiers
  4. 4. Object Framework package MooseOb; use Moose; ... no Moose; package DerivedOb; use Moose; extends 'MooseOb'; ... no Moose; my $ob = DerivedOb->new; Moose
  5. 5. Moose Simple Attribute/Accessor Creation has 'attribute' => ( # Properties is => 'rw', # Attribute is read/write isa => 'Str', # Indicate a type ); … $obj->attribute('value'); … $val = $obj->attribute();
  6. 6. Moose Method Modifiers package Base; use Moose; sub method { print “inside ”; } package Derived; use Moose; extends 'Base'; before 'method' => sub { print “before ”; }; around 'method' => sub { my $orig = shift; my $self = shift; print “around ”; $self->$orig(@_); # prints inside print “around ”; }; after 'method' => sub { print “after ”; };
  7. 7. HTML::FormHandler Overview HFH and MVC Forms Simple Example Object Creation Parameter Processing Validation Failed Validation Form Rendering Updating the Data Model Repeatable Fields
  8. 8. Model View Controller Form? HFH and MVC
  9. 9. Forms <ul><li>Fields </li><ul><li>Type
  10. 10. Name
  11. 11. Value </li><ul><li>Initial
  12. 12. Input
  13. 13. Validated </li></ul></ul></ul>
  14. 14. Forms – Top level object <ul><li>Fields – Objects within a form </li><ul><li>Type – The class of the field object
  15. 15. Name – An attribute of the field object
  16. 16. Value – Attributes of the field object </li><ul><li>Initial – Separate Attribute
  17. 17. Input – Separate Attribute (associted with a result object)
  18. 18. Validated – Separate Attribute </li></ul></ul></ul>
  19. 19. package Form; use HTML::FormHandler::Moose; extends 'HTML::FormHandler'; has_field 'column_name' => ( type => 'Text' ); has_field 'submit' => ( type => 'Submit' ); no HTML::FormHandler::Moose; … my $form = Form->new( item => $object ); $form->process( $params ); if ($form->validated) { # Form submitted successfully } else { $output = $form->render; }
  20. 20. package Form; use HTML::FormHandler::Moose; extends 'HTML::FormHandler::Model::DBIC'; use namespace::autoclean; has_field 'column_name' => ( type => 'Text' ); has_field 'submit' => ( type => 'Submit' ); __PACKAGE__->make_immutable; … my $form = Form->new( item => $dbic_row_ob ); $form->process( $c->req->parameters ); if ($form->validated) { # Redirect or something } else { my $output = $form->render; $c->stash->{template} = $output; }
  21. 21. Object Creation <ul><ul><li>No attributes and no model required </li><ul><li>my $form = Form->new; </li></ul></ul><li>Attributes and row object </li><ul><ul><li>my $form = Form->new( </li><ul><li>attr => 'val',
  22. 22. item => $row_ob </li></ul><li>); </li></ul></ul><ul><li>Dynamic form </li><ul><li>my $form = HTML::FormHandler->new( </li><ul><li>field_list => [ </li><ul><li>text => { type => 'Text' },
  23. 23. number => { type => 'Integer' }, </li></ul><li>], </li></ul><li>); </li></ul></ul></ul>
  24. 24. $form->process; $form->process( $params ); my $success = $form->process( params => $params, arg => $arg, ); Parses form data out of parameters Saves input data in field objects Validates input data Generates error messages if appropriate Parameter Processing
  25. 25. Validation Default has_field 'number' => ( type => 'Integer', required => 1, ); In your form class has_field 'no_spaces' => ( type => 'Text' ); sub validate_no_spaces { my ($self, $field) = @_; $form->add_error('Must not have spaces') if $field->value =~ /s/; } In your field class sub validate { my $self = shift; ... $self->add_error('My custom message') if $error; }
  26. 26. Failed Validation Check the process return value my $success = $form->process; Add an error message $field->add_error('Error message'); Check a form for errors if ($form->has_errors) { … } Check a field for errors if ($field->has_errors) { … } Get form error messages my @error_messages = $form->errors;
  27. 27. Form Rendering Render into a scalar using HFH widgets my $output = $form->render; Render in a Template Toolkit template using HFH widgets [% form.render %] Let Template Toolkit do the work [% f = form.field('my_text_field') %] Enter some text: <input type=”text” name=”[% f.html_name %]” value=”[% f.html_filter(f.fif) %]” />
  28. 28. Updating the Data Model Use the HFH Model Interface in your form extends 'HTML::FormHandler::Model::DBIC'; Roll your own form update_model method sub update_model { … } Use any object that with accessors matching field names $form->process( item => $object, params => $params );
  29. 29. Repeatable Fields has_field 'order_id' => ( type => 'PosInteger' ); has_field 'cart_items' => ( type => 'Repeatable' ); has_field 'cart_items.cost' => ( type => 'Money' ); has_field 'cart_items.name' => ( type => 'Text' ) ; … JavaScript to dynamically add items to the cart … DBIC extension will submit rows into the related table
  30. 30. Functional Examples
  31. 31. Real World Examples
  32. 32. Default rendering is very primitive HFH DBIC model has problems in some cases Repeatable fields forget initial values new and process do the same thing twice Limitations
  33. 33. Workarounds and Solutions Override render, or create your own widgets Write your own update_model Patches are welcome Cache form objects

×