Valiant/HTML/Util/Form.pm

filter: Statementstmt 86.8% 185 / 213 Branchbran 64.1% 95 / 148 Conditioncond 45.5% 36 / 79 MC/DCmcdc 12.5% 6 / 48 Subroutinesub 91.1% 31 / 34 Podpod 40.0% 2 / 5 totaltotal 67.3% 355 / 527 CC 105 SCAR 27.0
CC
105
Cov
72%
form_with
40.7
_html_options_for_form_with
25.1
form_for
16.0
CRAP 14.8
« Valiant/I18N.pm ↑ Summary Valiant/JSON/JSONBuilder.pm »
1 package Valiant::HTML::Util::Form;
2  
3 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Module::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Valiant::Naming;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Valiant::HTML::FormBuilder::DefaultModel;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 extends 'Valiant::HTML::Util::FormTags';
11  
12 2 has 'context' => (is=>'ro', required=>0, predicate=>'has_context');
13 2 has 'controller' => (is=>'ro', required=>0, predicate=>'has_controller');
14  
15 2 has 'form_with_generates_ids' => (
16 is => 'ro',
17 required => 1,
18 builder => '_form_with_generates_ids'
19 );
20  
21 3 ▶ sub _form_with_generates_ids { 0 }
Subroutine _form_with_generates_ids: called CC=1 SCAR=0.0
22  
23 2 has 'formbuilder_class' => (
24 is => 'ro',
25 required => 1,
26 lazy => 1,
27 builder => '_default_formbuilder_class',
28 );
29  
30 sub _default_formbuilder_class {
31 3 ▶ my $self = shift;
Subroutine _default_formbuilder_class: called CC=2 SCAR=7.1
32 3 ▶ return $self->view->formbuilder_class if $self->view->can('formbuilder_class');
Branch: 1 branch
BranchTrueFalseTotal
if $self->view->can("formbuilder_class") 0 3 3
33 3 return 'Valiant::HTML::FormBuilder';
34 };
35  
36 # for class and subclasses
37  
38 26 ▶sub _DEFAULT_ID_DELIM { '_' }
Subroutine _DEFAULT_ID_DELIM: called CC=1 SCAR=0.0
39  
40 sub _dom_class {
41 16 ▶ my ($self, $model, $prefix) = @_;
Subroutine _dom_class: called CC=2 SCAR=7.0
42 16 my $singular = $self->_model_name_from_object_or_class($model)->param_key;
43 16 ▶ return $prefix ? "${prefix}@{[ _DEFAULT_ID_DELIM ]}${singular}" : $singular;
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 16 0 16
44 }
45  
46 sub _dom_id {
47 8 ▶ my ($self, $model, $prefix) = @_;
Subroutine _dom_id: called CC=3 SCAR=13.5
48 8 ▶ if(my $model_id = _model_id_for_dom_id($model)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $model_id = _model_id_for_dom_id($model)) { } 0 8 8
49 0 return "@{[ $self->_dom_class($model, $prefix) ]}@{[ _DEFAULT_ID_DELIM ]}${model_id}";
50 } else {
51 8 ▶ $prefix ||= 'new';
Condition50%1/2
$prefix ||= "new"
l!l
80
MC/DC0%0/1
$prefix ||= "new"
$prefix
Truth table50%1/2
A
A= $prefix
Aresult
11
00
52 8 return $self->_dom_class($model, $prefix)
53 }
54 }
55  
56 sub _model_id_for_dom_id {
57 8 ▶ my $model = shift;
Subroutine _model_id_for_dom_id: called CC=3 SCAR=14.2
58 8 ▶ return unless $model->can('id') && defined($model->id);
Branch: 1 branch
BranchTrueFalseTotal
unless $model->can("id") and defined $model->id 8 0 8
Condition33%1/3
$model->can("id") and defined $model->id
!ll&&!rl&&r
800
MC/DC0%0/2
$model->can("id") and defined $model->id
$model->can("id") defined $model->id
Truth table33%1/3
A and B
A= $model->can("id")B= defined $model->id
ABresult
0X0
100
111
59 0 return join '_', ($model->id);
60 }
61  
62 sub _to_model {
63 59 ▶ my ($self, $model) = @_;
Subroutine _to_model: called CC=4 SCAR=14.6
64 59 ▶ croak "No model provided" unless $model;
Branch: 1 branch
BranchTrueFalseTotal
unless $model 0 59 59
65 59 ▶ confess "Model is not an object: $model" unless Scalar::Util::blessed($model);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($model) 0 59 59
66 59 ▶ return $model->to_model if $model->can('to_model');
Branch: 1 branch
BranchTrueFalseTotal
if $model->can("to_model") 0 59 59
67 59 return $model;
68 }
69  
70  
71 sub _model_name_from_object_or_class {
72 28 ▶ my ($self, $proto) = @_;
Subroutine _model_name_from_object_or_class: called CC=2 SCAR=7.6
73 28 my $model = $self->_to_model($proto);
74  
75 28 ▶ return $model->model_name if $model->can('model_name');
Branch: 1 branch
BranchTrueFalseTotal
if $model->can("model_name") 28 0 28
76 0 return Valiant::Name->new(Valiant::Naming::prepare_model_name_args($model));
77 }
78  
79 # public methods
80  
81 sub model_persisted {
82 26 ▶ my ($self, $model) = @_;
Subroutine model_persisted: called CC=4 SCAR=18.9
Pod undocumented
83 26 ▶ croak "No model provided" unless $model;
Branch: 1 branch
BranchTrueFalseTotal
unless $model 0 26 26
84 26 ▶ return $model->persisted if $model->can('persisted');
Branch: 1 branch
BranchTrueFalseTotal
if $model->can("persisted") 26 0 26
85 0 ▶ return $model->in_storage if $model->can('in_storage');
Branch: 1 branch
BranchTrueFalseTotal
if $model->can("in_storage") 0 0 0
86 0 return 0;
87 }
88  
89 # form_for $model, \%options, \&block
90 # form_for $model, \&block
91 #
92 # Where %options are:
93 # action: actual url the action. not required
94 # method: default is POST
95 # namespace: additional prefix for form and element IDs for uniqueness. not required
96 # html: additional hash of values for html attributes
97  
98 sub form_for {
99 10 ▶ my $self = shift;
Subroutine form_for: called CC=14 SCAR=27.7
Pod documented
100 10 my $proto = shift; # required; at the start
101 10 ▶ my $content_block_coderef = (ref($_[-1])||'') eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'CODE' ? : 10 0 10
Condition50%1/2
ref $_[-1] || ''
l!l
100
MC/DC0%0/1
ref $_[-1] || ''
ref $_[-1]
Truth table50%1/2
A
A= ref $_[-1]
Aresult
11
00
102 10 ▶ my $options = ref($_[-1]||'') eq 'HASH' ? pop : +{};
Branch: 1 branch
BranchTrueFalseTotal
ref($_[-1] || '') eq 'HASH' ? : 8 2 10
Condition100%2/2
$_[-1] || ''
l!l
82
MC/DC100%1/1
$_[-1] || ''
$_[-1]
Truth table100%2/2
A
A= $_[-1]
Aresult
11
00
103  
104 10 my ($model, $object_name);
105 10 ▶ if( ref(\$proto) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$proto eq 'SCALAR') { } 2 8 10
106 2 $object_name = $proto;
107 2 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 1 2
108 1 $model = shift; # form_for 'name', $model, \%options, \&block
109 } else {
110 # Support form_for 'attribute_name', \%options, \&block
111 1 ▶ $model = $self->view->read_attribute_for_html($object_name)
Branch: 1 branch
BranchTrueFalseTotal
if $self->view->attribute_exists_for_html($object_name) 1 0 1
112 if $self->view->attribute_exists_for_html($object_name);
113 }
114 2 ▶ if(defined $model) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $model) 2 0 2
115 2 ▶ $options->{as} ||= $object_name;
Condition33%1/3
$options->{"as"} ||= $object_name
l!l&&r!l&&!r
020
MC/DC0%0/2
$options->{"as"} ||= $object_name
$options->{"as"} $object_name
Truth table33%1/3
A ||= B
A= $options->{"as"}B= $object_name
ABresult
1X1
011
000
116 2 $self->_apply_form_for_options($model, $options);
117 }
118 } else {
119 8 my $object = $self->_object_from_record_proto($proto); # Is either arrayref or object
120  
121 8 ▶ croak "First argument can't be undef or empty string" unless defined($object) && length($object);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object and length $object 0 8 8
Condition33%1/3
defined $object and length $object
!ll&&!rl&&r
008
MC/DC0%0/2
defined $object and length $object
defined $object length $object
Truth table33%1/3
A and B
A= defined $objectB= length $object
ABresult
0X0
100
111
122 8 $model = $proto; # Is either arrayref or object
123 $object_name = exists $options->{as} ?
124 8 ▶ $options->{as} : $self->_model_name_from_object_or_class($object)->param_key;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'as'} ? : 0 8 8
125 8 $self->_apply_form_for_options($object, $options);
126 }
127  
128 10 $options->{model} = $model;
129 10 $options->{scope} = $object_name;
130 10 $options->{skip_default_ids} = 0;
131 $options->{allow_method_names_outside_object} = exists $options->{allow_method_names_outside_object}
132 ? $options->{allow_method_names_outside_object}
133 10 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'allow_method_names_outside_object'} ? : 0 10 10
134  
135 10 return $self->form_with($options, $content_block_coderef);
136 }
137  
138 sub _object_from_record_proto {
139 21 ▶ my ($self, $proto) = @_;
Subroutine _object_from_record_proto: called CC=5 SCAR=18.6
140 21 ▶ croak "First argument can't be undef or empty string" unless defined($proto) && length($proto);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $proto and length $proto 0 21 21
Condition33%1/3
defined $proto and length $proto
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $proto and length $proto
defined $proto length $proto
Truth table33%1/3
A and B
A= defined $protoB= length $proto
ABresult
0X0
100
111
141 21 ▶ return $proto->[-1] if ((ref($proto)||'') eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto || "") eq "ARRAY" 0 21 21
Condition50%1/2
ref $proto || ''
l!l
210
MC/DC0%0/1
ref $proto || ''
ref $proto
Truth table50%1/2
A
A= ref $proto
Aresult
11
00
142 21 return $proto;
143 }
144  
145 sub _apply_form_for_options {
146 10 ▶ my ($self, $model, $options) = @_;
Subroutine _apply_form_for_options: called CC=8 SCAR=20.8
147 10 $model = $self->_to_model($model);
148  
149 10 ▶ my $as = exists $options->{as} ? $options->{as} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'as'} ? : 2 8 10
150 10 ▶ my $namespace = exists $options->{namespace} ? $options->{namespace} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'namespace'} ? : 0 10 10
151 10 ▶ my ($action, $method) = @{ $self->model_persisted($model) ? ['edit', 'patch']:['new', 'post'] };
Branch: 1 branch
BranchTrueFalseTotal
$self->model_persisted($model) ? : 5 5 10
152  
153 $options->{html} = $self->_merge_attrs(
154 ($options->{html} || +{}),
155 +{
156 class => $as ? "${action}_${as}" : $self->_dom_class($model, $action),
157 10 ▶ id => join(_DEFAULT_ID_DELIM, ( $as ? (grep { defined $_ } $namespace, $action, $as) : (grep { defined $_ } ($namespace, $self->_dom_id($model, $action))) )),
Branch: 3 branches
BranchTrueFalseTotal
$as ? : 2 8 10
$as ? : 2 8 10
lc $method ne 'post' ? : 5 5 10
Condition50%1/2
$options->{'html'} || {}
l!l
010
MC/DC0%0/1
$options->{'html'} || {}
$options->{'html'}
Truth table50%1/2
A
A= $options->{'html'}
Aresult
11
00
158 method => $method,
159 (( lc($method) ne 'post') ? (data =>{tunneled_method => $method}) : ()),
160 },
161 );
162  
163 10 return $options;
164 }
165  
166 sub form_with {
167 17 ▶ my $self = shift;
Subroutine form_with: called CC=22 SCAR=37.1
Pod undocumented
168 17 ▶ my $content_block_coderef = (ref($_[-1])||'') eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'CODE' ? : 17 0 17
Condition50%1/2
ref $_[-1] || ''
l!l
170
MC/DC0%0/1
ref $_[-1] || ''
ref $_[-1]
Truth table50%1/2
A
A= ref $_[-1]
Aresult
11
00
169 17 ▶ my $options = @_ ? shift : +{};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 17 0 17
170 17 ▶ my $scope = exists $options->{scope} ? $options->{scope} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'scope'} ? : 12 5 17
171  
172 17 $options->{skip_default_ids} = 0;
173 17 ▶ $options->{controller} ||= $self->controller if $self->has_controller;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_controller 0 17 17
Condition0%0/3
$options->{"controller"} ||= $self->controller
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{"controller"} ||= $self->controller
$options->{"controller"} $self->controller
Truth table0%0/3
A ||= B
A= $options->{"controller"}B= $self->controller
ABresult
1X1
011
000
174  
175 $options->{allow_method_names_outside_object} = exists $options->{allow_method_names_outside_object}
176 ? $options->{allow_method_names_outside_object}
177 17 ▶ : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'allow_method_names_outside_object'} ? : 10 7 17
178  
179 17 my ($model, $model_path, $url);
180 17 ▶ if($options->{model}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"model"}) 13 4 17
181 13 ▶ $model_path = ((ref($options->{model})||'') eq 'ARRAY') ? $options->{model} : [$options->{model}];
Branch: 1 branch
BranchTrueFalseTotal
(ref $options->{'model'} || '') eq 'ARRAY' ? : 0 13 13
Condition50%1/2
ref $options->{'model'} || ''
l!l
130
MC/DC0%0/1
ref $options->{'model'} || ''
ref $options->{'model'}
Truth table50%1/2
A
A= ref $options->{'model'}
Aresult
11
00
182 13 $model = $self->_to_model($self->_object_from_record_proto(delete $options->{model}));
183 $scope = exists $options->{scope} ?
184 delete $options->{scope} :
185 13 ▶ $self->_model_name_from_object_or_class($model)->param_key;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'scope'} ? : 10 3 13
186 $url = $self->_polymorphic_path_for_model(
187 $model_path, $scope,
188 $options->{controller}, $options->{new_url}, $options->{edit_url}
189 13 ▶ ) unless $options->{action};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"action"} 13 0 13
190 }
191  
192 17 ▶ $url ||= delete $options->{url} if exists $options->{url};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"url"} 12 5 17
Condition33%1/3
$url ||= delete $options->{"url"}
l!l&&r!l&&!r
0120
MC/DC0%0/2
$url ||= delete $options->{"url"}
$url delete $options->{"url"}
Truth table33%1/3
A ||= B
A= $urlB= delete $options->{"url"}
ABresult
1X1
011
000
193 17 ▶ $url = $url->($self, $model_path) if (ref($url)||'') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $url || "") eq "CODE" 2 15 17
Condition100%2/2
ref $url || ''
l!l
215
MC/DC100%1/1
ref $url || ''
ref $url
Truth table100%2/2
A
A= ref $url
Aresult
11
00
194  
195 17 my $html_options = $self->_html_options_for_form_with($url, $model, $options);
196 17 ▶ $options->{html}{csrf_token} = $html_options->{csrf_token} if exists $html_options->{csrf_token};
Branch: 1 branch
BranchTrueFalseTotal
if exists $html_options->{"csrf_token"} 1 16 17
197
198 17 my $builder = $self->_instantiate_builder($scope, $model, $options);
199  
200 17 ▶ if($html_options->{data}{remote}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html_options->{"data"}{"remote"}) 0 17 17
201 0 my $url = $html_options->{action}->clone;
202 my $replace = exists($html_options->{data}{replace})
203 ? $html_options->{data}{replace}
204 0 ▶ : '#'.$html_options->{id};
Branch: 1 branch
BranchTrueFalseTotal
exists $html_options->{'data'}{'replace'} ? : 0 0 0
205 0 $url->query_param(replace=>$replace);
206  
207 0 $html_options->{action} = $url;
208 0 ▶ $html_options->{data}{method} ||= $html_options->{method};
Condition0%0/3
$html_options->{"data"}{"method"} ||= $html_options->{"method"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$html_options->{"data"}{"method"} ||= $html_options->{"method"}
$html_options->{"data"}{"method"} $html_options->{"method"}
Truth table0%0/3
A ||= B
A= $html_options->{"data"}{"method"}B= $html_options->{"method"}
ABresult
1X1
011
000
209 }
210  
211 17 ▶ if($content_block_coderef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_block_coderef) { } 17 0 17
212 return my $output = $self->join_tags(
213 $self->form_tag($html_options, sub {
214 17 ▶ my @form_node = $content_block_coderef->($self->view, $builder, $model);
Subroutine __ANON__: called CC=1 SCAR=0.0
215 17 return $builder->view->safe_concat(@form_node);
216 })
217 17 );
218 } else {
219 0 return my $obj = bless +{
220 fb => $builder,
221 model => $model,
222 html_options => $html_options,
223 util_form => $self,
224 view => $self->view,
225 }, 'Valiant::HTML::Util::Form::FormObject';
226 }
227 }
228  
229 # if url is empty
230 # update
231 # $view->update_uri_for($form_controller, $scope, $model_path)
232 # $form_controller->update_uri($model_path)
233 # create
234 # $view->create_uri_for($form_controller, $scope, $model_path, $new_model)
235 # $form_controller->create_uri($model_path, $new_model)
236  
237 sub _polymorphic_path_for_model {
238 13 ▶ my $self = shift;
Subroutine _polymorphic_path_for_model: called CC=4 SCAR=13.9
239 13 my @args = my ($model_path, $scope, $controller, $new_url, $edit_url) = @_;
240  
241 13 ▶ if($self->model_persisted($model_path->[-1])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->model_persisted($model_path->[-1])) { } 6 7 13
242 6 ▶ return $edit_url ?
Branch: 1 branch
BranchTrueFalseTotal
$edit_url ? : 1 5 6
243 $edit_url->($self->view, $model_path) :
244 $self->_edit_action_for_model(@args)
245 } else {
246 return $new_url ?
247 7 ▶ do { pop @{$model_path}; $new_url->($self->view, $model_path) } :
Branch: 1 branch
BranchTrueFalseTotal
$new_url ? : 1 6 7
248 $self->_new_action_for_model(@args);
249 }
250 }
251  
252 sub _edit_action_for_model {
253 5 ▶ my ($self, $model_path, $scope, $controller) = @_;
Subroutine _edit_action_for_model: called CC=6 SCAR=27.1
254  
255 5 ▶ if($self->can('update_uri_for')) { # check view first
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("update_uri_for")) 0 5 5
256 0 my $url = $self->update_uri_for($model_path, $scope, $controller);
257 0 ▶ return $url if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
258 }
259 5 ▶ if($controller && $controller->can('update_uri')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($controller and $controller->can("update_uri")) 0 5 5
Condition33%1/3
$controller and $controller->can("update_uri")
!ll&&!rl&&r
500
MC/DC0%0/2
$controller and $controller->can("update_uri")
$controller $controller->can("update_uri")
Truth table33%1/3
A and B
A= $controllerB= $controller->can("update_uri")
ABresult
0X0
100
111
260 0 my $url = $controller->update_uri($model_path);
261 0 ▶ return $url if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
262 }
263  
264 5 return undef;
265 }
266  
267 sub _new_action_for_model {
268 6 ▶ my ($self, $model_path, $scope, $controller) = @_;
Subroutine _new_action_for_model: called CC=6 SCAR=25.4
269 6 my $new_model = pop @{$model_path};
270  
271 6 ▶ if($self->can('create_uri_for')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("create_uri_for")) 0 6 6
272 0 my $url = $self->create_uri_for($model_path, $new_model, $scope, $controller);
273 0 ▶ return $url if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
274 }
275 6 ▶ if($controller && $controller->can('create_uri')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($controller and $controller->can("create_uri")) 0 6 6
Condition33%1/3
$controller and $controller->can("create_uri")
!ll&&!rl&&r
600
MC/DC0%0/2
$controller and $controller->can("create_uri")
$controller $controller->can("create_uri")
Truth table33%1/3
A and B
A= $controllerB= $controller->can("create_uri")
ABresult
0X0
100
111
276 0 my $url = $controller->create_uri($model_path);
277 0 ▶ return $url if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
278 }
279  
280 6 return undef;
281 }
282  
283 # _instantiate_builder($object)
284 # _instantiate_builder($object, \%options)
285 # _instantiate_builder($name, $object)
286 # _instantiate_builder($name, $object, \%options)
287  
288 sub _instantiate_builder {
289 57 ▶ my $self = shift;
Subroutine _instantiate_builder: called CC=10 SCAR=23.6
290 57 ▶ my $options = (ref($_[-1])||'') eq 'HASH' ? pop(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'HASH' ? : 57 0 57
Condition50%1/2
ref $_[-1] || ''
l!l
570
MC/DC0%0/1
ref $_[-1] || ''
ref $_[-1]
Truth table50%1/2
A
A= ref $_[-1]
Aresult
11
00
291 57 ▶ my $object = Scalar::Util::blessed($_[-1]) ? pop(@_) : bless +{}, 'Valiant::HTML::FormBuilder::DefaultModel';
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[-1]) ? : 52 5 57
292 57 ▶ my $model_name = scalar(@_) ? shift(@_) : $self->_model_name_from_object_or_class($object)->param_key;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 57 0 57
293 my $builder = exists($options->{builder}) && defined($options->{builder}) ?
294 $options->{builder} :
295 57 ▶ $self->formbuilder_class;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'builder'} && defined $options->{'builder'} ? : 31 26 57
Condition66%2/3
exists $options->{"builder"} and defined $options->{"builder"}
!ll&&!rl&&r
26031
MC/DC50%1/2
exists $options->{"builder"} and defined $options->{"builder"}
exists $options->{"builder"} defined $options->{"builder"}
Truth table66%2/3
A and B
A= exists $options->{"builder"}B= defined $options->{"builder"}
ABresult
0X0
100
111
296  
297 57 my %args = (
298 tag_helpers => $self,
299 model => $object,
300 name => $model_name,
301 options => $options
302 );
303  
304 57 $options->{builder} = $builder;
305 57 $self->_merge_attrs(\%args, $options, qw(namespace id index parent_builder theme));
306  
307 57 ▶ if( exists($options->{parent_builder}) && exists($options->{parent_builder}{theme}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"parent_builder"} and exists $options->{"parent_builder"}{"theme"}) 35 22 57
Condition66%2/3
exists $options->{"parent_builder"} and exists $options->{"parent_builder"}{"theme"}
!ll&&!rl&&r
22035
MC/DC50%1/2
exists $options->{"parent_builder"} and exists $options->{"parent_builder"}{"theme"}
exists $options->{"parent_builder"} exists $options->{"parent_builder"}{"theme"}
Truth table66%2/3
A and B
A= exists $options->{"parent_builder"}B= exists $options->{"parent_builder"}{"theme"}
ABresult
0X0
100
111
308 35 ▶ $args{theme} = +{ %{$args{theme}||+{}}, %{$options->{parent_builder}{theme}} };
Branch: 1 branch
BranchTrueFalseTotal
$args{"theme"} or {} 35 0 35
309 }
310  
311 57 return Module::Runtime::use_module($builder)->new(%args);
312 }
313  
314 sub _html_options_for_form_with {
315 17 ▶ my ($self, $url, $model, $options) = @_;
Subroutine _html_options_for_form_with: called CC=15 SCAR=32.2
316 17 ▶ my $html_options = $options->{html} || +{};
Condition100%2/2
$options->{'html'} || {}
l!l
116
MC/DC100%1/1
$options->{'html'} || {}
$options->{'html'}
Truth table100%2/2
A
A= $options->{'html'}
Aresult
11
00
317  
318 17 $self->_merge_attrs($html_options, $options, qw(action method data id csrf_token class style));
319 17 ▶ $url = $url->($self, $model) if (ref($url)||'') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $url || "") eq "CODE" 0 17 17
Condition50%1/2
ref $url || ''
l!l
017
MC/DC0%0/1
ref $url || ''
ref $url
Truth table50%1/2
A
A= ref $url
Aresult
11
00
320 17 ▶ $html_options->{action} ||= $url if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 14 3 17
Condition33%1/3
$html_options->{"action"} ||= $url
l!l&&r!l&&!r
0140
MC/DC0%0/2
$html_options->{"action"} ||= $url
$html_options->{"action"} $url
Truth table33%1/3
A ||= B
A= $html_options->{"action"}B= $url
ABresult
1X1
011
000
321  
322 17 my %csrf_opts = (form_id => $options->{html}{id});
323 17 ▶ $html_options->{csrf_token} ||= $self->view->csrf_token(%csrf_opts) if $self->view->can('csrf_token');
Branch: 1 branch
BranchTrueFalseTotal
if $self->view->can("csrf_token") 0 17 17
Condition0%0/3
$html_options->{"csrf_token"} ||= $self->view->csrf_token(%csrf_opts)
l!l&&r!l&&!r
000
MC/DC0%0/2
$html_options->{"csrf_token"} ||= $self->view->csrf_token(%csrf_opts)
$html_options->{"csrf_token"} $self->view->csrf_token(%csrf_opts)
Truth table0%0/3
A ||= B
A= $html_options->{"csrf_token"}B= $self->view->csrf_token(%csrf_opts)
ABresult
1X1
011
000
324 17 ▶ $html_options->{csrf_token} ||= $self->context->csrf_token(%csrf_opts) if $self->has_context && $self->context->can('csrf_token');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_context and $self->context->can("csrf_token") 0 17 17
Condition16%1/6
$self->has_context and $self->context->can("csrf_token")
!ll&&!rl&&r
1700
$html_options->{"csrf_token"} ||= $self->context->csrf_token(%csrf_opts)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->has_context and $self->context->can("csrf_token")
$self->has_context $self->context->can("csrf_token")
MC/DC0%0/2
$html_options->{"csrf_token"} ||= $self->context->csrf_token(%csrf_opts)
$html_options->{"csrf_token"} $self->context->csrf_token(%csrf_opts)
Truth table33%1/3
A and B
A= $self->has_contextB= $self->context->can("csrf_token")
ABresult
0X0
100
111
Truth table0%0/3
A ||= B
A= $html_options->{"csrf_token"}B= $self->context->csrf_token(%csrf_opts)
ABresult
1X1
011
000
325 17 ▶ $html_options->{tunneled_method} = 1 unless exists $html_options->{tunneled_method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $html_options->{"tunneled_method"} 17 0 17
326 17 ▶ $html_options->{method} ||= $model && $self->model_persisted($model) ? 'patch' : 'post';
Branch: 1 branch
BranchTrueFalseTotal
$model && $self->model_persisted($model) ? : 1 6 7
Condition83%5/6
$html_options->{"method"} ||= $model && $self->model_persisted($model) ? "patch" : "post"
l!l&&r!l&&!r
1070
$model and $self->model_persisted($model)
!ll&&!rl&&r
421
MC/DC0%0/3
$html_options->{"method"} ||= $model && $self->model_persisted($model) ? "patch" : "post"
$html_options->{"method"} $model $self->model_persisted($model)
Truth table0%0/4
A ||= B and C
A= $html_options->{"method"}B= $modelC= $self->model_persisted($model)
ABCresult
1XX1
0111
00X0
0100
327  
328 # This is what Rails does but not sure I want to force that. It might break a
329 # lot of existing code people try to shoehorn this stuff into.
330 # $html_options->{enctype} ||= $html_options->{method} eq 'get' ?
331 # 'application/x-www-form-urlencoded' :
332 # 'multipart/form-data';
333  
334 17 return $html_options;
335 }
336  
337 sub fields_for {
338 10 ▶ my $self = shift;
Subroutine fields_for: called CC=6 SCAR=17.9
Pod documented
339 10 my ($name, $model);
340 10 my $proto = shift;
341  
342 10 ▶ if( Scalar::Util::blessed $proto ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($proto)) { } 1 9 10
343 1 $model = $proto;
344 } else {
345 9 $name = $proto;
346 9 ▶ if( @_ && Scalar::Util::blessed($_[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and builtin::blessed($_[0])) { } 7 2 9
Condition66%2/3
@_ and builtin::blessed($_[0])
!ll&&!rl&&r
027
MC/DC50%1/2
@_ and builtin::blessed($_[0])
@_ builtin::blessed($_[0])
Truth table66%2/3
A and B
A= @_B= builtin::blessed($_[0])
ABresult
0X0
100
111
347 7 $model = shift;
348 } else {
349 2 ▶ $model = $self->view->read_attribute_for_html($proto)
Branch: 1 branch
BranchTrueFalseTotal
if $self->view->attribute_exists_for_html($proto) 1 1 2
350 if $self->view->attribute_exists_for_html($proto);
351 }
352 }
353 10 my $block = pop @_;
354 10 ▶ my $options = @_ ? shift(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 4 10
355  
356 10 $options = {
357 model => $model,
358 allow_method_names_outside_object => 0,
359 skip_default_ids => 0,
360 %$options
361 };
362  
363 10 return $self->fields($name, $options, $block);
364 }
365  
366 sub _object_for_form_builder {
367 8 ▶ my ($self, $object) = @_;
Subroutine _object_for_form_builder: called CC=2 SCAR=8.1
368 8 ▶ return $object if Scalar::Util::blessed($object);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($object) 8 0 8
369 ## in Rails this returns a single model object when $object is an arrayref
370 ## but I don't think that makes sense here at least for the moment.
371 0 die "Missing required object";
372 }
373  
374 sub fields {
375 10 ▶ my ($self, $scope, $options, $block) = @_;
Subroutine fields: called CC=4 SCAR=13.9
Pod undocumented
376  
377 $options = {
378 allow_method_names_outside_object => 1,
379 skip_default_ids => $self->form_with_generates_ids,
380 10 ▶ %{$options||+{}},
Branch: 1 branch
BranchTrueFalseTotal
$options or {} 0 10 10
381 };
382  
383 10 ▶ if ($options->{model}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"model"}) 8 2 10
384 8 my $model = $self->_to_model($self->_object_for_form_builder($options->{model}));
385 8 ▶ $scope ||= $self->_model_name_from_object_or_class($model)->param_key;
Condition66%2/3
$scope ||= $self->_model_name_from_object_or_class($model)->param_key
l!l&&r!l&&!r
710
MC/DC0%0/2
$scope ||= $self->_model_name_from_object_or_class($model)->param_key
$scope $self->_model_name_from_object_or_class($model)->param_key
Truth table66%2/3
A ||= B
A= $scopeB= $self->_model_name_from_object_or_class($model)->param_key
ABresult
1X1
011
000
386 }
387  
388 10 my $builder = $self->_instantiate_builder($scope, $options->{model}, $options);
389 10 my $output = $self->join_tags($block->($self->view, $builder));
390  
391 10 return $output;
392 }
393  
394 package Valiant::HTML::Util::Form::FormObject;
395  
396 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
397 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
398 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
399  
400 0 ▶sub fb { shift->{fb} }
Subroutine fb: not called CC=1 SCAR=6.9
401  
402 sub render {
403 0 ▶ my ($self, $content_block_coderef) = @_;
Subroutine render: not called CC=2 SCAR=17.9
404 0 ▶ croak "No content block provided" unless $content_block_coderef;
Branch: 1 branch
BranchTrueFalseTotal
unless $content_block_coderef 0 0 0
405  
406 return my $output = $self->{util_form}->join_tags(
407 $self->{util_form}->form_tag($self->{html_options}, sub {
408 my @form_node = $content_block_coderef->(
409 $self->{view},
410 $self->{fb},
411 $self->{model},
412 0 ▶ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
413 0 return $self->{fb}->view->safe_concat(@form_node);
414 })
415 0 );
416 }
417  
418 2 1;
419  
420 =head1 NAME
421  
422 Valiant::HTML::Util::Form - HTML Form
423  
424 =head1 SYNOPSIS
425  
426 Given a model like:
427  
428 package Person;
429  
430 use Moo;
431 use Valiant::Validations;
432  
433 has first_name => (is=>'ro');
434 has last_name => (is=>'ro');
435  
436 validates ['first_name', 'last_name'] => (
437 length => {
438 maximum => 20,
439 minimum => 3,
440 }
441 );
442  
443 Wrap a formbuilder object around it and generate HTML form field controls:
444  
445 use Valiant::HTML::Util::Form;
446  
447 my $f = Valiant::HTML::Util::Form->new()
448 my $person = Local::Person->new(first_name=>'J', last_name=>'Napiorkowski');
449 $person->validate;
450  
451 $f->form_for($person, sub($fb, $person) {
452 return $fb->label('first_name'),
453 $fb->input('first_name'),
454 $fb->errors_for('first_name', +{ class=>'invalid-feedback' }),
455 $fb->label('last_name'),
456 $fb->input('last_name'),
457 $fb->errors_for('last_name'+{ class=>'invalid-feedback' });
458 });
459  
460 Generates something like:
461  
462 <form accept-charset="UTF-8" class="new_person" enctype="multipart/form-data" id="new_person" method="post">
463 <label for="person_first_name">First Name</label>
464 <input id="person_first_name" name="person.first_name" type="text" value="John"/>
465 <div class='invalid-feedback'>First Name is too short (minimum is 3 characters).</div>
466 <label for="person_last_name">Last Name</label>
467 <input id="person_last_name" name="person.last_name" type="text" value="Napiorkowski"/>
468 </form>
469  
470 Alternatively you can create a form object and then render it later:
471  
472 my $form = $f->form_for($person);
473 $form->render(sub($fb, $person) {
474 return $fb->label('first_name'),
475 $fb->input('first_name'),
476 $fb->errors_for('first_name', +{ class=>'invalid-feedback' }),
477 $fb->label('last_name'),
478 $fb->input('last_name'),
479 $fb->errors_for('last_name'+{ class=>'invalid-feedback' });
480 });
481  
482 Would return the same output.
483  
484 =head1 DESCRIPTION
485  
486 Builds on L<Valiant::HTML::Util::TagBuilder> and L<Valiant::HTML::Util::FormTags> to provide a
487 wrapper around a model object that can be used to generate HTML form controls via a formbuilder
488 such as L<Valliant::HTML::FormBuilder>. or a subclass thereof.
489  
490 Like its parent classes, you can provide a view context to the constructor and it will be used
491 to provide attribute values as well as methods used to create safe strings. This is documented
492 extensively in L<Valiant::HTML::Util::TagBuilder> which you should review if you are creating
493 your own view integration. You can see an example view context in L<Valiant::HTML::Util::View>.
494  
495 =head1 INHERITED METHODS
496  
497 This class inherits all methods from L<Valiant::HTML::Util::TagBuilder> and
498 L<Valiant::HTML::Util::FormTags>.
499  
500 =head1 REQUIRED MODEL API
501  
502 This (and L<Valiant::HTML::FormBuilder>) wrap an object model that is expected to do the following
503 interface.
504  
505 =head2 to_model
506  
507 This is an optional method. If this is supported, we call C<to_model> on the wrapped object
508 before using it on the form methods. This allows you to delegate the required API to a secondary
509 object, which can result in a cleaner API depending on your designs and use cases.
510  
511 =head2 in_storage
512  
513 This is an optional method. If your object has a backing storage solution (such as your object
514 is an instance of a DBIC Result Source) you can provide this method to influence how your object
515 form tags are created. If provided this method should return a boolean when if true means that
516 the object is representing data which is stored in the backing storage solution. Please note that
517 this does not mean that the object is synchronized with the backing storage since its possible that
518 the object has been changed by the user.
519  
520 =head2 is_attribute_changed
521  
522 $model->is_attribute_changed($attr); # true or false
523  
524 Optional method. If provided returns a boolean if the attribute has been changed from its initial state,
525 as defined by either being different from the backing store (if it exists) or being changed from its default
526 value when created as a new model.
527  
528 =head2 human_attribute_name
529  
530 $model->human_attribute_name('user'); # User
531  
532 Optional. If provided, uses the model to look up a displayable version of the attribute name, for
533 example used in a label for an input control. If not present we use L<Valiant::HTML::FormTags\_humanize>
534 to create a displayable name from the attribute name.
535  
536 =head2 read_attribute_for_html
537  
538 $model->read_attribute_for_html('user'); # User
539  
540 Optional. If provided must access the string name of the field or attribute and should return the model
541 value for that attribute suitable for HTML form display. You might wish to use this as a way to deflate
542 or otherwise stringify non string values. If not provided we just use the attribute name and call it as an
543 accessor against the model.
544  
545 =head2 read_attribute_errors_for
546  
547 $model->read_attribute_errors_for('user');
548  
549 Optional. If provided should return an array of validation errors on a field. Allows you to do some
550 final tuning of the error messages. If not provides we expect and errors object as documented in
551 L<Valiant>
552  
553 =head2 errors
554  
555 Optional. Should return an instances of L<Valiant::Errors>. If present will be used to lookup model and
556 attribute errors.
557  
558 Please note this currently is tried to behavior expected from L<Valiant::Errors> but in the future
559 we might try to make this tied to a defined interface rather than this concrete class.
560  
561 =head2 has_errors
562  
563 Optional if you don't use builder methods that are for displaying errors; required otherwise. A boolean
564 that indicates if your model has errors or not. Used to determined if C<error_classes> are merged into
565 C<class> and in a few similar places.
566  
567 =head2 i18n
568  
569 Optional. If provided should return an instance of L<Valiant::I18N>. Used in a few places to support
570 translation tags.
571  
572 =head2 model_name
573  
574 Optional. If provide should return an instance of L<Valiant::Name>. Used in a few places where we
575 default a value to a human readable version of the model name. If you don't have this method we
576 fall back to guessing something based on the string name of the model.
577  
578 =head2 primary_columns
579  
580 Optional. When a model does C<in_storage> and its a nested model, when this method exists we use it to
581 get a list of primary columns for the underlying storage and then add them as hidden fields. This is
582 needed particularly with one - many style relationships so that we find the right record in the storage
583 model to update.
584  
585 B<NOTE>: This method requirement is subject to change. It feels a bit too tightly bound to the idea of
586 and ORM and to L<DBIx::Class> in particular.
587  
588 =head2 is_marked_for_deletion
589  
590 Optional, but should be supported if your model supports a storage backend. A boolean that indicates if
591 the model is currently marked for deletion upon successful validation. Used for things like radio
592 and checkbox collections when the state should be 'unchecked' even if the model is still in storage.
593  
594 =head1 VIEW, CONTROLLER, CONTEXT API
595  
596 Since you will have access potentially to a view, controller or context object you can use the following
597 API in those classes in order to influence the form generation.
598  
599 =head2 formbuilder_class
600  
601 Optional. If provided should return a string that is the name of a class that will be used to instantiate
602 the formbuilder. If not provided we default to L<Valiant::HTML::FormBuilder>.
603  
604 =head2 Generating a URL for the action attribute
605  
606 B<NOTE>: This is a work in progress and subject to change.
607  
608 The following methods are used to generate the URL for the C<action> attribute of the form tag. They
609 are tried in the following order:
610  
611 =over 4
612  
613 =item * $self->create_uri_for
614  
615 =item * $controller->create_uri
616  
617 =item * $self->update_uri_for
618  
619 =item * $controller->update_uri
620  
621 =back
622  
623 ?? Should there be a $c->uri_for_model($path, $model) ??
624  
625 The 'create' methods are checked when the model is not persistent (ie not in storage). The 'update' methods
626 are checked when the model is persistent (ie in storage).
627  
628 We first look for these methods on the view object, then the controller object. If you don't provide any of
629 these objects we will just return C<undef> which will result in no C<action> attribute being generated (You
630 will have to supply it yourself).
631  
632 All the 'update_*' methods are passed the full model path as an argument, which is an arrayref whose last
633 member is the model object and any preceding members are the parent objects or strings used to create a
634 path. For example if you have a deeply nested model you might need both its ID and the ID of its parent
635 to form the correct URL to its update action.
636  
637 All the 'create_*' methods are passed only the 'path' parts of model path, that is the models or
638 strings that are used to create the path. However the C<create_uri_for> (and the C<update_uri_for>)
639 methods always get the full path as an argument. You might use those if you create a central registry
640 mapping models to controllers (perhaps this is a possible good idea for a L<Catalyst> plugin or similar?)
641  
642 This allows you to move more of the request and application bound logic and information out of the view
643 and back to the controller. For an example of how this works with L<Catalyst> you can review the example
644 application in the C<example> directory of this distribution.
645  
646 If you prefer a different behavior you can override the methods C<_edit_action_for_model> and
647 C<_new_action_for_model> in your subclass. Or just set the url directly in the form and ignore this feature
648  
649 =head1 INSTANCE METHODS
650  
651 The following public instance methods are provided by this class.
652  
653 =head2 form_for
654  
655 $f->form_for($name, $model, \%options, \&block);
656 $f->form_for($model, \%options, \&block);
657 $f->form_for($model, \&block);
658 $f->form_for($name, \%options, \&block);
659 $f->form_for($name, \&block);
660  
661 Canonical xample. C<$person> is either an object or the name of an attribute on the C<$view> that
662 will supply the object.
663  
664 $f->form_for($person, sub($fb, $person) {
665 return $fb->label('name'),
666 $fb->input('name');
667 });
668  
669 # Generates something like:
670  
671 <form accept-charset="UTF-8" class="new_person" enctype="multipart/form-data" id="new_person" method="post">
672 <label for="person_name">Name</label>
673 <input id="person_name" name="person.name" type="text" value="John"/>
674 </form>
675  
676 Given a model as described above, wrap a L<Valiant::HTML::FormBuilder> instance around it which
677 provides methods for generating valid HTML form output. This provides a view logic centered
678 method for creating sensible and reusable form controls which include server generated error
679 output from validation.
680  
681 See L<Valiant::HTML::FormBuilder> for more on the formbuilder API.
682  
683 C<\%options> are used to influence the builder creation as well as pass attributes to the
684 generated C<form> tag. Options are as follows:
685  
686 =over 4
687  
688 =item as
689  
690 Supplies the C<name> argument for L<Valiant::HTML::FormBuilder>. This is generally used
691 to set the top namespace for your field IDs and C<name> attributes.
692  
693 =item method
694  
695 Sets the form attribute C<method>. Generally defaults to C<post> or C<patch> (when C<in_storage>
696 is supported and the model is marked for updating of an existing model).
697  
698 =item action
699  
700 Should be the URL that the form with post to. This is a string and used exactly as typed.
701  
702 =item data
703  
704 a hashref of HTML tag <data> attributes.
705  
706 =item class
707  
708 =item style
709  
710 HTML attributes that get merged into the C<html> options (below)
711  
712 =item html
713  
714 a hashref of items that will get rendered as HTML attributes for the form.
715  
716 =item namespace
717  
718 Optional. Can use used to prepend a namespace to your form IDs
719  
720 =item index
721  
722 Optional. When processing a collection model this will be the index of the current
723 model.
724  
725 =item builder
726  
727 The form builder. Defaults to L<Valiant::HTML::FormBuilder>. You can set this if you
728 create your own formbuilder subclass and want to use that. If you don't provide a value
729 we also check the attached view object for a C<formbuilder_class> method and use that if it
730 exists.
731  
732 =item csrf_token
733  
734 Optional. If provided, will be used to generate a hidden field with the name C<csrf_token>.
735 This is useful for CSRF protection. If you don't provide a value we will try to use the
736 C<csrf_token> method on the current view object. If that doesn't exist we will try to use
737 the C<csrf_token> method on the current context object (if one exists). If you are using
738 L<Catalyst> with this you can use L<Catalyst::Plugin::CSRFToken> to generate a token.
739  
740 =item url
741  
742 The url to use for the form action (unless 'action' is defined). Can be a string or a coderef.
743 if a coderef will recieve the view object and model_path as argument.
744  
745 url => sub ($self, $model_path) { $model_path->[-1]->persisted ? path('update') : path('create') }
746  
747 =item edit_url
748  
749 =item new_url
750  
751 A string or coderef that is the url for the form action when the model is persisted (edit_url) or
752 not persisted (new_url). If you provide these we will ignore the C<url> option.
753  
754 edit_url => path('update'), new_url => path('create'),
755  
756 If using the coderef form you get the view and model_path as arguments:
757  
758 edit_url => sub ($self, $model_path) { path('update') }
759  
760 For C<edit_url> the model_path will be the full path to the model, for C<new_url> the model_path
761 will be the path to the model minus the last element (which is the model itself).
762  
763 =item controller
764  
765 The controller object that is associated with the form, if one exists. Defaults to the controller
766 associated with the view if it exists.
767  
768 =item allow_method_names_outside_object
769  
770 By default you can indicate field attribute names in your formbuilder methods that have no map to
771 anything on the model to provide a value. This is useful when you are adding fields that you are
772 making part of the form but are not part of the model to be updated. However this could lead to
773 hard to debug errors in your code if you make a typo in the field attribute name or if you change
774 the model. Setting this to 1 (true) will instead result in an error when you try to use a field
775 name that isn't part of the model.
776  
777 This defaults to false for 'form_for', 'fields_for' and true for 'form_with', 'fields'.
778  
779 =back
780  
781 The last argument should be a reference to a subroute that will receive the created formbuilder
782 object and should return a string, or array of strings that will be flattened and displayed as
783 your form elements. Any strings returns not marked as C<safe> via L<Valiant::HTML::SafeString> will
784 be encoded and turned safe so be sure to mark any raw strings correctly unless you want double
785 encoding issues.
786  
787 You can also provide a string as the first argument to this method and it will be used to set the
788 overall scope of the formbuilder. This is useful if you want to use the same formbuilder for
789 multiple models. For example:
790  
791 $f->form_for('person', sub($fb, $person) {
792 $fb->input('name');
793 });
794  
795 $f->form_for('address', sub($fb, $address) {
796 $fb->input('street');
797 });
798  
799 # Generates something like:
800  
801 <form accept-charset="UTF-8" class="new_person" enctype="multipart/form-data" id="new_person" method="post">
802 <input id="person_name" name="person.name" type="text" value="John"/>
803 </form>
804  
805 <form accept-charset="UTF-8" class="new_address" enctype="multipart/form-data" id="new_address" method="post">
806 <input id="address_street" name="address.street" type="text" value="123 Main St"/>
807 </form>
808  
809 If the string name refers to an attribute on the current view object that attribute will be used
810 to provide model data. Lastly you can pass both a string name and a model object and the string
811 name will be used to set the scope of the formbuilder and the model object will be used to provide
812 the model data.
813  
814 Example:
815  
816 $f->form_for('foo', $person, sub($fb, $person) {
817 return $fb->label('name'),
818 $fb->input('name');
819 });
820  
821 # Generates something like:
822  
823 <form accept-charset="UTF-8" class="new_foo" enctype="multipart/form-data" id="new_foo" method="post">
824 <label for="foo_name">Name</label>
825 <input id="foo_name" name="foo.name" type="text" value="John"/>
826 </form>
827  
828 =head2 fields_for
829  
830 $f->fields_for($name, $model, \%options, \&block);
831 $f->fields_for($model, \%options, \&block);
832 $f->fields_for($model, \&block);
833 $f->fields_for($name, \%options, \&block);
834 $f->fields_for($name, \&block);
835  
836 Create an instance of a formbuilder that represents a model or a namespace in which to build
837 form elements. Its basically <form_for> without the C<form> tag. This is useful for building
838 nested forms or for building forms that are not the top level form.
839  
840 Examples:
841  
842 $f->fields_for($person, sub {
843 my ($fb) = @_;
844 return $fb->input('first_name'),
845 });
846  
847 # <input id="person_first_name" name="person.first_name" type="text" value="aa"/>
848  
849 # Assume that the view has a $person attribute
850 $f->fields_for('person', sub {
851 my ($fb) = @_;
852 return $fb->input('first_name'),
853 });
854  
855 # <input id="person_first_name" name="person.first_name" type="text" value="aa"/>
856  
857 $f->fields_for('foo', $person, sub {
858 my ($fb) = @_;
859 return $fb->input('first_name'),
860 });
861  
862 # <input id="foo_first_name" name="foo.first_name" type="text" value="aa"/>
863  
864 # In this case there is no model for the values or errors, we're just using the
865 # formbuilder to generate the correct names and ids for an empty form.
866  
867 $f->fields_for('foo', sub {
868 my ($fb) = @_;
869 return $fb->input('first_name'),
870 });
871  
872 # <input id="foo_first_name" name="foo.first_name" type="text" value=""/>
873 }
874  
875 done_testing;
876  
877 =head1 SEE ALSO
878
879 L<Valiant>, L<Valiant::HTML::FormBuilder>, L<Valiant::HTML::Util::FormTags>
880  
881 =head1 AUTHOR
882
883 See L<Valiant>
884  
885 =head1 COPYRIGHT & LICENSE
886
887 See L<Valiant>
888  
889 =cut
« Valiant/I18N.pm ↑ Summary Valiant/JSON/JSONBuilder.pm »