Cannot delegate clear_errors to clear_errors because the value of result is not defined

32 views
Skip to first unread message

Rippl, Steve

unread,
Dec 22, 2011, 6:17:42 PM12/22/11
to formh...@googlegroups.com
Seasons greetings!  I'm getting the following when I submit a form which has "compounded" checkbox fields checked that are generated by parameterized traits (if I leave then unchecked everything else works great) 

Caught exception in Site::Controller::Node->add "Cannot delegate clear_errors to clear_errors because the value of result is not defined at /usr/local/lib/perl/5.10.1/Moose/Meta/Method/Delegation.pm line 99
	HTML::FormHandler::Field::clear_errors('HTML::FormHandler::Field::Checkbox::8=HASH(0xbb9d2c8)') called at /usr/local/share/perl/5.10.1/HTML/FormHandler/Validate.pm line 54
	HTML::FormHandler::Validate::validate_field('HTML::FormHandler::Field::Checkbox::8=HASH(0xbb9d2c8)') called at /usr/local/share/perl/5.10.1/HTML/FormHandler/Fields.pm line 100
	HTML::FormHandler::Fields::_fields_validate('HTML::FormHandler::Field::Compound::4=HASH(0xbba08a0)') called at /usr/local/share/perl/5.10.1/HTML/FormHandler/Validate.pm line 73
	HTML::FormHandler::Validate::validate_field('HTML::FormHandler::Field::Compound::4=HASH(0xbba08a0)') called at /usr/local/share/perl/5.10.1/HTML/FormHandler/Fields.pm line 100
	HTML::FormHandler::Fields::_fields_validate('HTML::FormHandler::Field::Compound::4=HASH(0xbb9d298)') called at /usr/local/share/perl/5.10.1/HTML/FormHandler/Validate.pm line 73
	HTML::FormHandler::Validate::validate_field('HTML::FormHandler::Field::Compound::4=HASH(0xbb9d298)') called at /usr/local/share/perl/5.10.1/HTML/FormHandler/Fields.pm line 100
	HTML::FormHandler::Fields::_fields_validate('Site::Form::Node::Edit::10=HASH(0xbac8210)') called at /usr/local/share/perl/5.10.1/HTML/FormHandler.pm line 312

etc.

The parameterized role is...

package Site::Form::Node::Roles::RolePermissions;
use MooseX::Role::Parameterized;
use HTML::FormHandler::Moose::Role;

parameter id => (
    isa      => 'Int',
    required => 1,
);

parameter name => (
    isa      => 'Str',
    required => 1,
);

role {
    my $self = shift;

    has_field 'access.view.'.$self->id => (
        type  => 'Checkbox',
        label => $self->name,
    );

    has_field 'access.edit.'.$self->id => (
        type  => 'Checkbox',
        label => $self->name,
    );
};

1;

The form is called in the controller via...

    my $user_roles_perm_fields = $c->forward('_permission_roles');

    $c->stash->{form} = Site::Form::Node::Edit
        ->with_traits(@{ $user_roles_perm_fields })
        ->new(
            user_name    => $c->user->username,
            db_schema    => $c->model('DB')->schema,
            node_type_id => $content_type,
        );

    my $node = $c->model('DB::Node')->new_result( {} );
    
    if ( $c->stash->{form}->process(
                item   => $node,
                params => $c->req->params,
            )   
        )   
     {   
        $c->log->info('Node '.$node->id.' created by user '.$c->user->use    rname); 
        $c->res->redirect( $c->uri_for( '/node/'.$node->id,
            {mid => $c->set_status_msg('Node added')}) );
        $c->detach; 
    }   
    else {
        return;
    }   


where

sub _permission_roles : Private {
    my ( $self, $c ) = @_;

    my @user_roles_perm_fields;
    foreach my $role ( @{ $c->model('DB::Role')->role_array } ) {
        push( @user_roles_perm_fields,
              'Site::Form::Node::Roles::RolePermissions' => {
                  id   => $role->{id},
                  name => $role->{name},
             }
        );
    }

   return \@user_roles_perm_fields;
}

