| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
|
| 5 |
# type || p1 fif || p1 value || p2 validation || p2 fif || p2 value |
| 6 |
# (1) none init_obj init_obj params params params |
| 7 |
# (2) infl init_obj init_obj inflated params inflated |
| 8 |
# (3) defl deflated init_obj params params params |
| 9 |
# (4) defl-ffv deflated init_obj params deflated params |
| 10 |
# (5) infl-defl deflated init_obj inflated params inflated |
| 11 |
# (6) infl_def inflated(d) inflated(d) params params params |
| 12 |
# (7) defl_val init_obj init_obj params params deflated(v) |
| 13 |
# (8) def-val inflated(d) inflated(d) params params deflated(v) |
| 14 |
|
| 15 |
# inflated => 'inflate_method' |
| 16 |
# deflated => 'deflate_method' |
| 17 |
# inflated(d) => 'inflate_default_method' |
| 18 |
# deflated(v) => 'deflate_value_method' |
| 19 |
|
| 20 |
# An inflation method changes the format of the value used by validation |
| 21 |
# A deflation method changes the format of the fill-in-form string |
| 22 |
|
| 23 |
# These two are "conveniences" for munging data passed in and out of a form |
| 24 |
# The same effect could be achieved by modifying the value in the init_object or item |
| 25 |
# before passing it in, and modifying it once returned, so it's primarily |
| 26 |
# useful for database rows. |
| 27 |
# |
| 28 |
# An inflate_default method changes the 'value' retrieved from a default source |
| 29 |
# (default, init_object, item) |
| 30 |
# A deflate_value method changes the format of the value available after validation |
| 31 |
|
| 32 |
{ |
| 33 |
# plain field with no inflation or deflation |
| 34 |
# type || p1 fif || p1 value || p2 validation || p2 fif || p2 value |
| 35 |
# none init_obj init_obj params params params |
| 36 |
{ |
| 37 |
package Test::Form1; |
| 38 |
use HTML::FormHandler::Moose; |
| 39 |
extends 'HTML::FormHandler'; |
| 40 |
|
| 41 |
has_field 'foo'; |
| 42 |
sub validate_foo { |
| 43 |
my ( $self, $field ) = @_; |
| 44 |
$self->add_error unless $field->value eq 'fromparams'; |
| 45 |
} |
| 46 |
} |
| 47 |
my $form = Test::Form1->new; |
| 48 |
my $init_obj = { foo => 'initialfoo' }; |
| 49 |
my $params = { foo => 'fromparams' }; |
| 50 |
$form->process( init_object => $init_obj, params => {} ); |
| 51 |
is_deeply( $form->fif, $init_obj, 'fif matches init_object' ); |
| 52 |
is_deeply( $form->value, $init_obj, 'value matches init_object' ); |
| 53 |
$form->process( init_object => $init_obj, params => $params ); |
| 54 |
ok( $form->validated, 'form validated' ); |
| 55 |
is_deeply( $form->fif, $params, 'fif matches params' ); |
| 56 |
is_deeply( $form->value, $params, 'value matches params' ); |
| 57 |
} |
| 58 |
|
| 59 |
{ |
| 60 |
# field with only an inflation |
| 61 |
# value for validation is inflated; value returned is inflated; |
| 62 |
# type || p1 fif || p1 value || p2 validation || p2 fif || p2 value |
| 63 |
# infl init_obj init_obj inflated params inflated |
| 64 |
{ |
| 65 |
package Test::Form2; |
| 66 |
use HTML::FormHandler::Moose; |
| 67 |
extends 'HTML::FormHandler'; |
| 68 |
|
| 69 |
has_field 'foo' => ( inflate_method => \&inflate_foo ); |
| 70 |
sub inflate_foo { 'inflatedfoo' } |
| 71 |
sub validate_foo { |
| 72 |
my ( $self, $field ) = @_; |
| 73 |
$self->add_error unless $field->value eq 'inflatedfoo'; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
my $form = Test::Form2->new; |
| 78 |
my $init_obj = { foo => 'initialfoo' }; |
| 79 |
my $params = { foo => 'fromparams' }; |
| 80 |
$form->process( init_object => $init_obj, params => {} ); |
| 81 |
is_deeply( $form->fif, $init_obj, 'fif matches init_object' ); |
| 82 |
is_deeply( $form->value, $init_obj, 'value matches init_object' ); |
| 83 |
$form->process( init_object => $init_obj, params => $params ); |
| 84 |
ok( $form->validated, 'form validated' ); |
| 85 |
is_deeply( $form->fif, $params, 'fif matches params' ); |
| 86 |
is_deeply( $form->value, { foo => 'inflatedfoo' }, 'value is inflated' ); |
| 87 |
} |
| 88 |
|
| 89 |
{ |
| 90 |
# field with only a deflation |
| 91 |
# type || p1 fif || p1 value || p2 validation || p2 fif || p2 value |
| 92 |
# defl deflated init_obj params params params |
| 93 |
{ |
| 94 |
package Test::Form3; |
| 95 |
use HTML::FormHandler::Moose; |
| 96 |
extends 'HTML::FormHandler'; |
| 97 |
|
| 98 |
has_field 'foo' => ( deflate_method => \&deflate_foo ); |
| 99 |
sub deflate_foo { 'deflatedfoo' } |
| 100 |
sub validate_foo { |
| 101 |
my ( $self, $field ) = @_; |
| 102 |
$self->add_error unless $field->value eq 'fromparams'; |
| 103 |
} |
| 104 |
} |
| 105 |
my $form = Test::Form3->new; |
| 106 |
my $init_obj = { foo => 'initialfoo' }; |
| 107 |
my $params = { foo => 'fromparams' }; |
| 108 |
$form->process( init_object => $init_obj, params => {} ); |
| 109 |
is_deeply( $form->fif, { foo => 'deflatedfoo' }, 'fif is deflated foo' ); |
| 110 |
is_deeply( $form->value, { foo => 'initialfoo' }, 'value is initial foo' ); |
| 111 |
$form->process( init_object => $init_obj, params => $params ); |
| 112 |
ok( $form->validated, 'form validated' ); |
| 113 |
is_deeply( $form->fif, $params, 'fif matches params' ); |
| 114 |
is_deeply( $form->value, $params, 'value matches params' ); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
{ |
| 119 |
# field with only a deflation; 'fif_from_value' => 1 |
| 120 |
# type || p1 fif || p1 value || p2 validation || p2 fif || p2 value |
| 121 |
# defl-ffv deflated init_obj params deflated params |
| 122 |
{ |
| 123 |
package Test::Form4; |
| 124 |
use HTML::FormHandler::Moose; |
| 125 |
extends 'HTML::FormHandler'; |
| 126 |
|
| 127 |
has_field 'foo' => ( deflate_method => \&deflate_foo, |
| 128 |
'fif_from_value' => 1 ); |
| 129 |
sub deflate_foo { 'deflatedfoo' } |
| 130 |
sub validate_foo { |
| 131 |
my ( $self, $field ) = @_; |
| 132 |
$self->add_error unless $field->value eq 'fromparams'; |
| 133 |
} |
| 134 |
} |
| 135 |
my $form = Test::Form4->new; |
| 136 |
my $init_obj = { foo => 'initialfoo' }; |
| 137 |
my $params = { foo => 'fromparams' }; |
| 138 |
$form->process( init_object => $init_obj, params => {} ); |
| 139 |
is_deeply( $form->fif, { foo => 'deflatedfoo' }, 'fif is deflated foo' ); |
| 140 |
is_deeply( $form->value, { foo => 'initialfoo' }, 'value is initial foo' ); |
| 141 |
$form->process( init_object => $init_obj, params => $params ); |
| 142 |
ok( $form->validated, 'form validated' ); |
| 143 |
is_deeply( $form->fif, { foo => 'deflatedfoo' }, 'fif is deflated' ); |
| 144 |
is_deeply( $form->value, $params, 'value matches params' ); |
| 145 |
} |
| 146 |
|
| 147 |
{ |
| 148 |
# field with an inflation and deflation |
| 149 |
# both fif and value are 'deflated'. |
| 150 |
# type || p1 fif || p1 value || p2 validation || p2 fif || p2 value |
| 151 |
# infl-defl deflated init_obj inflated params inflated |
| 152 |
{ |
| 153 |
package Test::Form5; |
| 154 |
use HTML::FormHandler::Moose; |
| 155 |
extends 'HTML::FormHandler'; |
| 156 |
|
| 157 |
has_field 'foo' => ( inflate_method => \&inflate_foo, deflate_method => \&deflate_foo ); |
| 158 |
sub inflate_foo { 'inflatedfoo' } |
| 159 |
sub deflate_foo { 'deflatedfoo' } |
| 160 |
sub validate_foo { |
| 161 |
my ( $self, $field ) = @_; |
| 162 |
$self->add_error unless $field->value eq 'inflatedfoo'; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
my $form = Test::Form5->new; |
| 167 |
my $init_obj = { foo => 'initialfoo' }; |
| 168 |
my $params = { foo => 'fromparams' }; |
| 169 |
$form->process( init_object => $init_obj, params => {} ); |
| 170 |
is_deeply( $form->fif, { foo => 'deflatedfoo' }, 'fif is deflated' ); |
| 171 |
is_deeply( $form->value, $init_obj, 'value is initial' ); |
| 172 |
$form->process( init_object => $init_obj, params => $params ); |
| 173 |
ok( $form->validated, 'form validated' ); |
| 174 |
is_deeply( $form->fif, $params, 'fif matches params' ); |
| 175 |
is_deeply( $form->value, { foo => 'inflatedfoo' }, 'value is inflated' ); |
| 176 |
} |
| 177 |
|
| 178 |
{ |
| 179 |
# field with only 'inflate_default' |
| 180 |
# type || p1 fif || p1 value || p2 validation || p2 fif || p2 value |
| 181 |
# infl_def inflated inflated params params params |
| 182 |
{ |
| 183 |
package Test::Form6; |
| 184 |
use HTML::FormHandler::Moose; |
| 185 |
extends 'HTML::FormHandler'; |
| 186 |
|
| 187 |
has_field 'foo' => ( inflate_default_method => sub { 'infl_def_foo' } ); |
| 188 |
sub validate_foo { |
| 189 |
my ( $self, $field ) = @_; |
| 190 |
$self->add_error unless $field->value eq 'fromparams'; |
| 191 |
} |
| 192 |
} |
| 193 |
my $form = Test::Form6->new; |
| 194 |
my $init_obj = { foo => 'initialfoo' }; |
| 195 |
my $params = { foo => 'fromparams' }; |
| 196 |
$form->process( init_object => $init_obj, params => {} ); |
| 197 |
is_deeply( $form->fif, { foo => 'infl_def_foo' }, 'fif matches inflate_default' ); |
| 198 |
is_deeply( $form->value, { foo => 'infl_def_foo' }, 'value matches init_object' ); |
| 199 |
$form->process( init_object => $init_obj, params => $params ); |
| 200 |
ok( $form->validated, 'form validated' ); |
| 201 |
is_deeply( $form->fif, $params, 'fif matches params' ); |
| 202 |
is_deeply( $form->value, $params, 'value matches params' ); |
| 203 |
} |
| 204 |
|
| 205 |
{ |
| 206 |
# field with only a 'deflate_value' method |
| 207 |
# type || p1 fif || p1 value || p2 validation || p2 fif || p2 value |
| 208 |
# defl_val init_obj init_obj params params deflated |
| 209 |
{ |
| 210 |
package Test::Form7; |
| 211 |
use HTML::FormHandler::Moose; |
| 212 |
extends 'HTML::FormHandler'; |
| 213 |
|
| 214 |
has_field 'foo' => ( deflate_value_method => sub { 'defl_val_foo' } ); |
| 215 |
sub validate_foo { |
| 216 |
my ( $self, $field ) = @_; |
| 217 |
$self->add_error unless $field->value eq 'fromparams'; |
| 218 |
} |
| 219 |
} |
| 220 |
my $form = Test::Form7->new; |
| 221 |
my $init_obj = { foo => 'initialfoo' }; |
| 222 |
my $params = { foo => 'fromparams' }; |
| 223 |
$form->process( init_object => $init_obj, params => {} ); |
| 224 |
is_deeply( $form->fif, $init_obj, 'fif matches init_object' ); |
| 225 |
is_deeply( $form->value, $init_obj, 'value matches init_object' ); |
| 226 |
$form->process( init_object => $init_obj, params => $params ); |
| 227 |
ok( $form->validated, 'form validated' ); |
| 228 |
is_deeply( $form->fif, $params, 'fif matches params' ); |
| 229 |
is_deeply( $form->value, { foo => 'defl_val_foo' }, 'value is deflated by deflate_value' ); |
| 230 |
} |
| 231 |
|
| 232 |
{ |
| 233 |
# field with 'inflate_default' and a 'deflate_value' method |
| 234 |
# type || p1 fif || p1 value || p2 validation || p2 fif || p2 value |
| 235 |
# def-val inflated inflated params params deflated |
| 236 |
{ |
| 237 |
package Test::Form8; |
| 238 |
use HTML::FormHandler::Moose; |
| 239 |
extends 'HTML::FormHandler'; |
| 240 |
|
| 241 |
has_field 'foo' => ( inflate_default_method => sub { 'infl_def_foo' }, |
| 242 |
deflate_value_method => sub { 'defl_val_foo' } ); |
| 243 |
sub validate_foo { |
| 244 |
my ( $self, $field ) = @_; |
| 245 |
$self->add_error unless $field->value eq 'fromparams'; |
| 246 |
} |
| 247 |
} |
| 248 |
my $form = Test::Form8->new; |
| 249 |
my $init_obj = { foo => 'initialfoo' }; |
| 250 |
my $params = { foo => 'fromparams' }; |
| 251 |
$form->process( init_object => $init_obj, params => {} ); |
| 252 |
is_deeply( $form->fif, { foo => 'infl_def_foo' }, 'fif matches init_object' ); |
| 253 |
is_deeply( $form->value, { foo => 'infl_def_foo' }, 'value matches init_object' ); |
| 254 |
$form->process( init_object => $init_obj, params => $params ); |
| 255 |
ok( $form->validated, 'form validated' ); |
| 256 |
is_deeply( $form->fif, $params, 'fif matches params' ); |
| 257 |
is_deeply( $form->value, { foo => 'defl_val_foo' }, 'value from deflate_value' ); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
done_testing; |