allow adding formbuilder fields at specific positions
This commit is contained in:
parent
a3c6a2f288
commit
3a2b28eb76
3 changed files with 57 additions and 3 deletions
|
|
@ -73,6 +73,33 @@ sub addField {
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
=head2 addFieldAt ( field, position )
|
||||||
|
|
||||||
|
Add a field at a given position.
|
||||||
|
|
||||||
|
Note: Unlike L<addField>, this method requires a full field object. It will not
|
||||||
|
create one for you.
|
||||||
|
|
||||||
|
=cut
|
||||||
|
|
||||||
|
sub addFieldAt {
|
||||||
|
my ( $self, $field, $position ) = @_;
|
||||||
|
$self->addObjectAt( $field, $position );
|
||||||
|
# Rebuild the fields array to fix the ordering
|
||||||
|
# We can't be sure the position in $self->objects is the same as in $self->fields
|
||||||
|
$self->fields([]);
|
||||||
|
for my $obj ( @{$self->objects} ) {
|
||||||
|
next unless blessed $obj;
|
||||||
|
# A field isn't allowed to have child objects
|
||||||
|
if ( !$obj->can('does') || !$obj->does('WebGUI::FormBuilder::Role::HasObjects') ) {
|
||||||
|
push @{$self->fields}, $obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $field;
|
||||||
|
}
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------------
|
||||||
|
|
||||||
=head2 deleteField ( name )
|
=head2 deleteField ( name )
|
||||||
|
|
||||||
Delete a field by name. Returns the field deleted.
|
Delete a field by name. Returns the field deleted.
|
||||||
|
|
|
||||||
|
|
@ -59,8 +59,26 @@ sub addObject {
|
||||||
return $object;
|
return $object;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Handle re-ordering of objects
|
=head2 addObjectAt ( $object, $position )
|
||||||
|
|
||||||
|
Adds $object to the list of objects at a certain position, pushing all other
|
||||||
|
objects down.
|
||||||
|
|
||||||
|
=head3 $object
|
||||||
|
|
||||||
|
Some object
|
||||||
|
|
||||||
|
=head3 $position
|
||||||
|
|
||||||
|
The numeric index. 0 is the first object.
|
||||||
|
|
||||||
|
=cut
|
||||||
|
|
||||||
|
sub addObjectAt {
|
||||||
|
my ( $self, $object, $position ) = @_;
|
||||||
|
splice @{$self->objects}, $position, 1, $object;
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,6 @@ my $session = WebGUI::Test->session;
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
# Tests
|
# Tests
|
||||||
|
|
||||||
plan tests => 77; # Increment this number for each test you create
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
# Constructor and properties
|
# Constructor and properties
|
||||||
use_ok( 'WebGUI::FormBuilder' );
|
use_ok( 'WebGUI::FormBuilder' );
|
||||||
|
|
@ -243,6 +241,16 @@ cmp_deeply(
|
||||||
'field is deleted from fields',
|
'field is deleted from fields',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
# addFieldAt
|
||||||
|
$fb = WebGUI::FormBuilder->new( $session );
|
||||||
|
$field = $fb->addField( 'Text', name => "zero" );
|
||||||
|
|
||||||
|
$tab = $fb->addTab( tabset => 'one', name => 'one' );
|
||||||
|
$fset = $fb->addFieldset( name => 'three', label => 'Three' );
|
||||||
|
$field2 = $fb->addFieldAt( WebGUI::Form::Text->new( $session, name => "two" ), 2 );
|
||||||
|
|
||||||
|
is( $fb->objects->[2], $field2, 'objects array is correct' );
|
||||||
|
is( $fb->fields->[1], $field2, 'fields array is correct' );
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
# Serialize and deserialize
|
# Serialize and deserialize
|
||||||
|
|
@ -259,4 +267,5 @@ $fb->addField( 'submit', name => 'submit', label => 'Submit' );
|
||||||
# toHtml
|
# toHtml
|
||||||
|
|
||||||
|
|
||||||
|
done_testing;
|
||||||
#vim:ft=perl
|
#vim:ft=perl
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue