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

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