blib/lib/HTML/Formhandler/Role/ToJSON.pm untested

filter: Statementstmt 0.0% 0 / 6 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 15 CC 2 SCAR 12.8
CC
2
Cov
0%
TO_JSON
6.0
prepare_error_response
2.0
prepare_valid_response
2.0
CRAP 3.6
1 package HTML::Formhandler::Role::ToJSON;
2  
3 use Moose::Role;
4  
5 sub prepare_error_response {
6 return +{
7 form_errors => $_[0]->form_errors,
8 error_by_field => $_[0]->errors_by_name,
9 fields => $_[0]->fif,
10 };
11 }
12  
13 sub prepare_valid_response {
14 return +{
15 fields => $_[0]->fif,
16 };
17 }
18  
19 sub TO_JSON {
20 return $_[0]->is_valid ?
21 $_[0]->prepare_valid_response :
22 $_[0]->prepare_error_response;
23 }
24  
25 1;
26  
27 =head1 NAME
28  
29 HTML::Formhandler::Role::ToJSON - Adds a basic 'TO_JSON' method
30  
31 =head1 SYNOPSIS
32  
33 package MyApp::Form::Email
34  
35 use HTML::FormHandler::Moose;
36  
37 extends 'HTML::FormHandler';
38 with 'HTML::Formhandler::Role::ToJSON';
39  
40 has_field 'email' => (
41 type=>'Email',
42 size => 96,
43 required => 1);
44  
45 has_field 'fname' => (
46 type=>'Text',
47 size => 96,
48 required => 1);
49  
50 has_field 'lname' => (
51 type=>'Text',
52 size => 96,
53 required => 1);
54  
55 __PACKAGE__->meta->make_immutable;
56  
57 =head1 DESCRIPTION
58  
59 Most Perl JSON encoders support serializing perl objects via a
60 'TO_JSON' method. This can be a convenient way to have a standard
61 JSON data structure. ALthough you may find over time a need to
62 write a more custom data structure, this role will offer a basic
63 encoding for error and success states that should be suitable for say
64 support AJAX web forms. It can also serve as a blueprint for your
65 more customized versions as your application needs evolve.
66  
67 =head1 METHODS
68  
69 This role creates the following methods.
70  
71 =head2 TO_JSON
72  
73 Returns a Hashref that can be encoded into JSON by a supporting
74 encoding such as L<JSON>. This returns one of two data structures
75 depending on if the form state is valid or not. If the form state
76 is valid it will return a Hashref like the following (examples based
77 on the SYNOPSIS code)
78  
79  
80 +{
81 fields => +{
82 email => "jjn\@yahoo.com",
83 fname => "john",
84 lname => "nap"
85 }
86 }
87  
88 So in the valid state case you get a hashref with a single key 'fields'
89 which itself is a key to a hashref of the normalized valid form parameters
90 (the values as it uses to update the model).
91  
92 If it is invalid the hashref looks like this:
93  
94 +{
95 error_by_field => {
96 email => [
97 "Email field is required"
98 ],
99 fname => [
100 "Fname field is required"
101 ],
102 lname => [
103 "Lname field is required"
104 ]
105 },
106 fields => {
107 email => "",
108 fname => "",
109 lname => ""
110 },
111 form_errors => []
112 }
113  
114 So again a hashref with three keys: 'fields', which is as it in for the valid
115 result state described above, 'error_by_field' which is a key to a hashref
116 of each form field that has an error condition (the error conditions are given
117 in an arrayref even if there is only one) and a third 'form_errors' which is a
118 key to an arrayref of any form wide errors. Arrayrefs will be empty if there is
119 no related error state.
120  
121 =head1 AUTHOR
122
123 John Napiorkowski L<email:jjnapiork@cpan.org>
124
125 =head1 SEE ALSO
126
127 L<HTML::Formhandler>. L<Moose::Role>
128
129 =head1 COPYRIGHT & LICENSE
130
131 Copyright 2015, John Napiorkowski L<email:jjnapiork@cpan.org>
132
133 This library is free software; you can redistribute it and/or modify it under
134 the same terms as Perl itself.
135  
136 =cut
137