s2cuts has asked for the wisdom of the Perl Monks concerning the following question:

Good day all,

I'm needing to conditionally remove or disable a FormFu element (Radiogroup) from within a Catalyst action/method. My first inclination was to simply make it readonly or hidden, but I soon discovered that adding those attributes via the 'add_attributes' method would only apply the attribute to some outer span and have no effect on the radio buttons themselves.

I'm now willing to accept just simply removing the element (Radiogroup) altogether. My strategy was to use one of the 'get_*' methods to retrieve the element, then use the 'remove_element' method to remove it. However, remove_element seems to be complaining that "element not found..." in which ever way I call the method. Of course, I've confirmed that the element is indeed being found by the 'get_*' method.

Here's a little code snippet of what I've got in my action:

if ($row_object->row_attribute) { my $foo_element = $form->get_field(type => 'Radiogroup', name => 'foo_element'); $form->remove_element($foo_element); }

Maybe someone can see what I'm doing wrong, or they can suggest an alternate strategy.

Thanks all

Replies are listed 'Best First'.
Re: HTML::FormFu element removal...
by fireartist (Chaplain) on Jun 01, 2011 at 12:45 UTC
    You probably want:
    $foo_element->parent->remove_element( $foo_element );
    This is because get_field is recursive, whereas remove_element isn't.
      Yup, you are bang on. I did my best going through the docs, but...

      Many many thanks, and to all who gave input.

Re: HTML::FormFu element removal...
by Anonymous Monk on May 31, 2011 at 23:08 UTC
    Maybe someone can see what I'm doing wrong, or they can suggest an alternate strategy.

    Well, instead of using conditionals and disabling a group, have two different forms/pages; one with, one without.

      Yeah, I was hoping I wouldn't have to go that route. Ultimately if I can't figure this out I'll have to, but it seems unreasonable to not be able to delete one element from a form definition.
        Yeah, I was hoping I wouldn't have to go that route. Ultimately if I can't figure this out I'll have to, but it seems unreasonable to not be able to delete one element from a form definition.

        Why not?

        To my mind, each screen is a different form, anything else is too complicated and reserved for client side javascript (ajax) manipulations :) I guess its a matter of perspective.

        I'm not really up on HTML::FormFu::Manual::BasicConcepts, but remove_elements does appear to work

        #!/usr/bin/perl -- use strict; use warnings; use HTML::FormFu; use YAML::XS qw( Load ); Main( @ARGV ); exit( 0 ); sub Main { Fain(); print "\n", '#' x 33 , "\n"; Fain( { dependOnMe => "if i'm not here, remove fooRadiogroup" } ); } sub Fain { my $cgi = shift; my $form = HTML::FormFu->new; my $data = Load(<<'__YAML__'); --- elements: - type: Text name: foo - type: Text name: dependOnMe - type: Radiogroup name: fooRadiogroup options: - value: 1 label: 'First' attributes: myattr: 'escape&attr' label_attributes: myattr: 'escape&label' container_attributes: myattr: 'escape&container' - value: 2 label: 'Second' attributes_xml: myattr: 'noescape&amp;' __YAML__ $form->populate($data); $form->process( $cgi ); if( not $form->valid('dependOnMe') ){ my $pariah = $form->get_field( name => 'fooRadiogroup', type = +> 'Radiogroup' ) ; $form->remove_element( $pariah ); } print $form; } __END__