[Catalyst] Changing Formhandler field-type/widget based on Catalyst user-role

will trillich will.trillich at serensoft.com
Thu May 24 04:24:43 GMT 2012

Short version: How can we toggle field "X" from being a Select (popup) for
Admin users, to being a Hidden (nothing visible, but the value is
maintained) for Manager users?

Long version:

We have three levels of user-roles (*admins* can do everything,
*managers*can do a lot,
*readonly* can do very little). Admins get to specify user roles for all
users (even other admins), but managers (level two) only get to edit
readonly users and we don't want them to elevate any such users to being
managers (or worse, admins).

    package MyApp::Controller::Admin::Person;
    $self->form( MyApp::Form::Admin::Person->new( ctx =3D> $c ) );

    package MyApp::Form::Admin::Person;
    sub set_active {
        my $self =3D shift;
            unless $self->ctx->user->is_admin;

Trying to use ctx so we could pass $c and later test it in the form's
"set_active" method throws an exception because of ctx being undefined.

    package MyApp::Controller::Admin::Person;
    unless ( $c->user->is_admin ) {
#       $self->form->inactive(['role']);
#       $self->form->field('role')->widget('hidden');
#       $self->form->field('role')->type('Hidden');

So instead we've tried making the field inactive (no apparent effect at
all), switching to a different widget (field completely disappears) or
different type (field completely disappears).


-- =

=93Waiting for perfect is never as smart as making progress.=94  -- Seth Go=
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20120523/8ec65=

More information about the Catalyst mailing list