| 1 | package Form::Sensible::Form; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 16 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 16 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use Form::Sensible::DelegateConnection; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use Form::Sensible::Validator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 16 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 16 | ▶ | use Class::MOP; ## I don't believe this is required | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | ## a form is a collection of fields. Different form types will work differently. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | ## the Concept basically follows a flow: | ||||||||||||||||||||||||||||
| 14 | ## | ||||||||||||||||||||||||||||
| 15 | ## 1) Form is created. | ||||||||||||||||||||||||||||
| 16 | ## 2) Fields are created and added to the Form | ||||||||||||||||||||||||||||
| 17 | ## 3) Form is rendered | ||||||||||||||||||||||||||||
| 18 | ## 4) user takes action - filling in fields, etc. | ||||||||||||||||||||||||||||
| 19 | ## 5) user indicates an action (form is complete (submit) or cancelled) | ||||||||||||||||||||||||||||
| 20 | ## 6) If cancelled, form triggers cancel action. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | ## 7) if submitted - form calls validators in turn on each of it's fields (and finally a | ||||||||||||||||||||||||||||
| 23 | ## 'last step' validation that runs against all fields, if provided.) | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | ## 7a) if validation fails - failed_validation action is called - most often re-render of the form with messages. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | ## 7b) if validation succeeds - complete_action is called. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 16 | has 'name' => ( | |||||||||||||||||||||||||||
| 30 | is => 'rw', | ||||||||||||||||||||||||||||
| 31 | isa => 'Str', | ||||||||||||||||||||||||||||
| 32 | required => 1, | ||||||||||||||||||||||||||||
| 33 | default => 'form', | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | has '_fields' => ( | ||||||||||||||||||||||||||||
| 37 | is => 'rw', | ||||||||||||||||||||||||||||
| 38 | isa => 'HashRef[Form::Sensible::Field]', | ||||||||||||||||||||||||||||
| 39 | required => 1, | ||||||||||||||||||||||||||||
| 40 | 26 | ▶ | default => sub { return {}; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 16 | lazy => 1, | |||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | has 'field_order' => ( | ||||||||||||||||||||||||||||
| 45 | is => 'rw', | ||||||||||||||||||||||||||||
| 46 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 47 | required => 1, | ||||||||||||||||||||||||||||
| 48 | 26 | ▶ | default => sub { return []; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 16 | lazy => 1, | |||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 16 | has 'renderer' => ( | |||||||||||||||||||||||||||
| 53 | is => 'rw', | ||||||||||||||||||||||||||||
| 54 | isa => 'Form::Sensible::Renderer', | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | has 'render_hints' => ( | ||||||||||||||||||||||||||||
| 58 | is => 'rw', | ||||||||||||||||||||||||||||
| 59 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 60 | required => 1, | ||||||||||||||||||||||||||||
| 61 | 7 | ▶ | default => sub { return {}; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 16 | lazy => 1, | |||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | ## validation hints - FULL form validation | ||||||||||||||||||||||||||||
| 66 | ## runs _after_ field validation. | ||||||||||||||||||||||||||||
| 67 | has 'validation' => ( | ||||||||||||||||||||||||||||
| 68 | is => 'rw', | ||||||||||||||||||||||||||||
| 69 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 70 | required => 1, | ||||||||||||||||||||||||||||
| 71 | 12 | ▶ | default => sub { return {}; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 16 | lazy => 1, | |||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | ## DEPRECATION WARNING. validator_args is deprecated, as is validator_result and validator | ||||||||||||||||||||||||||||
| 76 | ## all of these are replaced via the validation_delegate. DUE FOR REMOVAL 2010-08-30 | ||||||||||||||||||||||||||||
| 77 | has 'validator_args' => ( | ||||||||||||||||||||||||||||
| 78 | is => 'rw', | ||||||||||||||||||||||||||||
| 79 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||
| 80 | required => 1, | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | default => sub { return []; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 82 | 16 | lazy => 1, | |||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 16 | has 'validator_result' => ( | |||||||||||||||||||||||||||
| 86 | is => 'rw', | ||||||||||||||||||||||||||||
| 87 | isa => 'Form::Sensible::Validator::Result', | ||||||||||||||||||||||||||||
| 88 | required => 0, | ||||||||||||||||||||||||||||
| 89 | clearer => '_clear_validator_result', | ||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 16 | has 'validator' => ( | |||||||||||||||||||||||||||
| 93 | is => 'rw', | ||||||||||||||||||||||||||||
| 94 | isa => 'Form::Sensible::Validator', | ||||||||||||||||||||||||||||
| 95 | lazy => 1, | ||||||||||||||||||||||||||||
| 96 | builder => '_create_validator' | ||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | ## calling format: ($form, $fieldname) returns true/false whether the field should be processed. | ||||||||||||||||||||||||||||
| 100 | has 'should_process_field_delegate' => ( | ||||||||||||||||||||||||||||
| 101 | is => 'rw', | ||||||||||||||||||||||||||||
| 102 | isa => 'Form::Sensible::DelegateConnection', | ||||||||||||||||||||||||||||
| 103 | required => 1, | ||||||||||||||||||||||||||||
| 104 | 15 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | 15 | ▶ | return FSConnector(sub { return 1; }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 106 | }, | ||||||||||||||||||||||||||||
| 107 | 16 | lazy => 1, | |||||||||||||||||||||||||||
| 108 | coerce => 1, | ||||||||||||||||||||||||||||
| 109 | # additional options | ||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | has 'validation_delegate' => ( | ||||||||||||||||||||||||||||
| 113 | is => 'rw', | ||||||||||||||||||||||||||||
| 114 | isa => 'Form::Sensible::DelegateConnection', | ||||||||||||||||||||||||||||
| 115 | required => 1, | ||||||||||||||||||||||||||||
| 116 | 11 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 117 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 118 | 11 | my $validator; | |||||||||||||||||||||||||||
| 119 | 11 | ▶ | if (defined($self->validation->{'validator_class'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | $validator = $self->_create_validator(); | |||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||
| 122 | 11 | $validator = Form::Sensible::Validator->new(); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 57 | ▶ | return FSConnector( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 126 | 57 | my $caller = shift; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 57 | return $validator->validate($caller); | |||||||||||||||||||||||||||
| 129 | 11 | }); | |||||||||||||||||||||||||||
| 130 | }, | ||||||||||||||||||||||||||||
| 131 | 16 | lazy => 1, | |||||||||||||||||||||||||||
| 132 | coerce => 1, | ||||||||||||||||||||||||||||
| 133 | # additional options | ||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | ## adds a field to the form. If position is specified, places it at the | ||||||||||||||||||||||||||||
| 137 | ## given position, otherwise places it at the end of the form. | ||||||||||||||||||||||||||||
| 138 | ## returns the new fields position in the form. | ||||||||||||||||||||||||||||
| 139 | ## Note that fieldname does not necessarily need to be the same as $field->name | ||||||||||||||||||||||||||||
| 140 | ## though you may get odd behavior if you accidentally insert multiple fields with | ||||||||||||||||||||||||||||
| 141 | ## the same $field->name | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 72 | ▶ | sub add_field { | ||||||||||||||||||||||||||
|
Subroutine
add_field: called CC=11 SCAR=26.7
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 72 | my ($self, $field, $fieldname, $position) = @_; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 72 | ▶ | if (!$fieldname) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 18 | ▶ | if (ref($field) =~ /^Form::Sensible::Field/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 148 | 9 | $fieldname = $field->name; | |||||||||||||||||||||||||||
| 149 | } elsif (ref($field) eq 'HASH') { | ||||||||||||||||||||||||||||
| 150 | 9 | $fieldname = $field->{'name'}; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 72 | ▶ | if (defined($self->_fields->{$fieldname})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | $self->remove_field($fieldname); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | ## this will cause an unblessed hash passed to add_field to auto-create | ||||||||||||||||||||||||||||
| 159 | ## the appropriate field type. | ||||||||||||||||||||||||||||
| 160 | 72 | ▶ | if (ref($field) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 63 | my $newfield = $field; | |||||||||||||||||||||||||||
| 162 | 63 | $field = Form::Sensible::Field->create_from_flattened($newfield); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 72 | ▶ | if ($field->isa('Form::Sensible::Field::SubForm') && $field->form == $self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$field->isa("Form::Sensible::Field::SubForm") and $field->form == $self
MC/DC0%0/2
$field->isa("Form::Sensible::Field::SubForm") and $field->form == $self
$field->isa("Form::Sensible::Field::SubForm")
$field->form == $self
Truth table66%2/3
A and B
A= $field->isa("Form::Sensible::Field::SubForm")B= $field->form == $self
| |||||||||||||||||||||||||||||
| 166 | 0 | croak "Unable to add sub-form. sub-form is the same as me. Infinite recursion will occur, dying now instead of later."; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 72 | $self->_fields->{$fieldname} = $field; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | ## if position is larger than the current form size | ||||||||||||||||||||||||||||
| 172 | ## reset position so the field is added to the end of the | ||||||||||||||||||||||||||||
| 173 | ## list. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | if ($position && $position > $#{$self->field_order}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$position and $position > $#{$self->field_order;}
MC/DC0%0/2
$position and $position > $#{$self->field_order;}
$position
$position > $#{$self->field_order;}
Truth table33%1/3
A and B
A= $positionB= $position > $#{$self->field_order;}
| |||||||||||||||||||||||||||||
| 176 | 0 | $position = undef; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 72 | ▶ | if (!$position) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 72 | push @{$self->field_order}, $fieldname; | |||||||||||||||||||||||||||
| 180 | 72 | $position = $#{$self->field_order}; | |||||||||||||||||||||||||||
| 181 | } else { | ||||||||||||||||||||||||||||
| 182 | 0 | splice @{$self->field_order}, $position, 0, $fieldname; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 72 | return $position; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | ## removes a field from the form by name. Returns the removed field | ||||||||||||||||||||||||||||
| 188 | ## or undef if the field was not present in the form. | ||||||||||||||||||||||||||||
| 189 | 0 | ▶ | sub remove_field { | ||||||||||||||||||||||||||
|
Subroutine
remove_field: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 0 | my ($self, $fieldname) = @_; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | my $field = $self->field($fieldname); | |||||||||||||||||||||||||||
| 193 | 0 | delete($self->_fields->{$fieldname}); | |||||||||||||||||||||||||||
| 194 | 0 | foreach my $i (0..$#{$self->field_order}) { | |||||||||||||||||||||||||||
| 195 | 0 | ▶ | if ($self->field_order->[$i] eq $fieldname ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | splice @{$self->field_order}, $i, 1; | |||||||||||||||||||||||||||
| 197 | 0 | last; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 0 | return $field; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | ## moves a field from one position to another. This may not work the way | ||||||||||||||||||||||||||||
| 204 | ## I think because the field is removed and then replaced, depending on it's | ||||||||||||||||||||||||||||
| 205 | ## original position, it may not appear where the user expects. Need to work | ||||||||||||||||||||||||||||
| 206 | ## on that. | ||||||||||||||||||||||||||||
| 207 | 0 | ▶ | sub reorder_field { | ||||||||||||||||||||||||||
|
Subroutine
reorder_field: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 208 | 0 | my ($self, $fieldname, $newposition) = @_; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | my $field = $self->remove_field($fieldname); | |||||||||||||||||||||||||||
| 211 | 0 | return $self->add_field($field, $fieldname, $newposition); | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | ## returns the field requested or undef if a field by that name is not found | ||||||||||||||||||||||||||||
| 215 | 503 | ▶ | sub field { | ||||||||||||||||||||||||||
|
Subroutine
field: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 216 | 503 | my ($self, $fieldname) = @_; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 503 | return $self->_fields->{$fieldname}; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | ## returns a hash containing all the fields in the current form | ||||||||||||||||||||||||||||
| 222 | ## fields is DEPRECATED. DO NOT USE IT. | ||||||||||||||||||||||||||||
| 223 | 0 | ▶ | sub fields { | ||||||||||||||||||||||||||
|
Subroutine
fields: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 224 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | return { %{$self->_fields} }; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | ## Returns an array of all the fields in the form in field_order | ||||||||||||||||||||||||||||
| 230 | ## obeying should_process_field_delegate. | ||||||||||||||||||||||||||||
| 231 | 67 | ▶ | sub get_fields { | ||||||||||||||||||||||||||
|
Subroutine
get_fields: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 232 | 67 | my $self = shift; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 67 | my @active_fields; | |||||||||||||||||||||||||||
| 235 | 67 | foreach my $fieldname (@{$self->field_order}) { | |||||||||||||||||||||||||||
| 236 | 310 | ▶ | if ($self->should_process_field_delegate->($self, $fieldname)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 310 | push @active_fields, $self->field($fieldname); | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | 67 | return @active_fields; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | ## returns the fieldnames in the current form in their presentation order | ||||||||||||||||||||||||||||
| 245 | 65 | ▶ | sub fieldnames { | ||||||||||||||||||||||||||
|
Subroutine
fieldnames: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 246 | 65 | my $self = shift; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 65 | return @{$self->field_order}; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | ▶ | sub _create_validator { | ||||||||||||||||||||||||||
|
Subroutine
_create_validator: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 252 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 0 | my $validator; | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | ## first see if we have had our validator class overridden | ||||||||||||||||||||||||||||
| 257 | my $classname; | ||||||||||||||||||||||||||||
| 258 | 0 | ▶ | if (defined($self->validation->{'validator_class'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | ## we have, so get the class name and instantiate an object of that class | ||||||||||||||||||||||||||||
| 260 | 0 | $classname = $self->validation->{'validator_class'}; | |||||||||||||||||||||||||||
| 261 | } else { | ||||||||||||||||||||||||||||
| 262 | ## otherwise, we create a Form::Sensible::Validator object. | ||||||||||||||||||||||||||||
| 263 | 0 | $classname = '+Form::Sensible::Validator'; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | 0 | ▶ | if ($classname =~ /^\+(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 0 | $classname = $1; | |||||||||||||||||||||||||||
| 267 | } else { | ||||||||||||||||||||||||||||
| 268 | 0 | $classname = 'Form::Sensible::Validator::' . $classname; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | 0 | Class::MOP::load_class($classname); | |||||||||||||||||||||||||||
| 271 | 0 | $validator = $classname->new(@{$self->validator_args}); | |||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 0 | return $validator; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 57 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 278 | 57 | my ($self) = @_; | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 57 | my $result = $self->validation_delegate->($self); | |||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | ## for now - validation_results() are set automatically if validate is run from the form. | ||||||||||||||||||||||||||||
| 283 | ## this is deprecated and will be removed. DUE FOR REMOVAL 2010-07-30 | ||||||||||||||||||||||||||||
| 284 | 57 | $self->validator_result($result); | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 57 | return $result; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 0 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 290 | 0 | my ($self) = shift; | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 0 | ▶ | if ($self->renderer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 0 | return $self->renderer->render($self, @_); | |||||||||||||||||||||||||||
| 294 | } else { | ||||||||||||||||||||||||||||
| 295 | 0 | croak __PACKAGE__ . '->render() called but no renderer defined for this form (' . $self->name . ')'; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 62 | ▶ | sub set_values { | ||||||||||||||||||||||||||
|
Subroutine
set_values: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 300 | 62 | my ($self, $values) = @_; | |||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 62 | foreach my $fieldname ( $self->fieldnames ) { | |||||||||||||||||||||||||||
| 303 | 291 | ▶ | if (exists($values->{$fieldname})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | 115 | $self->field($fieldname)->value($values->{$fieldname}); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 0 | ▶ | sub get_all_values { | ||||||||||||||||||||||||||
|
Subroutine
get_all_values: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 310 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 0 | my $value_hash = {}; | |||||||||||||||||||||||||||
| 313 | 0 | foreach my $fieldname ( $self->fieldnames ) { | |||||||||||||||||||||||||||
| 314 | 0 | $value_hash->{$fieldname} = $self->field($fieldname)->value(); | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | 0 | return $value_hash; | |||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 2 | ▶ | sub clear_state { | ||||||||||||||||||||||||||
|
Subroutine
clear_state: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 320 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 2 | foreach my $fieldname ( $self->fieldnames ) { | |||||||||||||||||||||||||||
| 323 | 7 | $self->field($fieldname)->clear_state(); | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | ## DUE FOR REMOVAL 2010-07-30 | ||||||||||||||||||||||||||||
| 327 | 2 | $self->_clear_validator_result(); | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 1 | ▶ | sub delegate_all_field_values { | ||||||||||||||||||||||||||
|
Subroutine
delegate_all_field_values: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 331 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 332 | 1 | my $value_delegate = shift; | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 1 | foreach my $field ($self->get_fields()) { | |||||||||||||||||||||||||||
| 335 | 3 | $field->value_delegate( $value_delegate ); | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 1 | ▶ | sub delegate_all_field_values_to_hashref { | ||||||||||||||||||||||||||
|
Subroutine
delegate_all_field_values_to_hashref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 340 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 341 | 1 | my $hashref = shift; | |||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | # We only need one value delegate object because it gets the name from the field's name. | ||||||||||||||||||||||||||||
| 344 | # So we save a bit of memory by not creating a separate delegate object for each field | ||||||||||||||||||||||||||||
| 345 | 34 | ▶ | my $value_delegate = FSConnector( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 346 | 34 | my $caller = shift; | |||||||||||||||||||||||||||
| 347 | 34 | my $fieldname = $caller->name(); | |||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 34 | ▶ | if ($#_ > -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 8 | ▶ | if (ref($_[0]) eq 'ARRAY' && !($caller->accepts_multiple)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_[0] eq "ARRAY" and not $caller->accepts_multiple
MC/DC50%1/2
ref $_[0] eq "ARRAY" and not $caller->accepts_multiple
ref $_[0] eq "ARRAY"
not $caller->accepts_multiple
Truth table66%2/3
A and not(B)
A= ref $_[0] eq "ARRAY"B= not $caller->accepts_multiple
| |||||||||||||||||||||||||||||
| 351 | 1 | $hashref->{$fieldname} = $_[0]->[0]; | |||||||||||||||||||||||||||
| 352 | } else { | ||||||||||||||||||||||||||||
| 353 | 7 | $hashref->{$fieldname} = $_[0]; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | 34 | return $hashref->{$fieldname}; | |||||||||||||||||||||||||||
| 357 | 1 | }); | |||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 1 | $self->delegate_all_field_values($value_delegate); | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 1 | ▶ | sub flatten { | ||||||||||||||||||||||||||
|
Subroutine
flatten: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 365 | 1 | my ($self, $template_only) = @_; | |||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 1 | my $form_hash = { | |||||||||||||||||||||||||||
| 368 | 'name' => $self->name, | ||||||||||||||||||||||||||||
| 369 | 'render_hints' => $self->render_hints, | ||||||||||||||||||||||||||||
| 370 | 'validation' => $self->validation, | ||||||||||||||||||||||||||||
| 371 | 'field_order' => $self->field_order, | ||||||||||||||||||||||||||||
| 372 | }; | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 1 | $form_hash->{'fields'} = {}; | |||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 1 | foreach my $fieldname ( $self->fieldnames ) { | |||||||||||||||||||||||||||
| 377 | 4 | $form_hash->{'fields'}{$fieldname} = $self->field($fieldname)->flatten($template_only); | |||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | 1 | return $form_hash; | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 16 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 383 | 16 | 1; | |||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | __END__ | ||||||||||||||||||||||||||||