File Coverage

blib/lib/HTML/FormHandler/Model.pm
Criterion Covered Total %
statement 12 13 92.3
branch n/a
condition n/a
subroutine 9 11 81.8
pod 6 8 75.0
total 27 32 84.3


line stmt bran cond sub pod time code
1             package HTML::FormHandler::Model;
2             # ABSTRACT: default model base class
3             $HTML::FormHandler::Model::VERSION = '0.40068';
4 143     143   103046 use Moose::Role;
  143         390776  
  143         1248  
5 143     143   760633 use Carp;
  143         422  
  143         39315  
6              
7              
8             has 'item' => (
9             is => 'rw',
10             lazy => 1,
11             builder => 'build_item',
12             clearer => 'clear_item',
13             trigger => sub { shift->set_item(@_) }
14             );
15 349     349 1 10048 sub build_item { return }
16              
17             sub set_item {
18 14     14 0 59 my ( $self, $item ) = @_;
19 14         460 $self->item_class( ref $item );
20             }
21              
22              
23             has 'item_id' => (
24             is => 'rw',
25             clearer => 'clear_item_id',
26             trigger => sub { shift->set_item_id(@_) }
27             );
28              
29       2 0   sub set_item_id { }
30              
31              
32             has 'item_class' => (
33             isa => 'Str',
34             is => 'rw',
35             );
36              
37              
38             sub guess_field_type {
39 0     0 1   Carp::confess "Don't know how to determine field type of [$_[1]]";
40             }
41              
42              
43       10 1   sub lookup_options { }
44              
45              
46       167 1   sub validate_model { }
47              
48              
49       0 1   sub clear_model { }
50              
51              
52       97 1   sub update_model { }
53              
54 143     143   1732 use namespace::autoclean;
  143         6468  
  143         1531  
55             1;
56              
57             __END__
58              
59             =pod
60              
61             =encoding UTF-8
62              
63             =head1 NAME
64              
65             HTML::FormHandler::Model - default model base class
66              
67             =head1 VERSION
68              
69             version 0.40068
70              
71             =head1 SYNOPSIS
72              
73             This class defines the base attributes for FormHandler model
74             classes. It is not used directly.
75              
76             =head1 DESCRIPTION
77              
78             This is an empty base class that defines methods called by
79             HTML::FormHandler to support interfacing forms with a data store
80             such as a database.
81              
82             This module provides instructions on methods to override to create
83             a HTML::FormHandler::Model class to work with a specific object relational
84             mapping (ORM) tool.
85              
86             =head1 METHODS
87              
88             =head2 item, build_item
89              
90             The "item" is initialized with "build_item" the first time $form->item is called.
91             "item" must be defined in the model class to fetch the object based on the item id.
92             It should return the item's object. Column values are fetched and updated
93             by calling methods on the returned object.
94              
95             For example, with Class::DBI you might return:
96              
97             return $self->item_class->retrieve( $self->item_id );
98              
99             =head2 item_id
100              
101             The id (primary key) of the item (object) that the form is updating
102             or has just created. The model class should have a build_item method that can
103             fetch the object from the item_class for this id.
104              
105             =head2 item_class
106              
107             "item_class" sets and returns a value used by the model class to access
108             the ORM class related to a form.
109              
110             For example:
111              
112             has '+item_class' => ( default => 'User' );
113              
114             This gives the model class a way to access the data store.
115             If this is not a fixed value (as above) then do not define the
116             method in your subclass and instead set the value when the form
117             is created:
118              
119             my $form = MyApp::Form::Users->new( item_class => $class );
120              
121             The value can be any scalar (or object) needed by the specific ORM
122             to access the data related to the form.
123              
124             A builder for 'item_class' might be to return the class of the 'item'.
125              
126             =head2 guess_field_type
127              
128             Returns the guessed field type. The field name is passed as the first argument.
129             This is only required if using "Auto" type of fields in your form classes.
130             You could override this in your form class, for example, if you use a field
131             naming convention that indicates the field type.
132              
133             The metadata info about the columns can be used to assign types.
134              
135             =head2 lookup_options
136              
137             Retrieve possible options for a given select field from the database.
138             The default method returns undef.
139              
140             Returns an array reference of key/value pairs for the column passed in.
141             These values are used for the values and labels for field types that
142             provide a list of options to select from (e.g. Select, Multiple).
143              
144             A 'Select' type field (or a field that inherits from
145             HTML::FormHandler::Field::Select) can set a number of scalars that control how
146             options are looked up:
147              
148             label_column() - column that holds the label
149             active_column() - column that indicates if a row is acitve
150             sort_column() - column used for sorting the options
151              
152             The default for label_column is "name".
153              
154             =head2 validate_model
155              
156             Validates fields that are dependent on the model.
157             This is called via the validation process and the model class
158             must at least validate "unique" constraints defined in the form
159             class.
160              
161             Any errors on a field found should be set by calling the field's
162             add_error method:
163              
164             $field->add_error('Value must be unique in the database');
165              
166             The default method does nothing.
167              
168             =head2 clear_model
169              
170             Clear out any dynamic data for persistent object
171              
172             =head2 update_model
173              
174             Update the model with validated fields
175              
176             =head1 AUTHOR
177              
178             FormHandler Contributors - see HTML::FormHandler
179              
180             =head1 COPYRIGHT AND LICENSE
181              
182             This software is copyright (c) 2017 by Gerda Shank.
183              
184             This is free software; you can redistribute it and/or modify it under
185             the same terms as the Perl 5 programming language system itself.
186              
187             =cut