and in the form itself...


package Site::Form::Node::Edit;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Model::DBIC';

has 'user_name' => ( is => 'ro', required => 1 );
has 'db_schema' => ( is => 'rw', required => 1 );
has 'node_type_id' => ( is => 'ro', isa => 'Int' );
has '+item_class' => ( default => 'Node' );
has '+auto_fieldset' => ( default => 0 );

has_field 'title' => (
    type     => 'Text',
    required => 1,
);

has_field 'body' => (
    type      => 'TextArea',
    required  => 1,
    css_class => 'tinymce',
);

has_field 'access' => (
    type => 'Compound',
);

has_field 'access.view' => (
    type => 'Compound',
);

has_field 'access.edit' => (
    type => 'Compound',
);

has_field 'user_id' => (
    type     => 'Text',
    label    => 'Owner',
    required => 1,
    traits   => [ 'Site::Form::Node::Roles::NodeOwner' ],
);

sub validate_user_id {
    my ( $self, $field ) = @_;
    $field->add_error('Not a valid username')
        if !$self->db_schema->resultset('User')->search({
            username => $field->value
        })->first;
}

has_field 'type_id' => (
    type     => 'Hidden',
    required => 1,
    traits   => [ 'Site::Form::Node::Roles::TypeDefault' ],
);

has_field 'submit' => (
    type => 'Submit',
    value => 'Submit',
);

around 'update_model' => sub {
    my $orig   = shift;
    my $self   = shift;
    my $item   = $self->item;
    my $schema = $self->db_schema;

# convert username to uid
    my $uid = $schema->resultset('User')->search({
        username => $self->field('user_id')->value
    })->first->id;

    $self->values->{user_id} = $uid;

    $self->schema->txn_do( sub {
# run the dbic model updates for node table
        $self->$orig(@_);

# get the access permission fields and update node_access accordingly
        foreach my $role ( @{ $schema->resultset('Role')->role_array } ) {
            my $view = $self->field("access.view.".$role->{id})->value || 0;
            my $edit  = $self->field("access.edit.".$role->{id})->value  || 0;

            $schema->resultset('NodeAccess')->update_or_create({
                node_id => $item->id,
                role_id => $role->{id},
                view    => $view,
                edit    => $edit,
            });
        }
    });
};



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

Steve Rippl

unread,
Jan 3, 2012, 2:18:44 PM1/3/12
to formhandler
Can anyone point me in a direction on this one?

