MuForm.pm

filter: Statementstmt 86.9% 274 / 315 Branchbran 69.5% 57 / 82 Conditioncond 65.5% 19 / 29 MC/DCmcdc 47.3% 9 / 19 Subroutinesub 86.3% 76 / 88 Podpod 13.8% 10 / 72 totaltotal 73.5% 445 / 605 SCAR 43.9
CC
60
Cov
82%
render_hidden_fields
20.0
after_update_model
13.4
check
12.0
CRAP 80.4
« MuForm/Field/Date.pm ↑ Summary MuForm/Params.pm »
1 package Data::MuForm;
2 # ABSTRACT: Data validator and form processor
3  
4 82 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 82 ▶use Data::MuForm::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 82 with 'Data::MuForm::Model';
8 82 with 'Data::MuForm::Fields';
9 82 with 'Data::MuForm::Common';
10  
11 82 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 82 ▶use Class::Load ('load_optional_class');
Subroutine BEGIN: called CC=1 SCAR=0.0
13 82 ▶use Data::Clone ('data_clone');
Subroutine BEGIN: called CC=1 SCAR=0.0
14 82 ▶use Data::MuForm::Params;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 82 ▶use Data::MuForm::Localizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 82 ▶use MooX::Aliases;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 82 ▶use Data::MuForm::Merge ('merge');
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 82 our $VERSION = '0.04';
20  
21  
22 82 has 'name' => ( is => 'ro', builder => 'build_name');
23 sub build_name {
24 110 ▶ my $self = shift;
Subroutine build_name: called CC=2 SCAR=7.0
Pod undocumented
25 110 my $class = ref $self;
26 110 my ( $name ) = ( $class =~ /.*::(.*)$/ );
27 110 ▶ $name ||= $class;
Condition66%2/3
$name ||= $class
l!l&&r!l&&!r
10550
MC/DC0%0/2
$name ||= $class
$name $class
Truth table66%2/3
A ||= B
A= $nameB= $class
ABresult
1X1
011
000
28 110 return $name;
29 }
30 82 has 'id' => ( is => 'ro', lazy => 1, builder => 'build_id' );
31 1 ▶sub build_id { $_[0]->name }
Subroutine build_id: called CC=1 SCAR=0.0
Pod undocumented
32 82 has 'submitted' => ( is => 'rw', default => undef ); # three values: 0, 1, undef
33 82 has 'processed' => ( is => 'rw', default => 0 );
34 #has 'no_init_process' => ( is => 'rw', default => 0 );
35  
36 82 has 'ran_validation' => ( is => 'rw', default => 0 );
37 133 ▶has '_params' => ( is => 'rw', isa => HashRef, default => sub {{}}, alias => 'data' );
Subroutine __ANON__: called CC=1 SCAR=0.0
38 71 ▶sub clear_params { $_[0]->{_params} = {} }
Subroutine clear_params: called CC=1 SCAR=0.0
Pod undocumented
39 171 ▶sub has_params { my $self = shift; return scalar keys %{$self->{_params}}; }
Subroutine has_params: called CC=1 SCAR=0.0
Pod undocumented
40 sub params {
41 316 ▶ my ( $self, $params ) = @_;
Subroutine params: called CC=2 SCAR=6.9
Pod documented
42 316 ▶ if ( $params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params) 146 170 316
43 146 $params = $self->munge_params($params);
44 146 $self->{_params} = $params;
45 }
46 316 return $self->{_params};
47 }
48 82 has 'field_prefix' => ( is => 'rw' );
49  
50 133 ▶has 'form_meta_fields' => ( is => 'rw', isa => ArrayRef, default => sub {[]} );
Subroutine __ANON__: called CC=1 SCAR=0.0
51 133 ▶has 'index' => ( is => 'rw', isa => HashRef, default => sub {{}} );
Subroutine __ANON__: called CC=1 SCAR=0.0
52 571 ▶sub add_to_index { my ( $self, $field_name, $field ) = @_; $self->{index}->{$field_name} = $field; }
Subroutine add_to_index: called CC=1 SCAR=0.0
Pod undocumented
53 4497 ▶sub form { shift }
Subroutine form: called CC=1 SCAR=0.0
Pod documented
54 591 ▶sub is_form {1}
Subroutine is_form: called CC=1 SCAR=0.0
Pod undocumented
55 ▶sub parent { }
Subroutine parent: called CC=1 SCAR=0.0
Pod undocumented
56 82 has 'ctx' => ( is => 'rw', weak_ref => 1 );
57 # init_values can be a blessed object or a hashref
58 82 has 'init_values' => ( is => 'rw', alias => 'init_object' );
59 71 ▶sub clear_init_values { $_[0]->{init_values} = undef }
Subroutine clear_init_values: called CC=1 SCAR=0.0
Pod undocumented
60 sub has_init_values {
61 23 ▶ my $self = shift;
Subroutine has_init_values: called CC=4 SCAR=14.5
Pod undocumented
62 23 my $init_obj = $self->init_values;
63 23 ▶ return 0 unless defined $init_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $init_obj 16 7 23
64 7 ▶ return 0 if ref $init_obj eq 'HASH' and ! scalar keys %$init_obj;
Branch: 1 branch
BranchTrueFalseTotal
if ref $init_obj eq "HASH" and not scalar keys %$init_obj 0 7 7
Condition33%1/3
ref $init_obj eq "HASH" and not scalar keys %$init_obj
!ll&&!rl&&r
070
MC/DC0%0/2
ref $init_obj eq "HASH" and not scalar keys %$init_obj
ref $init_obj eq "HASH" not scalar keys %$init_obj
Truth table33%1/3
A and not(B)
A= ref $init_obj eq "HASH"B= not scalar keys %$init_obj
ABresult
0X0
100
111
65 7 return 1;
66 }
67 82 has 'fill_from_object_source' => ( is => 'rw', );
68 82 has 'active' => ( is => 'rw', clearer => 'clear_active', predicate => 'has_active' );
69 82 has 'inactive' => ( is => 'rw', clearer => 'clear_inactive', predicate => 'has_inactive' );
70 1468 ▶sub full_name { '' }
Subroutine full_name: called CC=1 SCAR=0.0
Pod undocumented
71 15 ▶sub full_accessor { '' }
Subroutine full_accessor: called CC=1 SCAR=0.0
Pod undocumented
72 58 ▶sub fif { shift->fields_fif(@_) }
Subroutine fif: called CC=1 SCAR=0.0
Pod documented
73  
74 has '_repeatable_fields' => (
75 is => 'rw',
76 133 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
77 82 );
78 sub add_repeatable_field {
79 35 ▶ my ( $self, $field ) = @_;
Subroutine add_repeatable_field: called CC=1 SCAR=0.0
Pod undocumented
80 35 push @{$self->_repeatable_fields}, $field;
81 }
82 sub has_repeatable_fields {
83 64 ▶ my ( $self, $field ) = @_;
Subroutine has_repeatable_fields: called CC=1 SCAR=0.0
Pod undocumented
84 64 return scalar @{$self->_repeatable_fields};
85 }
86 sub all_repeatable_fields {
87 3 ▶ my $self = shift;
Subroutine all_repeatable_fields: called CC=1 SCAR=0.0
Pod undocumented
88 3 return @{$self->_repeatable_fields};
89 }
90  
91 #========= Rendering ==========
92 82 has 'http_method' => ( is => 'ro', default => 'post' );
93 82 has 'action' => ( is => 'rw' );
94 82 has 'enctype' => ( is => 'rw' );
95 82 has 'renderer_class' => ( is => 'ro', builder => 'build_renderer_class' );
96 133 ▶sub build_renderer_class { 'Data::MuForm::Renderer::Base' }
Subroutine build_renderer_class: called CC=1 SCAR=0.0
Pod undocumented
97 82 has 'renderer' => ( is => 'rw', lazy => 1, builder => 'build_renderer' );
98 sub build_renderer {
99 133 ▶ my $self = shift;
Subroutine build_renderer: called CC=2 SCAR=7.0
Pod undocumented
100 133 ▶ my $renderer_class = load_optional_class($self->renderer_class) ? $self->renderer_class : 'Data::MuForm::Renderer::Base';
Branch: 1 branch
BranchTrueFalseTotal
load_optional_class($self->renderer_class) ? : 133 0 133
101 my $renderer = $renderer_class->new(
102 localizer => $self->localizer,
103 form => $self->form,
104 133 %{$self->renderer_args},
105 );
106 133 return $renderer;
107 }
108 82 has 'renderer_args' => ( is => 'ro', isa => HashRef, builder => 'build_renderer_args' );
109 130 ▶sub build_renderer_args {{}}
Subroutine build_renderer_args: called CC=1 SCAR=0.0
Pod undocumented
110 82 has 'render_args' => ( is => 'rw', lazy => 1, isa => HashRef, builder => 'build_render_args' );
111 7 ▶sub build_render_args {{}}
Subroutine build_render_args: called CC=1 SCAR=0.0
Pod undocumented
112 sub base_render_args {
113 7 ▶ my $self = shift;
Subroutine base_render_args: called CC=4 SCAR=14.6
Pod undocumented
114 7 ▶ my $args = {
Condition50%1/2
$self->form_errors || []
l!l
70
MC/DC0%0/1
$self->form_errors || []
$self->form_errors
Truth table50%1/2
A
A= $self->form_errors
Aresult
11
00
115 name => $self->name,
116 id => $self->name,
117 form_errors => $self->form_errors || [],
118 form_attr => {
119 method => $self->http_method,
120 }
121 };
122 7 ▶ $args->{form_attr}->{action} = $self->action if $self->action;
Branch: 1 branch
BranchTrueFalseTotal
if $self->action 0 7 7
123 7 ▶ $args->{form_attr}->{enctype} = $self->enctype if $self->enctype;
Branch: 1 branch
BranchTrueFalseTotal
if $self->enctype 0 7 7
124 7 return $args;
125 }
126  
127  
128 #========= Errors ==========
129 133 ▶has 'form_errors' => ( is => 'rw', isa => ArrayRef, default => sub {[]} );
Subroutine __ANON__: called CC=1 SCAR=0.0
130 71 ▶sub clear_form_errors { $_[0]->{form_errors} = []; }
Subroutine clear_form_errors: called CC=1 SCAR=0.0
Pod undocumented
131 5 ▶sub all_form_errors { return @{$_[0]->form_errors}; }
Subroutine all_form_errors: called CC=1 SCAR=0.0
Pod undocumented
132 5 ▶sub has_form_errors { scalar @{$_[0]->form_errors} }
Subroutine has_form_errors: called CC=1 SCAR=0.0
Pod undocumented
133 7 ▶sub num_form_errors { scalar @{$_[0]->form_errors} }
Subroutine num_form_errors: called CC=1 SCAR=0.0
Pod undocumented
134 0 ▶sub push_form_error { push @{$_[0]->form_errors}, $_[1] }
Subroutine push_form_error: not called CC=1 SCAR=6.9
Pod undocumented
135 sub add_form_error {
136 0 ▶ my ( $self, @message ) = @_;
Subroutine add_form_error: not called CC=2 SCAR=17.9
Pod undocumented
137 0 my $out;
138 0 ▶ if ( $message[0] !~ /{/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $message[0] =~ /{/) { } 0 0 0
139 0 $out = $self->localizer->loc_($message[0]);
140 }
141 else {
142 0 $out = $self->localizer->loc_x(@message);
143 }
144 0 return $self->push_form_error($out);
145 }
146  
147 sub has_errors {
148 16 ▶ my $self = shift;
Subroutine has_errors: called CC=2 SCAR=7.1
Pod undocumented
149 16 ▶ return $self->has_error_fields || $self->has_form_errors;
Condition66%2/3
$self->has_error_fields || $self->has_form_errors
l!l&&r!l&&!r
1204
MC/DC50%1/2
$self->has_error_fields || $self->has_form_errors
$self->has_error_fields $self->has_form_errors
Truth table66%2/3
A || B
A= $self->has_error_fieldsB= $self->has_form_errors
ABresult
1X1
011
000
150 }
151 sub num_errors {
152 7 ▶ my $self = shift;
Subroutine num_errors: called CC=1 SCAR=0.0
Pod undocumented
153 7 return $self->num_error_fields + $self->num_form_errors;
154 }
155 0 ▶sub get_errors { shift->errors }
Subroutine get_errors: not called CC=1 SCAR=6.9
Pod undocumented
156  
157 sub all_errors {
158 5 ▶ my $self = shift;
Subroutine all_errors: called CC=1 SCAR=0.0
Pod undocumented
159 5 my @errors = $self->all_form_errors;
160 5 push @errors, map { $_->all_errors } $self->all_error_fields;
161 5 return @errors;
162 }
163 2 ▶sub errors { [$_[0]->all_errors] }
Subroutine errors: called CC=1 SCAR=0.0
Pod documented
164  
165 sub errors_by_id {
166 1 ▶ my $self = shift;
Subroutine errors_by_id: called CC=2 SCAR=6.9
Pod undocumented
167 1 my %errors;
168 1 $errors{$_->id} = [$_->all_errors] for $self->all_error_fields;
169 1 return \%errors;
170 }
171  
172 sub errors_by_name {
173 1 ▶ my $self = shift;
Subroutine errors_by_name: called CC=2 SCAR=6.9
Pod undocumented
174 1 my %errors;
175 1 $errors{$_->prefixed_name} = [$_->all_errors] for $self->all_error_fields;
176 1 return \%errors;
177 }
178  
179 #========= Localization ==========
180  
181 82 has 'language' => ( is => 'rw', builder => 'build_language' );
182 133 ▶sub build_language { 'en' }
Subroutine build_language: called CC=1 SCAR=0.0
Pod undocumented
183 82 has 'localizer' => ( is => 'rw', lazy => 1, builder => 'build_localizer' );
184 sub build_localizer {
185 133 ▶ my $self = shift;
Subroutine build_localizer: called CC=1 SCAR=0.0
Pod undocumented
186 133 return Data::MuForm::Localizer->new(
187 language => $self->language,
188 );
189 }
190  
191 #========= Messages ==========
192 82 has 'messages' => ( is => 'rw', isa => HashRef, builder => 'build_messages' );
193 132 ▶sub build_messages {{}}
Subroutine build_messages: called CC=1 SCAR=0.0
Pod undocumented
194 1 ▶sub _get_form_message { my ($self, $msgname) = @_; return $self->messages->{$msgname}; }
Subroutine _get_form_message: called CC=1 SCAR=0.0
195 38 ▶sub _has_form_message { my ($self, $msgname) = @_; return exists $self->messages->{$msgname}; }
Subroutine _has_form_message: called CC=1 SCAR=0.0
196 0 ▶sub set_message { my ( $self, $msgname, $msg) = @_; $self->messages->{$msgname} = $msg; }
Subroutine set_message: not called CC=1 SCAR=6.9
Pod undocumented
197 82 my $class_messages = {};
198 sub get_class_messages {
199 0 ▶ return $class_messages;
Subroutine get_class_messages: not called CC=1 SCAR=6.9
Pod undocumented
200 }
201 sub get_message {
202 0 ▶ my ( $self, $msg ) = @_;
Subroutine get_message: not called CC=2 SCAR=17.9
Pod undocumented
203 0 ▶ return $self->_get_form_message($msg) if $self->_has_form_message($msg);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_form_message($msg) 0 0 0
204 0 return $self->get_class_messages->{$msg};
205 }
206 sub all_messages {
207 0 ▶ my $self = shift;
Subroutine all_messages: not called CC=1 SCAR=6.9
Pod undocumented
208 0 return { %{$self->get_class_messages}, %{$self->messages} };
209 }
210  
211  
212 #========= Methods ==========
213 sub BUILD {
214 133 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
215  
216 # instantiate
217 133 $self->localizer;
218 133 $self->renderer;
219 133 $self->build_fields;
220 132 $self->after_build_fields;
221 # put various defaults into fields. Should this happen?
222 # it will be re-done on the first process call
223 132 $self->fill_values;
224 }
225  
226 sub process {
227 170 ▶ my $self = shift;
Subroutine process: called CC=5 SCAR=16.1
Pod documented
228  
229 170 ▶ $self->clear if $self->processed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->processed 70 100 170
230 170 $self->setup(@_);
231 170 $self->after_setup;
232 170 ▶ $self->validate_form if $self->submitted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->submitted 103 67 170
233  
234 170 ▶ $self->update_model if ( $self->validated );
Branch: 1 branch
BranchTrueFalseTotal
if $self->validated 64 106 170
235 170 ▶ $self->after_update_model if ( $self->validated );
Branch: 1 branch
BranchTrueFalseTotal
if $self->validated 64 106 170
236  
237 170 $self->processed(1);
238 170 return $self->validated;
239 }
240  
241 sub check {
242 0 ▶ my $self = shift;
Subroutine check: not called CC=3 SCAR=24.8
Pod documented
243 0 ▶ $self->clear if $self->processed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->processed 0 0 0
244 0 $self->setup(@_);
245 0 $self->after_setup;
246 0 ▶ $self->validate_form if $self->submitted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->submitted 0 0 0
247 0 $self->processed(1);
248 0 return $self->check_result;
249 }
250  
251 sub check_result {
252 0 ▶ my $self = shift;
Subroutine check_result: not called CC=1 SCAR=6.9
Pod undocumented
253 0 return $self->validated;
254 }
255  
256 sub clear {
257 71 ▶ my $self = shift;
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
258 71 $self->clear_params;
259 71 $self->clear_filled_from;
260 71 $self->submitted(undef);
261 71 $self->model(undef);
262 71 $self->clear_init_values;
263 71 $self->fill_from_object_source(undef);
264 71 $self->ctx(undef);
265 71 $self->processed(0);
266 71 $self->ran_validation(0);
267  
268 # this will recursively clear field data
269 71 $self->clear_data;
270 71 $self->clear_form_errors;
271 71 $self->clear_error_fields;
272 }
273  
274  
275 sub setup {
276 170 ▶ my ( $self, @args ) = @_;
Subroutine setup: called CC=9 SCAR=22.0
Pod documented
277  
278 170 ▶ if ( @args == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1) { } 38 132 170
elsif (@args > 1) { } 116 16 132
279 38 $self->params( $args[0] );
280 }
281 elsif ( @args > 1 ) {
282 116 my $hashref = {@args};
283 116 ▶ while ( my ( $key, $value ) = each %{$hashref} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$hashref;}) 164 116 280
284 164 ▶ warn "invalid attribute '$key' passed to setup_form"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($key) 0 164 164
285 unless $self->can($key);
286 164 $self->$key($value);
287 }
288 }
289 # set_active
290 170 $self->set_active;
291  
292 # customization hook
293 170 $self->in_setup;
294  
295 # set the submitted flag
296 170 ▶ $self->submitted(1) if ( $self->has_params && ! defined $self->submitted );
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_params and not defined $self->submitted 103 67 170
Condition100%3/3
$self->has_params and not defined $self->submitted
!ll&&!rl&&r
661103
MC/DC100%2/2
$self->has_params and not defined $self->submitted
$self->has_params not defined $self->submitted
Truth table100%3/3
A and not(B)
A= $self->has_paramsB= not defined $self->submitted
ABresult
0X0
100
111
297  
298 # fill the 'value' attributes from model, init_values or fields
299 170 $self->fill_values;
300  
301 # fill in the input attribute
302 170 my $params = data_clone( $self->params );
303 170 ▶ if ( $self->submitted ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->submitted) 105 65 170
304 105 $self->fill_from_params($params, 1 );
305 # if the params submitted don't match fields, it shouldn't count as 'submitted'
306 105 ▶ if ( ! scalar keys %{$self->input} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not scalar keys %{$self->input;}) 2 103 105
307 2 $self->submitted(0);
308 }
309 }
310  
311 }
312  
313 sub fill_values {
314 302 ▶ my $self = shift;
Subroutine fill_values: called CC=5 SCAR=16.1
Pod undocumented
315  
316 # these fill the 'value' attributes
317 302 ▶ if ( $self->model && $self->use_model_for_defaults ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->model and $self->use_model_for_defaults) { } 9 293 302
elsif ($self->init_values) { } 41 252 293
elsif (not $self->submitted) { } 157 95 252
Condition66%2/3
$self->model and $self->use_model_for_defaults
!ll&&!rl&&r
29309
MC/DC50%1/2
$self->model and $self->use_model_for_defaults
$self->model $self->use_model_for_defaults
Truth table66%2/3
A and B
A= $self->modelB= $self->use_model_for_defaults
ABresult
0X0
100
111
318 9 $self->fill_from_object_source('model');
319 9 $self->fill_from_object($self->model);
320 }
321 elsif ( $self->init_values ) {
322 41 $self->fill_from_object_source('init_values');
323 41 $self->fill_from_object($self->init_values );
324 }
325 elsif ( !$self->submitted ) {
326 # no initial object. empty form must be initialized
327 157 $self->fill_from_fields;
328 }
329 }
330  
331 ▶sub in_setup { }
Subroutine in_setup: called CC=1 SCAR=0.0
Pod undocumented
332 ▶sub after_setup { }
Subroutine after_setup: called CC=1 SCAR=0.0
Pod undocumented
333 ▶sub after_build_fields { }
Subroutine after_build_fields: called CC=1 SCAR=0.0
Pod undocumented
334  
335 sub update_model {
336 62 ▶ my $self = shift;
Subroutine update_model: called CC=1 SCAR=0.0
Pod undocumented
337 }
338  
339  
340 sub munge_params {
341 146 ▶ my ( $self, $params, $attr ) = @_;
Subroutine munge_params: called CC=3 SCAR=11.6
Pod undocumented
342  
343 146 my $_fix_params = Data::MuForm::Params->new;
344 146 my $new_params = $_fix_params->expand_hash($params);
345 146 ▶ if ( $self->field_prefix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->field_prefix) 0 146 146
346 0 $new_params = $new_params->{ $self->field_prefix };
347 }
348 146 ▶ $new_params = {} if !defined $new_params;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_params 0 146 146
349 146 return $new_params;
350 }
351  
352 sub set_active {
353 170 ▶ my $self = shift;
Subroutine set_active: called CC=7 SCAR=19.7
Pod undocumented
354 170 ▶ if( $self->has_active ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_active) 1 169 170
355 1 foreach my $fname (@{$self->active}) {
356 1 my $field = $self->field($fname);
357 1 ▶ if ( $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) { } 1 0 1
358 1 $field->_active(1);
359 }
360 else {
361 0 warn "field $fname not found to set active";
362 }
363 }
364 1 $self->clear_active;
365 }
366 170 ▶ if( $self->has_inactive ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_inactive) 2 168 170
367 2 foreach my $fname (@{$self->inactive}) {
368 2 my $field = $self->field($fname);
369 2 ▶ if ( $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) { } 2 0 2
370 2 $field->_active(0);
371 }
372 else {
373 0 warn "field $fname not found to set inactive";
374 }
375 }
376 2 $self->clear_inactive;
377 }
378 }
379  
380 #====================================================================
381 # Validation
382 #====================================================================
383  
384 sub validate_form {
385 103 ▶ my $self = shift;
Subroutine validate_form: called CC=1 SCAR=0.0
Pod undocumented
386  
387 103 $self->fields_validate;
388 103 $self->validate; # hook
389 103 $self->validate_model; # hook
390 103 $self->fields_set_value;
391 103 $self->submitted(undef);
392 103 $self->ran_validation(1);
393 }
394  
395 # hook for child forms
396 ▶sub validate { }
Subroutine validate: called CC=1 SCAR=0.0
Pod documented
397  
398 # hook for model validation
399 ▶sub validate_model { }
Subroutine validate_model: called CC=1 SCAR=0.0
Pod undocumented
400  
401 567 ▶sub validated { my $self = shift; return $self->ran_validation && ! $self->has_error_fields; }
Subroutine validated: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
$self->ran_validation && !$self->has_error_fields
!ll&&!rl&&r
208218141
MC/DC100%2/2
$self->ran_validation && !$self->has_error_fields
$self->ran_validation !$self->has_error_fields
Truth table100%3/3
A && not(B)
A= $self->ran_validationB= !$self->has_error_fields
ABresult
0X0
100
111
402  
403 ▶sub get_default_value { }
Subroutine get_default_value: called CC=1 SCAR=0.0
Pod undocumented
404  
405 sub transform_and_set_input {
406 105 ▶ my ($self, $input) = @_;
Subroutine transform_and_set_input: called CC=1 SCAR=0.0
Pod undocumented
407 105 $self->input($input);
408 }
409  
410 sub get_result {
411 1 ▶ my $self = shift;
Subroutine get_result: called CC=3 SCAR=11.3
Pod undocumented
412 1 my $result = {
413 method => $self->http_method,
414 action => $self->action,
415 name => $self->name,
416 id => $self->id,
417 submitted => $self->submitted,
418 validated => $self->validated,
419 };
420 1 ▶ $result->{form_errors} = $self->form_errors if $self->has_form_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_form_errors 0 1 1
421 1 ▶ $result->{errors} = $self->errors if $self->has_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_errors 0 1 1
422 1 return $result;
423 }
424  
425 1 ▶sub results { shift->fields_get_results }
Subroutine results: called CC=1 SCAR=0.0
Pod undocumented
426  
427 sub add_field {
428 2 ▶ my ( $self, %field_attr ) = @_;
Subroutine add_field: called CC=2 SCAR=6.9
Pod documented
429 2 my $field = $self->_make_field( \%field_attr );
430 # make it the last field.
431 2 ▶ unless ( exists $field_attr{order} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $field_attr{"order"}) 1 1 2
432 1 my $order = $field->parent->_get_highest_field_order;
433 1 $field->order($order + 5);
434 }
435 2 return $field;
436 }
437  
438 sub after_update_model {
439 64 ▶ my $self = shift;
Subroutine after_update_model: called CC=13 SCAR=25.9
Pod undocumented
440 # This an attempt to reload the repeatable
441 # relationships after the database is updated, so that we get the
442 # primary keys of the repeatable elements. Otherwise, if a form
443 # is re-presented, repeatable elements without primary keys may
444 # be created again. There is no reliable way to connect up
445 # existing repeatable elements with their db-created primary keys.
446 64 ▶ if ( $self->has_repeatable_fields && $self->model ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_repeatable_fields and $self->model) 2 62 64
Condition100%3/3
$self->has_repeatable_fields and $self->model
!ll&&!rl&&r
49132
MC/DC100%2/2
$self->has_repeatable_fields and $self->model
$self->has_repeatable_fields $self->model
Truth table100%3/3
A and B
A= $self->has_repeatable_fieldsB= $self->model
ABresult
0X0
100
111
447 2 foreach my $field ( $self->all_repeatable_fields ) {
448 4 ▶ next unless $field->is_active;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->is_active 0 4 4
449 # Check to see if there are any repeatable subfields with
450 # null primary keys, so we can skip reloading for the case
451 # where all repeatables have primary keys.
452 4 my $needs_reload = 0;
453 4 foreach my $sub_field ( $field->all_fields ) {
454 2 ▶ if ( $sub_field->is_compound && $sub_field->has_primary_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_field->is_compound and $sub_field->has_primary_key) 1 1 2
Condition66%2/3
$sub_field->is_compound and $sub_field->has_primary_key
!ll&&!rl&&r
011
MC/DC50%1/2
$sub_field->is_compound and $sub_field->has_primary_key
$sub_field->is_compound $sub_field->has_primary_key
Truth table66%2/3
A and B
A= $sub_field->is_compoundB= $sub_field->has_primary_key
ABresult
0X0
100
111
455 1 foreach my $pk_field ( @{ $sub_field->primary_key } ) {
456 1 ▶ $needs_reload++ unless $pk_field->fif;
Branch: 1 branch
BranchTrueFalseTotal
unless $pk_field->fif 1 0 1
457 }
458 1 ▶ last if $needs_reload;
Branch: 1 branch
BranchTrueFalseTotal
if $needs_reload 1 0 1
459 }
460 }
461 4 ▶ next unless $needs_reload;
Branch: 1 branch
BranchTrueFalseTotal
unless $needs_reload 3 1 4
462 1 my @names = split( /\./, $field->full_name );
463 1 my $rep_model = $self->find_sub_obj( $self->model, \@names );
464 # $rep_model is a single row or an array of rows or undef
465 # If we found a database model for the repeatable, initialize
466 # with 'fill_from_object'
467 1 ▶ if ( ref $rep_model ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rep_model) 1 0 1
468 1 my $parent = $field->parent;
469 1 $field->init_state;
470 1 $field->fill_from_object( $rep_model );
471 }
472 }
473 }
474 }
475  
476 # model for render_hook in forms to modify render_args for rendering
477 sub render_hook {
478 213 ▶ my ( $self, $renderer, $rargs ) = @_;
Subroutine render_hook: called CC=1 SCAR=0.0
Pod undocumented
479 }
480  
481 sub get_render_args {
482 7 ▶ my ( $self, %args ) = @_;
Subroutine get_render_args: called CC=1 SCAR=0.0
Pod undocumented
483 7 my $render_args = merge( $self->base_render_args, $self->render_args );
484 7 $render_args = merge( $render_args, \%args );
485 7 return $render_args;
486 }
487  
488 sub render {
489 6 ▶ my ( $self, $rargs ) = @_;
Subroutine render: called CC=1 SCAR=0.0
Pod undocumented
490 6 my $render_args = $self->get_render_args(%$rargs, rendering => 'form');
491 6 return $self->renderer->render_form($render_args, $self->sorted_fields);
492 }
493  
494 sub render_start {
495 1 ▶ my ( $self, $rargs ) = @_;
Subroutine render_start: called CC=1 SCAR=0.0
Pod undocumented
496 1 my $render_args = $self->get_render_args( form_attr => $rargs, rendering => 'form_start');
497 1 return $self->renderer->render_start($render_args);
498 }
499  
500 sub render_end {
501 0 ▶ my ( $self, $rargs ) = @_;
Subroutine render_end: not called CC=1 SCAR=6.9
Pod undocumented
502 # is there any need for attributes on a form end tag? never seen any...
503 0 my $render_args = $self->get_render_args(rendering => 'form_end');
504 0 return $self->renderer->render_end($render_args);
505 }
506  
507 sub render_errors {
508 0 ▶ my ( $self, $rargs ) = @_;
Subroutine render_errors: not called CC=1 SCAR=6.9
Pod undocumented
509 # we're not doing 'form_error_attr'. only processing 'error_tag' and 'error_class'
510 0 my $render_args = $self->get_render_args( %$rargs, rendering => 'form_errors');
511 0 return $self->renderer->render_form_errors($render_args);
512 }
513  
514 # just for top level fields
515 sub render_hidden_fields {
516 0 ▶ my $self = shift;
Subroutine render_hidden_fields: not called CC=4 SCAR=30.0
Pod undocumented
517 0 foreach my $field ( $self->all_sorted_fields ) {
518 0 ▶ if ( $field->type eq 'Hidden' && $field->has_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->type eq "Hidden" and $field->has_value) 0 0 0
Condition0%0/3
$field->type eq "Hidden" and $field->has_value
!ll&&!rl&&r
000
MC/DC0%0/2
$field->type eq "Hidden" and $field->has_value
$field->type eq "Hidden" $field->has_value
Truth table0%0/3
A and B
A= $field->type eq "Hidden"B= $field->has_value
ABresult
0X0
100
111
519 0 $field->render_element;
520 }
521 }
522 }
523  
524 82 1;
525  
526 __END__
527  
528 =pod
529  
530 =encoding UTF-8
531  
532 =head1 NAME
533  
534 Data::MuForm - Data validator and form processor
535  
536 =head1 VERSION
537  
538 version 0.04
539  
540 =head1 SYNOPSIS
541  
542 Moo conversion of HTML::FormHandler, with more emphasis on data
543 validation and a new way of rendering. The core behavior is basically
544 the same as FormHandler, but some names have been changed, some
545 functionality removed, some added. It will be necessary to change
546 your forms to convert to MuForm, but it shouldn't be difficult.
547  
548 See the manual at L<Data::MuForm::Manual>, and in particular
549 L<Data::MuForm::Manual::FormHandlerDiff> if you're already using
550 FormHandler.
551  
552 my $validator = MyApp::Form::Test->new;
553 $validator->check( data => $params );
554 or
555 my $form = MyApp::Form::User->new;
556 $form->process( model => $row, params => $params );
557 my $rendered_form = $form->render;
558 if( $form->validated ) {
559 # perform validated form actions
560 }
561 else {
562 # perform non-validated actions
563 }
564  
565 An example of a custom form class:
566  
567 package MyApp::Form::User;
568 use Moo;
569 use Data::MuForm::Meta;
570 extends 'Data::MuForm';
571  
572 has_field 'name' => ( type => 'Text' );
573 has_field 'age' => ( type => 'Integer', apply => [ MinimumAge ] );
574 has_field 'hobbies' => ( type => 'Multiple' );
575 has_field 'address' => ( type => 'Compound' );
576 has_field 'address.city' => ( type => 'Text' );
577 has_field 'address.state' => ( type => 'Select' );
578 has_field 'email' => ( type => 'Email' );
579  
580 A dynamic form - one that does not use a custom form class - may be
581 created using the 'field_list' attribute to set fields:
582  
583 my $validator = Data::MuForm->new(
584 name => 'login_form',
585 field_list => [
586 'username' => {
587 type => 'Text',
588 apply => [ { check => qr/^[0-9a-z]*\z/,
589 message => 'Contains invalid characters' } ],
590 },
591 'password' => { apply => [ Password ] },
592 ],
593 );
594  
595 =head1 DESCRIPTION
596  
597 This documentation is mainly of Data::MuForm class attributes and methods.
598 For general-purpose documentation see L<Data::MuForm::Manual>.
599  
600 =head1 NAME
601  
602 Data::MuForm
603  
604 =head1 ATTRIBUTES and METHODS
605  
606 =head2 Creating a form with 'new'
607  
608 The new constructor takes name/value pairs:
609  
610 MyForm->new( action => $action );
611  
612 No attributes are required on new. Normally you would pass in persistent
613 attributes on 'new' (such as 'schema') and request/process related attributes on 'process'.
614  
615 The form's fields will be built from the form definitions on new.
616  
617 The 'field_list' is passed in on 'new' because fields are build at construction time.
618 It can also be a method in the form class.
619  
620 field_list - an array of field definitions
621  
622 =head2 process & check
623  
624 Data to be validated/processed:
625  
626 data $validator->check( data => {} );
627 params $form->process( params => {} );
628  
629 The 'init_values' hashref can be passed in, but can also be set in the form class:
630  
631 init_values - a hashref or object to provide initial values
632  
633 Passed in on process with a DBIC model MuForm class:
634  
635 model - database row object
636  
637 See the model class for more information about 'model', 'model_id',
638 'model_class', and 'schema' (for the DBIC model).
639 L<Data::MuForm::Model::DBIC>.
640  
641 =head2 Processing
642  
643 =head3 check
644  
645 Use the 'check' method to perform validation on your data:
646  
647 my $result = $validator->check( data => { ... } );
648  
649 The 'result' returned from 'check' is $form->validated, but you can
650 override the 'check_result' method in the form to return data in
651 whatever format you wish.
652  
653 =head3 process
654  
655 Call the 'process' method on your form to perform validation and
656 update. A database form must have either a model (row object) or
657 a schema, model_id (row primary key), and model_class (usually set in the form).
658 A non-database form requires only parameters.
659  
660 $form->process( model => $book, params => $c->req->parameters );
661 $form->process( model_id => $model_id,
662 schema => $schema, params => $c->req->parameters );
663 $form->process( params => $c->req->parameters );
664  
665 This process method returns the 'validated' flag (C<< $form->validated >>).
666 If it is a database form and the form validates, the database row
667 will be updated.
668  
669 After the form has been processed, you can get a parameter hashref suitable
670 for using to fill in the form from C<< $form->fif >>.
671 A hash of inflated values (that would be used to update the database for
672 a database form) can be retrieved with C<< $form->value >>.
673  
674 If you don't want to update the database, you can use the 'check' method instead.
675  
676 =head3 params
677  
678 Parameters are passed in when you call 'process'.
679 HFH gets data to validate and store in the database from the params hash.
680 If the params hash is empty, no validation is done, so it is not necessary
681 to check for POST before calling C<< $form->process >>. (Although see
682 the 'posted' option for complications.)
683  
684 Params can either be in the form of CGI/HTTP style params:
685  
686 {
687 user_name => "Joe Smith",
688 occupation => "Programmer",
689 'addresses.0.street' => "999 Main Street",
690 'addresses.0.city' => "Podunk",
691 'addresses.0.country' => "UT",
692 'addresses.0.address_id' => "1",
693 'addresses.1.street' => "333 Valencia Street",
694 'addresses.1.city' => "San Francisco",
695 'addresses.1.country' => "UT",
696 'addresses.1.address_id' => "2",
697 }
698  
699 or as structured data in the form of hashes and lists:
700  
701 {
702 addresses => [
703 {
704 city => 'Middle City',
705 country => 'GK',
706 address_id => 1,
707 street => '101 Main St',
708 },
709 {
710 city => 'DownTown',
711 country => 'UT',
712 address_id => 2,
713 street => '99 Elm St',
714 },
715 ],
716 'occupation' => 'management',
717 'user_name' => 'jdoe',
718 }
719  
720 CGI style parameters will be converted to hashes and lists for HFH to
721 operate on.
722  
723 =head3 submitted
724  
725 Note that MuForm by default uses empty params as a signal that the
726 form has not actually been submitted, and so will not attempt to validate
727 a form with empty params. Most of the time this works OK, but if you
728 have a small form with only the controls that do not return a post
729 parameter if unselected (checkboxes and select lists), then the form
730 will not be validated if everything is unselected. For this case you
731 can either add a hidden field as an 'indicator', or use the 'submitted' flag:
732  
733 $form->process( submitted => ($c->req->method eq 'POST'), params => ... );
734  
735 The 'submitted' flag also works to prevent validation from being performed
736 if there are extra params in the params hash and it is not a 'POST' request.
737  
738 =head2 Getting data out
739  
740 =head3 fif (fill in form)
741  
742 If you don't use MuForm rendering and want to fill your form values in
743 using some other method (such as with HTML::FillInForm or using a template)
744 this returns a hash of values that are equivalent to params which you may
745 use to fill in your form.
746  
747 The fif value for a 'title' field in a TT form:
748  
749 [% form.fif.title %]
750  
751 Or you can use the 'fif' method on individual fields:
752  
753 [% form.field('title').fif %]
754  
755 If you use MuForm to render your forms or field you probably won't use
756 these methods.
757  
758 =head3 value
759  
760 Returns a hashref of all field values. Useful for non-database forms, or if
761 you want to update the database yourself. The 'fif' method returns
762 a hashref with the field names for the keys and the field's 'fif' for the
763 values; 'value' returns a hashref with the field accessors for the keys, and the
764 field's 'value' (possibly inflated) for the values.
765  
766 Forms containing arrays to be processed with L<Data::MuForm::Field::Repeatable>
767 will have parameters with dots and numbers, like 'addresses.0.city', while the
768 values hash will transform the fields with numbers to arrays.
769  
770 =head2 Accessing and setting up fields
771  
772 Fields are declared with a number of attributes which are defined in
773 L<Data::MuForm::Field>. If you want additional attributes you can
774 define your own field classes (or apply a role to a field class - see
775 L<Data::MuForm::Manual::Cookbook>). The field 'type' (used in field
776 definitions) is the short class name of the field class, used when
777 searching the 'field_namespace' for the field class.
778  
779 =head3 has_field
780  
781 The most common way of declaring fields is the 'has_field' syntax.
782 Using the 'has_field' syntax sugar requires C< use Data::MuForm::Meta; >.
783 See L<Data::MuForm::Manual::Intro>
784  
785 use Moo;
786 use Data::MuForm::Meta;
787 has_field 'field_name' => ( type => 'FieldClass', .... );
788  
789 =head3 field_list
790  
791 A 'field_list' is an array of field definitions which can be used as an
792 alternative to 'has_field' in small, dynamic forms to create fields.
793  
794 field_list => [
795 field_one => {
796 type => 'Text',
797 required => 1
798 },
799 field_two => 'Text,
800 ]
801  
802 The field_list array takes elements which are either a field_name key
803 pointing to a 'type' string or a field_name key pointing to a
804 hashref of field attributes. You can also provide an array of
805 hashref elements with the name as an additional attribute.
806 The field list can be set inside a form class, when you want to
807 add fields to the form depending on some other state, although
808 you can also create all the fields and set some of them inactive.
809  
810 sub field_list {
811 my $self = shift;
812 my $fields = $self->schema->resultset('SomeTable')->
813 search({user_id => $self->user_id, .... });
814 my @field_list;
815 while ( my $field = $fields->next )
816 {
817 < create field list >
818 }
819 return \@field_list;
820 }
821  
822 =head2 add_field
823  
824 You can add an additional field with $form->add_field.
825  
826 my $order = $form->field('foo')->order + 1;
827 $form->add_field(
828 name => 'my_cb',
829 type => 'Checkbox',
830 order => $order,
831 );
832  
833 It will be ordered as the last field unless you set the 'order'
834 attribute. Form fields are automatically ordered by 5 (i.e. 5, 10, 15, etc).
835  
836 =head3 active/inactive
837  
838 A field can be marked 'inactive' and set to active at process time
839 by specifying the field name in the 'active' array:
840  
841 has_field 'foo' => ( type => 'Text', inactive => 1 );
842 ...
843 my $form = MyApp::Form->new;
844 $form->process( active => ['foo'] );
845  
846 Or a field can be a normal active field and set to inactive at process
847 time:
848  
849 has_field 'bar';
850 ...
851 my $form = MyApp::Form->new;
852 $form->process( inactive => ['foo'] );
853  
854 Fields specified as active/inactive on 'process' will have the flag
855 flag cleared when the form is cleared (on the next process/check call).
856  
857 The 'sorted_fields' method returns only active fields, sorted according to the
858 'order' attribute. The 'fields' method returns all fields.
859  
860 foreach my $field ( $self->all_sorted_fields ) { ... }
861  
862 You can test whether a field is active by using the field 'is_active' and 'is_inactive'
863 methods.
864  
865 =head3 field_namespace
866  
867 Use to look for field during form construction. If a field is not found
868 with the field_namespace (or Data::MuForm/Data::MuFormX),
869 the 'type' must start with a '+' and be the complete package name.
870  
871 =head3 fields
872  
873 The array of fields, objects of L<Data::MuForm::Field> or its subclasses.
874 A compound field will itself have an array of fields,
875 so this is a tree structure.
876  
877 =head3 sorted_fields
878  
879 Returns those fields from the fields array which are currently active, ordered
880 by the 'order' attribute. This is the method that returns the fields that are
881 looped through when rendering.
882  
883 =head3 field($name), subfield($name)
884  
885 'field' is the method that is usually called to access a field:
886  
887 my $title = $form->field('title')->value;
888 [% f = form.field('title') %]
889  
890 my $city = $form->field('addresses.0.city')->value;
891  
892 Since fields are searched for using the form as a base, if you want to find
893 a sub field in a compound field method, the 'subfield' method may be more
894 useful, since you can search starting at the current field. The 'chained'
895 method also works:
896  
897 -- in a compound field --
898 $self->field('media.caption'); # fails
899 $self->field('media')->field('caption'); # works
900 $self->subfield('media.caption'); # works
901  
902 =head3 build_field_id
903  
904 Create a 'build_field_id' sub in the form class to use a common method for
905 constructing field ids.
906  
907 sub build_field_id {
908 my ( $self, $field ) = @_;
909 return $field->name . '_' . $self->id;
910 }
911  
912 =head2 Constraints and validation
913  
914 Most validation is performed on a per-field basis, and there are a number
915 of different places in which validation can be performed.
916  
917 See also L<Data::MuForm::Manual::Validation>.
918  
919 =head3 Class validation for individual fields
920  
921 You can define a method in your class to perform validation on a field.
922 This method is the equivalent of the field class validate method except it is
923 in the validator/form class, so you might use this
924 validation method if you don't want to create a field subclass.
925  
926 It has access to the form ($self) and the field.
927 This method is called after the field class 'validate' method, and is not
928 called if the value for the field is empty ('', undef). (If you want an
929 error message when the field is empty, use the 'required' flag and message
930 or the form 'validate' method.)
931 The name of this method can be set with 'set_validate' on the field. The
932 default is 'validate_' plus the field name:
933  
934 sub validate_testfield { my ( $self, $field ) = @_; ... }
935  
936 If the field name has dots they should be replaced with underscores.
937  
938 Note that you can also provide a coderef which will be a method on the field:
939  
940 has_field 'foo' => ( methods => { validate => \&validate_foo } );
941  
942 =head3 validate
943  
944 This is a form method that is useful for cross checking values after they have
945 been saved as their final validated value, and for performing more complex
946 dependency validation. It is called after all other field validation is done,
947 and whether or not validation has succeeded, so it has access to the
948 post-validation values of all the fields.
949  
950 This is the best place to do validation checks that depend on the values of
951 more than one field.
952  
953 =head2 Accessing errors
954  
955 Also see L<Data::MuForm::Manual::Errors>.
956  
957 Set an error in a field with C<< $field->add_error('some error string'); >>.
958 Set a form error not tied to a specific field with
959 C<< $self->add_form_error('another error string'); >>.
960 The 'add_error' and 'add_form_error' methods call localization. If you
961 want to skip localization for a particular error, you can use 'push_error'
962 or 'push_form_errors' instead.
963  
964 has_errors - returns true or false
965 error_fields - returns list of fields with errors
966 errors - returns array of error messages for the entire form
967 num_errors - number of errors in form
968  
969 Each field has an array of error messages. (errors, has_errors, num_errors,
970 clear_errors)
971  
972 $form->field('title')->errors;
973  
974 Compound fields also have an array of error_fields.
975  
976 =head2 Clear form state
977  
978 The clear method is called at the beginning of 'process' if the form
979 object is reused, such as when it is persistent,
980 or in tests. If you add other attributes to your form that are set on
981 each request, you may need to either clear those yourself or ensure that
982 they are always set on each process call.
983  
984 =head2 Miscellaneous attributes
985  
986 =head3 name
987  
988 The form's name. Useful for multiple forms. Used for the form element 'id'.
989 When 'field_prefix' is set it is used to construct the field 'id'
990 and 'name'. The default is derived from the form class name.
991  
992 =head3 init_values
993  
994 An 'init_values' object or hashref may be used instead of the 'model' to pre-populate the values
995 in the form. This can be useful when populating a form from default values
996 stored in a similar but different object than the one the form is creating.
997 It can be set in a variety of ways:
998  
999 my $form = MyApp::Form->new( init_values => { .... } );
1000 $form->process( init_values => {...}, ... );
1001 has '+init_values' => ( default => sub { { .... } } );
1002 sub init_values { my $self = shift; .... }
1003  
1004 The method version is useful if the organization of data in your form does
1005 not map to an existing or database object in an automatic way, and you need
1006 to create a different type of object for initialization. (You might also
1007 want to do 'update_model' yourself.)
1008  
1009 You can use both a 'model' and an 'init_values' hashref
1010 when some of the fields in your form come from the database and some
1011 are process or environment type flags that are not in the database.
1012  
1013 =head3 ctx
1014  
1015 Place to store application context for your use in your form's methods.
1016  
1017 =head2 Localizer
1018  
1019 The form has a 'localizer' object which is shared with form fields.
1020 Uses gettext style .po files with names parameters, and is implemented
1021 with internal code borrowed from L<Locale::TextDomain::OO>.
1022  
1023 =head2 Flags
1024  
1025 =head3 validated, is_valid
1026  
1027 Flag that indicates if form has been validated. You might want to use
1028 this flag if you're doing something in between process and returning,
1029 such as setting a stash key. ('is_valid' is a synonym for this flag)
1030  
1031 $form->process( ... );
1032 $c->stash->{...} = ...;
1033 return unless $form->validated;
1034  
1035 =head3 ran_validation
1036  
1037 Flag to indicate that validation has been run. This flag will be
1038 false when the form is initially loaded and displayed, since
1039 validation is not run until MuForm has params to validate.
1040  
1041 =head3 field_prefix
1042  
1043 String to be used as a prefix for field ids and names
1044 in an HTML form. Useful for multiple forms
1045 on the same HTML page. The prefix is stripped off of the fields
1046 before creating the internal field name, and added back in when
1047 returning a parameter hash from the 'fif' method. For example,
1048 the field name in the HTML form could be "book.borrower", and
1049 the field name in the MuForm form (and the database column)
1050 would be just "borrower".
1051  
1052 has '+name' => ( default => 'book' );
1053 has '+field_prefix' => ( default => 'book' );
1054  
1055 Also see the Field attribute "prefixed_name", a convenience function which
1056 will return the field_prefix + "." + field full_name
1057  
1058 =head2 setup
1059  
1060 This is where args passed to 'process' are set, and the form is
1061 filled by params, object, or fields.
1062  
1063 =head1 AUTHOR
1064  
1065 Gerda Shank
1066  
1067 =head1 COPYRIGHT AND LICENSE
1068  
1069 This software is copyright (c) 2017 by Gerda Shank.
1070  
1071 This is free software; you can redistribute it and/or modify it under
1072 the same terms as the Perl 5 programming language system itself.
1073  
1074 =cut
« MuForm/Field/Date.pm ↑ Summary MuForm/Params.pm »