Skip to content

Commit

Permalink
Import of CFRANKS/HTML-FormFu-0.06001 from CPAN.
Browse files Browse the repository at this point in the history
gitpan-cpan-distribution: HTML-FormFu
gitpan-cpan-version:      0.06001
gitpan-cpan-path:         CFRANKS/HTML-FormFu-0.06001.tar.gz
gitpan-cpan-author:       CFRANKS
gitpan-cpan-maturity:     released
  • Loading branch information
Carl Franks authored and Gitpan committed Oct 26, 2014
1 parent 3c235e9 commit ff22015
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 8 deletions.
7 changes: 7 additions & 0 deletions Changes
@@ -1,3 +1,10 @@
0.06001 2010-01-08

- Fixed issue with Model::HashRef where form fields with an underscore
and overlapping name (e.g. 'foo' and 'foo_bar') were causing problems

- Fix test suite year issue.

0.06000 2009-12-10

- New get_parent() method that traverses the parent hierarchy, returning
Expand Down
2 changes: 1 addition & 1 deletion META.yml
Expand Up @@ -59,4 +59,4 @@ requires:
perl: 5.8.1
resources:
license: http://dev.perl.org/licenses/
version: 0.06000
version: 0.06001
2 changes: 1 addition & 1 deletion lib/HTML/FormFu.pm
Expand Up @@ -112,7 +112,7 @@ __PACKAGE__->mk_inherited_merging_accessors(qw( tt_args config_callback ));
*plugins = \&plugin;
*add_plugins = \&add_plugin;

our $VERSION = '0.06000';
our $VERSION = '0.06001';
$VERSION = eval $VERSION;

Class::C3::initialize();
Expand Down
4 changes: 2 additions & 2 deletions lib/HTML/FormFu/Constraint/Repeatable/Any.pm
Expand Up @@ -27,11 +27,11 @@ sub process {
my $repeatable = $field->get_parent({ type => 'Repeatable' });
my $pass;

my $original_name = $field->original_name;
my $original_name = $field->original_name || '';

my @fields =
grep { $_->get_parent({ type => 'Repeatable' }) == $repeatable }
grep { $_->original_name eq $original_name }
grep { ( $_->original_name || '' ) eq $original_name }
@{ $repeatable->get_fields };

my $increment_field_names = $repeatable->increment_field_names;
Expand Down
25 changes: 24 additions & 1 deletion lib/HTML/FormFu/Model/HashRef.pm
Expand Up @@ -105,9 +105,10 @@ sub update { shift->create(@_) }
sub create {
my $self = shift;
if($self->form->submitted) {
my $input = _escape_hash($self->form->input);
my $hf = new Hash::Flatten(
{ ArrayDelimiter => '_', HashDelimiter => '.' } );
my $input = $hf->unflatten($self->form->input);
$input = _unescape_hash($hf->unflatten($self->form->input));
$self->default_values( $self->_unfold_repeatable($self->form, $input) );
}
$self->form->render_data;
Expand Down Expand Up @@ -178,6 +179,28 @@ sub _as_object_get {
$self->flatten ? $names : $hf->unflatten($names) );
}

sub _escape_hash {
my $hash = shift;
my $method = shift || \&_escape_name;
return $hash unless(ref $hash);
foreach my $k (keys %$hash) {
my $v = delete $hash->{$k};
if(ref $v eq 'HASH') {
$hash->{$method->($k)} = _escape_hash($v, $method);
} elsif(ref $v eq 'ARRAY') {
$hash->{$method->($k)} = [ map { _escape_hash($_, $method) } @$v];
} else {
$hash->{$method->($k)} = $v;
}
}
return $hash;
}


sub _unescape_hash {
return _escape_hash(shift, \&_unescape_name);
}

sub _escape_name {
my $name = shift;
$name =~ s/_/\\_/g;
Expand Down
4 changes: 2 additions & 2 deletions t/elements/date_undef.t
Expand Up @@ -8,12 +8,12 @@ use HTML::FormFu;
my $form = HTML::FormFu->new(
{ tt_args => { INCLUDE_PATH => 'share/templates/tt/xhtml' } } );

$form->populate({elements => [{type => "Date", name => "foo", default => '30-08-2009'}]});
$form->populate({elements => [{type => "Date", name => "foo", year => {list => [2009]}, default => '30-08-2009'}]});

$form->process;

like($form->render, qr/value="2009" selected="selected"/);

$form->get_field('foo')->default(undef);

like($form->render, qr/value="2009">/);
like($form->render, qr/value="2009">/);
55 changes: 54 additions & 1 deletion t/model/hashref_escaping.t
@@ -1,8 +1,9 @@
use strict;
use warnings;

use Test::More tests => 5;
use Test::More;

use HTML::FormFu;
use HTML::FormFu::Model::HashRef;

my %test = (
Expand All @@ -17,3 +18,55 @@ while ( my ( $k, $v ) = each %test ) {
}

is( HTML::FormFu::Model::HashRef::_unescape_name('foo\\_bar'), 'foo_bar' );

is_deeply(
HTML::FormFu::Model::HashRef::_escape_hash(
{
'name_2' => 'foo',
'name_bar_foo' => 'bar',
'name_2_bar' => 'baz',
'name_2.bar' => { 'bas_z' => 1 },
'bar_z' => [ { foo_w => 1, foo_2 => 2 } ],
}
),
{
'name_2' => 'foo',
'name\\_bar\\_foo' => 'bar',
'name\\_2\\_bar' => 'baz',
'name_2.bar' => { 'bas\\_z' => 1 },
'bar\\_z' => [ { 'foo\\_w' => 1, foo_2 => 2 } ]
}
);

is_deeply(
HTML::FormFu::Model::HashRef::_unescape_hash(
{
'name_2' => 'foo',
'name\\_bar\\_foo' => 'bar',
'name\\_2\\_bar' => 'baz',
'name_2.bar' => { 'bas\\_z' => 1 },
'bar\\_z' => [ { 'foo\\_w' => 1, foo_2 => 2 } ]

}
),
{
'name_2' => 'foo',
'name_bar_foo' => 'bar',
'name_2_bar' => 'baz',
'name_2.bar' => { 'bas_z' => 1 },
'bar_z' => [ { foo_w => 1, foo_2 => 2 } ],
}
);

my $form = HTML::FormFu->new;
$form->populate(
{
elements =>
[ { name => 'foo' }, { name => 'bar' }, { name => 'foo_bar' } ]
}
);
$form->process( { foo => 1, bar => 2, foo_bar => 3 } );

is_deeply($form->model('HashRef')->create, { foo => 1, bar => 2, foo_bar => 3 });

done_testing;

0 comments on commit ff22015

Please sign in to comment.