Skip to content

Commit

Permalink
Merge branch 'master' of vc.ionzero.com:FormSensible
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Kuri committed Jan 28, 2011
2 parents 2b2b5b3 + 814126e commit 57c5158
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.swp
3 changes: 3 additions & 0 deletions lib/Form/Sensible.pm
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,11 @@ the same convention for class name passing as the get_renderer method.
=head1 AUTHORS
Jay Kuri - E<lt>jayk@cpan.orgE<gt>
Luke Saunders - E<lt>luke.saunders@gmail.comE<gt>
Devin Austin - E<lt>dhoss@cpan.orgE<gt>
=head1 SPONSORED BY
Ionzero LLC. L<http://ionzero.com/>
Expand Down
22 changes: 18 additions & 4 deletions lib/Form/Sensible/Reflector.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,23 @@ use Moose;
use namespace::autoclean;
use Carp;

our $VERSION = "0.01";
eval $VERSION;

# ABSTRACT: A simple reflector class for Form::Sensible

=head2 $self->with_trigger
Add a submit button to the form. Defaults to 0.
=cut

has 'with_trigger' => (
is => 'rw',
lazy => 1,
default => 0,
);

sub reflect_from {
my ( $self, $handle, $options) = @_;

Expand Down Expand Up @@ -36,10 +51,9 @@ sub reflect_from {
$form->add_field( $field_def );
}

## convenience - add a submit button
##my $submit_button = Form::Sensible::Field::Trigger->new( name => 'submit' );
##$form->add_field($submit_button);
#warn "Form in create_form: " . Dumper $form;
my $trigger = $self->with_trigger;
$form->add_field(Form::Sensible::Field::Trigger->new( name => 'submit' ))
if $trigger == 1;
return $self->finalize_form($form, $handle);
}

Expand Down
40 changes: 40 additions & 0 deletions t/lib/MockReflector.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package MockReflector;
use Moose;
use namespace::autoclean;
extends 'Form::Sensible::Reflector';

sub get_field_names {
return qw/ field1 field2 field3 /;
}

sub get_all_field_definitions {
return (
{
field_class => 'Text',
name => 'field1',
validation => {
regex => qr/^(.+){3,}$/
},
},
{
field_class => 'FileSelector',
name => 'field2',
validation => {}, # wtf do we validate here?
},
{
field_class => 'Text',
name => 'field3',
validation => {
regex => qr/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/,
},
},
);
}

sub create_form_object {
my ( $self, $handle, $form_options ) = @_;

return Form::Sensible::Form->new(name => "test");
}

1;
75 changes: 75 additions & 0 deletions t/reflector/trigger.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use strict;
use warnings;
use Test::More;
use Form::Sensible;
use FindBin;
use lib "$FindBin::Bin/../lib";
use lib "t/lib";
use MockReflector;
use Data::Dumper;

my $reflector = MockReflector->new( with_trigger => 1 );
my $form = $reflector->reflect_from( undef, { form => { name => 'test' } } );
my $reflector_without_submit = MockReflector->new();
my $form_without_submit =
$reflector_without_submit->reflect_from( undef,
{ form => { name => 'test' } } );

my $expected_without_submit = Form::Sensible->create_form(
{
name => "test",
fields => [
{
field_class => 'Text',
name => 'field1',
validation => { regex => qr/^(.+){3,}$/ },
},
{
field_class => 'FileSelector',
name => 'field2',
validation => {}, # wtf do we validate here?
},
{
field_class => 'Text',
name => 'field3',
validation => {
regex =>
qr/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/,
},
},
],
}
);

my $expected = Form::Sensible->create_form(
{
name => "test",
fields => [
{
field_class => 'Text',
name => 'field1',
validation => { regex => qr/^(.+){3,}$/ },
},
{
field_class => 'FileSelector',
name => 'field2',
validation => {}, # wtf do we validate here?
},
{
field_class => 'Text',
name => 'field3',
validation => {
regex =>
qr/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/,
},
},
{
field_class => 'Trigger',
name => 'submit',
}
],
}
);
is_deeply( $form, $expected, "forms compare correctly" );
is_deeply( $form_without_submit, $expected_without_submit, "forms compare correctly" );
done_testing();

0 comments on commit 57c5158

Please sign in to comment.