Valiant/HTML/Util/Form.pm

filter: Statementstmt 86.8% 185 / 213 Branchbran 64.5% 93 / 144 Conditioncond 45.7% 38 / 83 MC/DCmcdc 12.0% 6 / 50 Subroutinesub 91.1% 31 / 34 Podpod 40.0% 2 / 5 totaltotal 67.1% 355 / 529 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/Validates.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 3 ▶ sub _default_formbuilder_class {
Subroutine _default_formbuilder_class: called CC=2 SCAR=7.1
31 3 my $self = shift;
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 16 ▶sub _dom_class {
Subroutine _dom_class: called CC=2 SCAR=7.0
41 16 my ($self, $model, $prefix) = @_;
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 8 ▶sub _dom_id {
Subroutine _dom_id: called CC=3 SCAR=13.5
47 8 my ($self, $model, $prefix) = @_;
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 8 ▶sub _model_id_for_dom_id {
Subroutine _model_id_for_dom_id: called CC=3 SCAR=14.2
57 8 my $model = shift;
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 59 ▶sub _to_model {
Subroutine _to_model: called CC=4 SCAR=14.6
63 59 my ($self, $model) = @_;
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 28 ▶sub _model_name_from_object_or_class {
Subroutine _model_name_from_object_or_class: called CC=2 SCAR=7.6
72 28 my ($self, $proto) = @_;
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 26 ▶sub model_persisted {
Subroutine model_persisted: called CC=4 SCAR=18.9
Pod undocumented
82 26 my ($self, $model) = @_;
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 10 ▶sub form_for {
Subroutine form_for: called CC=14 SCAR=27.7
Pod documented
99 10 my $self = shift;
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 21 ▶sub _object_from_record_proto {
Subroutine _object_from_record_proto: called CC=5 SCAR=18.6
139 21 my ($self, $proto) = @_;
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 10 ▶sub _apply_form_for_options {
Subroutine _apply_form_for_options: called CC=8 SCAR=20.8
146 10 my ($self, $model, $options) = @_;
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 17 ▶sub form_with {
Subroutine form_with: called CC=22 SCAR=37.1
Pod undocumented
167 17 my $self = shift;
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 17 ▶ $self->form_tag($html_options, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
214 17 my @form_node = $content_block_coderef->($self->view, $builder, $model);
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 13 ▶sub _polymorphic_path_for_model {
Subroutine _polymorphic_path_for_model: called CC=4 SCAR=13.9
238 13 my $self = shift;
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 5 ▶sub _edit_action_for_model {
Subroutine _edit_action_for_model: called CC=6 SCAR=27.1
253 5 my ($self, $model_path, $scope, $controller) = @_;
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 6 ▶sub _new_action_for_model {
Subroutine _new_action_for_model: called CC=6 SCAR=25.4
268 6 my ($self, $model_path, $scope, $controller) = @_;
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 57 ▶sub _instantiate_builder {
Subroutine _instantiate_builder: called CC=10 SCAR=23.6
289 57 my $self = shift;
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}} };
Condition50%1/2
$args{'theme'} || {}
l!l
035
MC/DC0%0/1
$args{'theme'} || {}
$args{'theme'}
Truth table50%1/2
A
A= $args{'theme'}
Aresult
11
00
309 }
310  
311 57 return Module::Runtime::use_module($builder)->new(%args);
312 }
313  
314 17 ▶sub _html_options_for_form_with {
Subroutine _html_options_for_form_with: called CC=15 SCAR=32.2
315 17 my ($self, $url, $model, $options) = @_;
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 10 ▶sub fields_for {
Subroutine fields_for: called CC=6 SCAR=17.9
Pod documented
338 10 my $self = shift;
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 8 ▶sub _object_for_form_builder {
Subroutine _object_for_form_builder: called CC=2 SCAR=8.1
367 8 my ($self, $object) = @_;
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 10 ▶sub fields {
Subroutine fields: called CC=4 SCAR=13.9
Pod undocumented
375 10 my ($self, $scope, $options, $block) = @_;
376  
377 $options = {
378 allow_method_names_outside_object => 1,
379 skip_default_ids => $self->form_with_generates_ids,
380 10 ▶ %{$options||+{}},
Condition50%1/2
$options || {}
l!l
100
MC/DC0%0/1
$options || {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
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 0 ▶sub render {
Subroutine render: not called CC=2 SCAR=17.9
403 0 my ($self, $content_block_coderef) = @_;
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 0 ▶ $self->{util_form}->form_tag($self->{html_options}, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
408 my @form_node = $content_block_coderef->(
409 $self->{view},
410 $self->{fb},
411 $self->{model},
412 0 );
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(view => $view);
448 my $person = Local::Person->new(first_name=>'J', last_name=>'Napiorkowski');
449 $person->validate;
450  
451 $f->form_for($person, sub($view, $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 The required C<view> is an object providing C<safe>, C<raw>, C<escape_html> and
471 C<safe_concat> -- normally the template system instance you are rendering
472 inside (a L<Template::EmbeddedPerl> object works directly). Use the SAME
473 object that renders your page: safe-string marking is class-based, so mixing
474 one template system's safe strings into another's output HTML-escapes them a
475 second time. The content block receives three arguments -- the view, the
476 formbuilder, and the model -- as shown throughout these examples.
477  
478 Alternatively you can create a form object and then render it later:
479  
480 my $form = $f->form_for($person);
481 $form->render(sub($view, $fb, $person) {
482 return $fb->label('first_name'),
483 $fb->input('first_name'),
484 $fb->errors_for('first_name', +{ class=>'invalid-feedback' }),
485 $fb->label('last_name'),
486 $fb->input('last_name'),
487 $fb->errors_for('last_name', +{ class=>'invalid-feedback' });
488 });
489  
490 Would return the same output.
491  
492 =head1 DESCRIPTION
493  
494 Builds on L<Valiant::HTML::Util::TagBuilder> and L<Valiant::HTML::Util::FormTags> to provide a
495 wrapper around a model object that can be used to generate HTML form controls via a formbuilder
496 such as L<Valliant::HTML::FormBuilder>. or a subclass thereof.
497  
498 Like its parent classes, you can provide a view context to the constructor and it will be used
499 to provide attribute values as well as methods used to create safe strings. This is documented
500 extensively in L<Valiant::HTML::Util::TagBuilder> which you should review if you are creating
501 your own view integration. You can see an example view context in L<Valiant::HTML::Util::View>.
502  
503 =head1 INHERITED METHODS
504  
505 This class inherits all methods from L<Valiant::HTML::Util::TagBuilder> and
506 L<Valiant::HTML::Util::FormTags>.
507  
508 =head1 REQUIRED MODEL API
509  
510 This (and L<Valiant::HTML::FormBuilder>) wrap an object model that is expected to do the following
511 interface.
512  
513 =head2 to_model
514  
515 This is an optional method. If this is supported, we call C<to_model> on the wrapped object
516 before using it on the form methods. This allows you to delegate the required API to a secondary
517 object, which can result in a cleaner API depending on your designs and use cases.
518  
519 =head2 in_storage
520  
521 This is an optional method. If your object has a backing storage solution (such as your object
522 is an instance of a DBIC Result Source) you can provide this method to influence how your object
523 form tags are created. If provided this method should return a boolean when if true means that
524 the object is representing data which is stored in the backing storage solution. Please note that
525 this does not mean that the object is synchronized with the backing storage since its possible that
526 the object has been changed by the user.
527  
528 =head2 is_attribute_changed
529  
530 $model->is_attribute_changed($attr); # true or false
531  
532 Optional method. If provided returns a boolean if the attribute has been changed from its initial state,
533 as defined by either being different from the backing store (if it exists) or being changed from its default
534 value when created as a new model.
535  
536 =head2 human_attribute_name
537  
538 $model->human_attribute_name('user'); # User
539  
540 Optional. If provided, uses the model to look up a displayable version of the attribute name, for
541 example used in a label for an input control. If not present we use L<Valiant::HTML::FormTags\_humanize>
542 to create a displayable name from the attribute name.
543  
544 =head2 read_attribute_for_html
545  
546 $model->read_attribute_for_html('user'); # User
547  
548 Optional. If provided must access the string name of the field or attribute and should return the model
549 value for that attribute suitable for HTML form display. You might wish to use this as a way to deflate
550 or otherwise stringify non string values. If not provided we just use the attribute name and call it as an
551 accessor against the model.
552  
553 =head2 read_attribute_errors_for
554  
555 $model->read_attribute_errors_for('user');
556  
557 Optional. If provided should return an array of validation errors on a field. Allows you to do some
558 final tuning of the error messages. If not provides we expect and errors object as documented in
559 L<Valiant>
560  
561 =head2 errors
562  
563 Optional. Should return an instances of L<Valiant::Errors>. If present will be used to lookup model and
564 attribute errors.
565  
566 Please note this currently is tried to behavior expected from L<Valiant::Errors> but in the future
567 we might try to make this tied to a defined interface rather than this concrete class.
568  
569 =head2 has_errors
570  
571 Optional if you don't use builder methods that are for displaying errors; required otherwise. A boolean
572 that indicates if your model has errors or not. Used to determined if C<error_classes> are merged into
573 C<class> and in a few similar places.
574  
575 =head2 i18n
576  
577 Optional. If provided should return an instance of L<Valiant::I18N>. Used in a few places to support
578 translation tags.
579  
580 =head2 model_name
581  
582 Optional. If provide should return an instance of L<Valiant::Name>. Used in a few places where we
583 default a value to a human readable version of the model name. If you don't have this method we
584 fall back to guessing something based on the string name of the model.
585  
586 =head2 primary_columns
587  
588 Optional. When a model does C<in_storage> and its a nested model, when this method exists we use it to
589 get a list of primary columns for the underlying storage and then add them as hidden fields. This is
590 needed particularly with one - many style relationships so that we find the right record in the storage
591 model to update.
592  
593 B<NOTE>: This method requirement is subject to change. It feels a bit too tightly bound to the idea of
594 and ORM and to L<DBIx::Class> in particular.
595  
596 =head2 is_marked_for_deletion
597  
598 Optional, but should be supported if your model supports a storage backend. A boolean that indicates if
599 the model is currently marked for deletion upon successful validation. Used for things like radio
600 and checkbox collections when the state should be 'unchecked' even if the model is still in storage.
601  
602 =head1 VIEW, CONTROLLER, CONTEXT API
603  
604 Since you will have access potentially to a view, controller or context object you can use the following
605 API in those classes in order to influence the form generation.
606  
607 =head2 formbuilder_class
608  
609 Optional. If provided should return a string that is the name of a class that will be used to instantiate
610 the formbuilder. If not provided we default to L<Valiant::HTML::FormBuilder>.
611  
612 =head2 Generating a URL for the action attribute
613  
614 B<NOTE>: This is a work in progress and subject to change.
615  
616 The following methods are used to generate the URL for the C<action> attribute of the form tag. They
617 are tried in the following order:
618  
619 =over 4
620  
621 =item * $self->create_uri_for
622  
623 =item * $controller->create_uri
624  
625 =item * $self->update_uri_for
626  
627 =item * $controller->update_uri
628  
629 =back
630  
631 ?? Should there be a $c->uri_for_model($path, $model) ??
632  
633 The 'create' methods are checked when the model is not persistent (ie not in storage). The 'update' methods
634 are checked when the model is persistent (ie in storage).
635  
636 We first look for these methods on the view object, then the controller object. If you don't provide any of
637 these objects we will just return C<undef> which will result in no C<action> attribute being generated (You
638 will have to supply it yourself).
639  
640 All the 'update_*' methods are passed the full model path as an argument, which is an arrayref whose last
641 member is the model object and any preceding members are the parent objects or strings used to create a
642 path. For example if you have a deeply nested model you might need both its ID and the ID of its parent
643 to form the correct URL to its update action.
644  
645 All the 'create_*' methods are passed only the 'path' parts of model path, that is the models or
646 strings that are used to create the path. However the C<create_uri_for> (and the C<update_uri_for>)
647 methods always get the full path as an argument. You might use those if you create a central registry
648 mapping models to controllers (perhaps this is a possible good idea for a L<Catalyst> plugin or similar?)
649  
650 This allows you to move more of the request and application bound logic and information out of the view
651 and back to the controller. For an example of how this works with L<Catalyst> you can review the example
652 application in the C<example> directory of this distribution.
653  
654 If you prefer a different behavior you can override the methods C<_edit_action_for_model> and
655 C<_new_action_for_model> in your subclass. Or just set the url directly in the form and ignore this feature
656  
657 =head1 INSTANCE METHODS
658  
659 The following public instance methods are provided by this class.
660  
661 =head2 form_for
662  
663 $f->form_for($name, $model, \%options, \&block);
664 $f->form_for($model, \%options, \&block);
665 $f->form_for($model, \&block);
666 $f->form_for($name, \%options, \&block);
667 $f->form_for($name, \&block);
668  
669 Canonical xample. C<$person> is either an object or the name of an attribute on the C<$view> that
670 will supply the object.
671  
672 $f->form_for($person, sub($view, $fb, $person) {
673 return $fb->label('name'),
674 $fb->input('name');
675 });
676  
677 # Generates something like:
678  
679 <form accept-charset="UTF-8" class="new_person" enctype="multipart/form-data" id="new_person" method="post">
680 <label for="person_name">Name</label>
681 <input id="person_name" name="person.name" type="text" value="John"/>
682 </form>
683  
684 Given a model as described above, wrap a L<Valiant::HTML::FormBuilder> instance around it which
685 provides methods for generating valid HTML form output. This provides a view logic centered
686 method for creating sensible and reusable form controls which include server generated error
687 output from validation.
688  
689 See L<Valiant::HTML::FormBuilder> for more on the formbuilder API.
690  
691 C<\%options> are used to influence the builder creation as well as pass attributes to the
692 generated C<form> tag. Options are as follows:
693  
694 =over 4
695  
696 =item as
697  
698 Supplies the C<name> argument for L<Valiant::HTML::FormBuilder>. This is generally used
699 to set the top namespace for your field IDs and C<name> attributes.
700  
701 =item method
702  
703 Sets the form attribute C<method>. Generally defaults to C<post> or C<patch> (when C<in_storage>
704 is supported and the model is marked for updating of an existing model).
705  
706 =item action
707  
708 Should be the URL that the form with post to. This is a string and used exactly as typed.
709  
710 =item data
711  
712 a hashref of HTML tag <data> attributes.
713  
714 =item class
715  
716 =item style
717  
718 HTML attributes that get merged into the C<html> options (below)
719  
720 =item html
721  
722 a hashref of items that will get rendered as HTML attributes for the form.
723  
724 =item namespace
725  
726 Optional. Can use used to prepend a namespace to your form IDs
727  
728 =item index
729  
730 Optional. When processing a collection model this will be the index of the current
731 model.
732  
733 =item builder
734  
735 The form builder. Defaults to L<Valiant::HTML::FormBuilder>. You can set this if you
736 create your own formbuilder subclass and want to use that. If you don't provide a value
737 we also check the attached view object for a C<formbuilder_class> method and use that if it
738 exists.
739  
740 =item csrf_token
741  
742 Optional. If provided, will be used to generate a hidden field with the name C<csrf_token>.
743 This is useful for CSRF protection. If you don't provide a value we will try to use the
744 C<csrf_token> method on the current view object. If that doesn't exist we will try to use
745 the C<csrf_token> method on the current context object (if one exists). If you are using
746 L<Catalyst> with this you can use L<Catalyst::Plugin::CSRFToken> to generate a token.
747  
748 =item url
749  
750 The url to use for the form action (unless 'action' is defined). Can be a string or a coderef.
751 if a coderef will recieve the view object and model_path as argument.
752  
753 url => sub ($self, $model_path) { $model_path->[-1]->persisted ? path('update') : path('create') }
754  
755 =item edit_url
756  
757 =item new_url
758  
759 A string or coderef that is the url for the form action when the model is persisted (edit_url) or
760 not persisted (new_url). If you provide these we will ignore the C<url> option.
761  
762 edit_url => path('update'), new_url => path('create'),
763  
764 If using the coderef form you get the view and model_path as arguments:
765  
766 edit_url => sub ($self, $model_path) { path('update') }
767  
768 For C<edit_url> the model_path will be the full path to the model, for C<new_url> the model_path
769 will be the path to the model minus the last element (which is the model itself).
770  
771 =item controller
772  
773 The controller object that is associated with the form, if one exists. Defaults to the controller
774 associated with the view if it exists.
775  
776 =item allow_method_names_outside_object
777  
778 By default you can indicate field attribute names in your formbuilder methods that have no map to
779 anything on the model to provide a value. This is useful when you are adding fields that you are
780 making part of the form but are not part of the model to be updated. However this could lead to
781 hard to debug errors in your code if you make a typo in the field attribute name or if you change
782 the model. Setting this to 1 (true) will instead result in an error when you try to use a field
783 name that isn't part of the model.
784  
785 This defaults to false for 'form_for', 'fields_for' and true for 'form_with', 'fields'.
786  
787 =back
788  
789 The last argument should be a reference to a subroute that will receive the created formbuilder
790 object and should return a string, or array of strings that will be flattened and displayed as
791 your form elements. Any strings returns not marked as C<safe> via L<Valiant::HTML::SafeString> will
792 be encoded and turned safe so be sure to mark any raw strings correctly unless you want double
793 encoding issues.
794  
795 You can also provide a string as the first argument to this method and it will be used to set the
796 overall scope of the formbuilder. This is useful if you want to use the same formbuilder for
797 multiple models. For example:
798  
799 $f->form_for('person', sub($view, $fb, $person) {
800 $fb->input('name');
801 });
802  
803 $f->form_for('address', sub($view, $fb, $address) {
804 $fb->input('street');
805 });
806  
807 # Generates something like:
808  
809 <form accept-charset="UTF-8" class="new_person" enctype="multipart/form-data" id="new_person" method="post">
810 <input id="person_name" name="person.name" type="text" value="John"/>
811 </form>
812  
813 <form accept-charset="UTF-8" class="new_address" enctype="multipart/form-data" id="new_address" method="post">
814 <input id="address_street" name="address.street" type="text" value="123 Main St"/>
815 </form>
816  
817 If the string name refers to an attribute on the current view object that attribute will be used
818 to provide model data. Lastly you can pass both a string name and a model object and the string
819 name will be used to set the scope of the formbuilder and the model object will be used to provide
820 the model data.
821  
822 Example:
823  
824 $f->form_for('foo', $person, sub($view, $fb, $person) {
825 return $fb->label('name'),
826 $fb->input('name');
827 });
828  
829 # Generates something like:
830  
831 <form accept-charset="UTF-8" class="new_foo" enctype="multipart/form-data" id="new_foo" method="post">
832 <label for="foo_name">Name</label>
833 <input id="foo_name" name="foo.name" type="text" value="John"/>
834 </form>
835  
836 =head2 fields_for
837  
838 $f->fields_for($name, $model, \%options, \&block);
839 $f->fields_for($model, \%options, \&block);
840 $f->fields_for($model, \&block);
841 $f->fields_for($name, \%options, \&block);
842 $f->fields_for($name, \&block);
843  
844 Create an instance of a formbuilder that represents a model or a namespace in which to build
845 form elements. Its basically <form_for> without the C<form> tag. This is useful for building
846 nested forms or for building forms that are not the top level form.
847  
848 Examples:
849  
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 # Assume that the view has a $person attribute
858 $f->fields_for('person', sub {
859 my ($fb) = @_;
860 return $fb->input('first_name'),
861 });
862  
863 # <input id="person_first_name" name="person.first_name" type="text" value="aa"/>
864  
865 $f->fields_for('foo', $person, sub {
866 my ($fb) = @_;
867 return $fb->input('first_name'),
868 });
869  
870 # <input id="foo_first_name" name="foo.first_name" type="text" value="aa"/>
871  
872 # In this case there is no model for the values or errors, we're just using the
873 # formbuilder to generate the correct names and ids for an empty form.
874  
875 $f->fields_for('foo', sub {
876 my ($fb) = @_;
877 return $fb->input('first_name'),
878 });
879  
880 # <input id="foo_first_name" name="foo.first_name" type="text" value=""/>
881 }
882  
883 done_testing;
884  
885 =head1 SEE ALSO
886
887 L<Valiant>, L<Valiant::HTML::FormBuilder>, L<Valiant::HTML::Util::FormTags>
888  
889 =head1 AUTHOR
890
891 See L<Valiant>
892  
893 =head1 COPYRIGHT & LICENSE
894
895 See L<Valiant>
896  
897 =cut
« Valiant/I18N.pm ↑ Summary Valiant/Validates.pm »