Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

has_field works with subclasses

  • Loading branch information...
commit 0af4ddd2ab5d27dc80cbf52a102c9bf2bf660b81 1 parent f55efa1
gerda.shank@gmail.com authored
12 lib/HTML/FormHandler.pm
View
@@ -1,6 +1,6 @@
package HTML::FormHandler;
-use HTML::FormHandler::Moose;
+use Moose;
use MooseX::AttributeHelpers;
extends 'HTML::FormHandler::Model';
@@ -508,8 +508,6 @@ This stores a weakened value.
has 'parent_field' => ( is => 'rw', weak_ref => 1 );
-has 'has_field_list' => ( is => 'rw', isa => 'ArrayRef' );
-
# tell Moose to make this class immutable
HTML::FormHandler->meta->make_immutable;
@@ -721,8 +719,8 @@ field objects. It calls the make_field() method for each field.
sub build_form
{
my $self = shift;
-
- my $meta_flist = $self->meta->field_list;
+$DB::single=1;
+ my $meta_flist = $self->meta->field_list if $self->meta->can('field_list');
my $flist = $self->field_list;
$self->_build_fields( $meta_flist, 0 ) if $meta_flist;
$self->_build_fields( $flist->{'required'}, 1 ) if $flist->{'required'};
@@ -1007,7 +1005,7 @@ Pass a second true value to not die on errors.
sub field
{
my ( $self, $name, $no_die ) = @_;
-$DB::single=1;
+
$name = $self->name_prefix . '.' . $name if $self->name_prefix;
for my $field ( $self->fields )
{
@@ -1338,5 +1336,5 @@ the same terms as Perl itself.
=cut
-no HTML::FormHandler::Moose;
+no Moose;
1;
10 lib/HTML/FormHandler/Meta/Class.pm
View
@@ -1,5 +1,5 @@
package # hide from Pause
- HTML::FormHandler::Meta::Class;
+ HTML::FormHandler::Meta::Class;
use Moose;
extends 'Moose::Meta::Class';
@@ -9,11 +9,11 @@ HTML::FormHandler::Meta::Class
=head1 SYNOPSIS
-Adds a 'field_list' meta attribute for handling 'has_field'
+Add metaclass to field_list attribute
=cut
-has 'field_list' => ( is => 'rw' );
+has 'field_list' => ( is => 'rw', isa => 'ArrayRef', predicate => 'has_field_list' );
=head1 AUTHOR
@@ -21,10 +21,8 @@ Gerda Shank, gshank@cpan.org
=head1 COPYRIGHT
-This library is free software, you can redistribute it and/or modify it under
-the same terms as Perl itself.
+Same terms as Perl itself.
=cut
-no Moose;
1;
36 lib/HTML/FormHandler/Moose.pm
View
@@ -1,7 +1,7 @@
-package # hide from Pause
- HTML::FormHandler::Moose;
+package HTML::FormHandler::Moose;
use Moose;
+use Moose::Exporter;
use HTML::FormHandler::Meta::Class;
=head1 NAME
@@ -10,17 +10,15 @@ HTML::FormHandler::Moose - to add FormHandler sugar
=head1 SYNOPSIS
-Enables the use of field specification sugar:
-
- has_field 'username' => ( type => 'Text', ... );
-
+Enables the use of field specification sugar (has_field).
Use this module instead of C< use Moose; >
package MyApp::Form::Foo;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
- < define form>
+ has_field 'username' => ( type => 'Text', ... );
+ has_field 'something_else' => ( ... );
no HTML::FormHandler::Moose;
1;
@@ -33,18 +31,28 @@ Moose::Exporter->setup_import_methods(
);
sub init_meta {
- shift;
- Moose->init_meta( @_, metaclass => 'HTML::FormHandler::Meta::Class' );
+ shift;
+ Moose->init_meta( @_, metaclass => 'HTML::FormHandler::Meta::Class' );
}
sub has_field
{
my ( $class, $name, %options ) = @_;
- my $instance = Class::MOP::Class->initialize( 'HTML::FormHandler' );
- my $flist_attr = $instance->get_attribute('has_field_list');
- my $value = $flist_attr->get_value( $instance ) || [];
- push @{$value}, {$name => \%options};
- $flist_attr->set_value($instance, $value);
+$DB::single=1;
+ my $value = $class->meta->field_list || [];
+ if( scalar @{$value} == 0 )
+ {
+ # first time in this class.
+ foreach my $sc ( $class->meta->superclasses )
+ {
+ last if $sc eq 'HTML::FormHandler';
+ if ( $sc->meta->can('field_list') )
+ {
+ push @{$value}, @{$sc->meta->field_list};
+ }
+ }
+ }
+ push @{$value}, ($name => \%options);
$class->meta->field_list($value);
}
25 t/has_field.t
View
@@ -0,0 +1,25 @@
+use Test::More tests => 10;
+
+use lib 't/lib';
+
+use_ok( 'HTML::FormHandler' );
+
+use_ok( 'Form::Two' );
+
+my $form = Form::Two->new;
+
+ok( $form, 'get subclassed form' );
+
+is( $form->field('optname')->temp, 'Txxt', 'new field');
+
+ok( $form->field('reqname'), 'get old field' );
+
+ok( $form->field('fruit'), 'fruit field' );
+
+use_ok( 'Form::Test' );
+
+$form = Form::Test->new;
+
+ok( $form, 'get base form' );
+ok( !$form->field_exists('new_field'), 'no new field');
+ok( $form->field_exists('optname'), 'base field exists');
3  t/lib/Form/Test.pm
View
@@ -3,11 +3,12 @@ package Form::Test;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
+has '+name' => ( default => 'TestForm');
+
has_field 'reqname' => ( type => 'Text', required => 1 );
has_field 'optname' => ( type => 'Text' );
has_field 'fruit' => ( type => 'Select' );
-has 'name' => ( isa => 'Str', is => 'rw', default => 'TestForm');
sub options_fruit {
return (
Please sign in to comment.
Something went wrong with that request. Please try again.