builder for default_over_obj

11 views
Skip to first unread message

Rippl, Steve

unread,
Dec 5, 2011, 6:22:28 PM12/5/11
to formh...@googlegroups.com
I'm wanting to use default_over_obj with a sub{} rather than a static value, and the docs say...

"At this time there is no equivalent of 'set_default', but the type of the attribute is not defined so you can provide default values in a variety of other ways, including providing a trait which does 'build_default_over_obj'. For examples, see tests in the distribution."

Unfortunately I can't find (or recognize) examples of this!  Can someone point me in the right direction please?

Many thanks,
Steve



--
Steve Rippl
Technology Director
Woodland Public Schools
360 841 2730

Gerda Shank

unread,
Dec 6, 2011, 1:18:20 PM12/6/11
to formh...@googlegroups.com
On 12/5/11 6:22 PM, Rippl, Steve wrote:
> I'm wanting to use default_over_obj with a sub{} rather than a static
> value, and the docs say...
>
> "At this time there is no equivalent of 'set_default', but the type of
> the attribute is not defined so you can provide default values in a
> variety of other ways, including providing a trait which does
> 'build_default_over_obj'. For examples, see tests in the distribution."
>
> Unfortunately I can't find (or recognize) examples of this! Can
> someone point me in the right direction please?
>
> Many thanks,
> Steve
>
>

Steve:

The test is in xt/init.t in the repository. It applies a trait to the
field with a 'build_default_over_obj' method. You could also create a
special field type. This would also work with a DBIC row and item => $row.

Maybe it would be better to allow a coderef for that... Hmm....

Gerda

========

use strict;
use warnings;
use Test::More;

{
package My::Default;
use Moose::Role;

sub build_default_over_obj {
return 'From Method';
}
}

{
package My::Other::Form;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';

has '+name' => ( default => 'testform_' );
has_field 'optname' => ( temp => 'First' );
has_field 'reqname' => ( required => 1, default_over_obj => 'From
Attribute' );
has_field 'altname' => ( traits => ['My::Default'] );
has_field 'somename';
has_field 'extraname' => ( default_over_obj => '' );

sub default_somename {
my $self = shift;
return 'SN from meth';
}
}

my $init_object = { reqname => 'Starting Perl', optname => 'Over Again',
altname => 'test',
extraname => 'not_empty',
};
my $form = My::Other::Form->new;
ok( $form, 'get form' );

$form->process( init_object => $init_object, params => {} );
is( $form->field('altname')->value, 'From Method', 'correct value' );


Rippl, Steve

unread,
Dec 6, 2011, 4:48:12 PM12/6/11
to formh...@googlegroups.com
On Tue, Dec 6, 2011 at 10:18 AM, Gerda Shank <gerda...@gmail.com> wrote:

Steve:

The test is in xt/init.t in the repository. It applies a trait to the field with a 'build_default_over_obj' method. You could also create a special field type. This would also work with a DBIC row and item => $row.

Maybe it would be better to allow a coderef for that... Hmm....

Gerda



Sorry, I was grepping through t/ not xt/!  Thanks for pointing that out, works perfectly!
Reply all
Reply to author
Forward
0 new messages