On Dec 22 2011, 3:17 pm, "Rippl, Steve" <rip...@woodlandschools.org>
wrote:
> Seasons greetings!  I'm getting the following when I submit a form
> which has "compounded" checkbox fields checked that are generated by
> parameterized traits (if I leave then unchecked everything else works
> great)
>
> Caught exception in Site::Controller::Node->add "Cannot delegate
> clear_errors to clear_errors because the value of result is not
> defined at /usr/local/lib/perl/5.10.1/Moose/Meta/Method/Delegation.pm
> line 99
>         HTML::FormHandler::Field::clear_errors('HTML::FormHandler::Field::Checkbox: :8=HASH(0xbb9d2c8)')
> called at /usr/local/share/perl/5.10.1/HTML/FormHandler/Validate.pm
> line 54
>         HTML::FormHandler::Validate::validate_field('HTML::FormHandler::Field::Chec kbox::8=HASH(0xbb9d2c8)')
> called at /usr/local/share/perl/5.10.1/HTML/FormHandler/Fields.pm line
> 100
>         HTML::FormHandler::Fields::_fields_validate('HTML::FormHandler::Field::Comp ound::4=HASH(0xbba08a0)')
> called at /usr/local/share/perl/5.10.1/HTML/FormHandler/Validate.pm
> line 73
>         HTML::FormHandler::Validate::validate_field('HTML::FormHandler::Field::Comp ound::4=HASH(0xbba08a0)')
> called at /usr/local/share/perl/5.10.1/HTML/FormHandler/Fields.pm line
> 100
>         HTML::FormHandler::Fields::_fields_validate('HTML::FormHandler::Field::Comp ound::4=HASH(0xbb9d298)')
> called at /usr/local/share/perl/5.10.1/HTML/FormHandler/Validate.pm
> line 73
>         HTML::FormHandler::Validate::validate_field('HTML::FormHandler::Field::Comp ound::4=HASH(0xbb9d298)')
> called at /usr/local/share/perl/5.10.1/HTML/FormHandler/Fields.pm line
> 100
>         HTML::FormHandler::Fields::_fields_validate('Site::Form::Node::Edit::10=HAS H(0xbac8210)')
> called at /usr/local/share/perl/5.10.1/HTML/FormHandler.pm line 312
>
> etc.
>
> *The parameterized role is...*
>
> package Site::Form::Node::Roles::RolePermissions;
> use MooseX::Role::Parameterized;
> use HTML::FormHandler::Moose::Role;
>
> parameter id => (
>     isa      => 'Int',
>     required => 1,
> );
>
> parameter name => (
>     isa      => 'Str',
>     required => 1,
> );
>
> role {
>     my $self = shift;
>
>     has_field 'access.view.'.$self->id => (
>         type  => 'Checkbox',
>         label => $self->name,
>     );
>
>     has_field 'access.edit.'.$self->id => (
>         type  => 'Checkbox',
>         label => $self->name,
>     );
>
> };
>
> 1;
>
> *The form is called in the controller via...*
>
>     my $user_roles_perm_fields = $c->forward('_permission_roles');
>
>     $c->stash->{form} = Site::Form::Node::Edit
>         ->with_traits(@{ $user_roles_perm_fields })
>         ->new(
>             user_name    => $c->user->username,
>             db_schema    => $c->model('DB')->schema,
>             node_type_id => $content_type,
>         );
>
>     my $node = $c->model('DB::Node')->new_result( {} );
>
>     if ( $c->stash->{form}->process(
>                 item   => $node,
>                 params => $c->req->params,
>             )
>         )
>      {
>         $c->log->info('Node '.$node->id.' created by user '.$c->user->use
>  rname);
>         $c->res->redirect( $c->uri_for( '/node/'.$node->id,
>             {mid => $c->set_status_msg('Node added')}) );
>         $c->detach;
>     }
>     else {
>         return;
>     }
>
> *where*
>
> sub _permission_roles : Private {
>     my ( $self, $c ) = @_;
>
>     my @user_roles_perm_fields;
>     foreach my $role ( @{ $c->model('DB::Role')->role_array } ) {
>         push( @user_roles_perm_fields,
>               'Site::Form::Node::Roles::RolePermissions' => {
>                   id   => $role->{id},
>                   name => $role->{name},
>              }
>         );
>     }
>
>    return \@user_roles_perm_fields;
>
> }
>
> *and in the form itself...*

Gerda Shank

unread,
Jan 4, 2012, 11:26:07 AM1/4/12
to formh...@googlegroups.com
Steve:

Result objects are built from three different sources: the object (item
or init_object), input (params), or fields. You're doing a hybrid in
which most of the form's fields come from the object, but you add a few
extra (the parameterized 'permission fields'). Since you're updating
those separately, I assume that they're not found in your item. The code
that creates the tree of results is supposed to create results from the
fields, if there are no accessors for the field names in your 'item'. So
something is probably breaking down there... *Are* there accessors in
your DBIC result source that match those field names? If there are, you
could change the field names so they don't match, if that's confusing
things.

You don't have to provide an item; you can provide a schema and
'item_class' instead. That's one thing to try. (All forms that inherit
from the DBIC model have a 'schema' attribute so you don't need to add
another one with 'db_schema' ).

If you can provide a testcase that I can use to step through the code
I'll take a look at it.

Gerda

Reply all
Reply to author
Forward
0 new messages