[html-formfu] Re: Adding a link to the output of a nested repeatable

Mike South msouth at gmail.com
Wed Jun 3 03:25:55 GMT 2009


On Tue, Jun 2, 2009 at 5:07 PM, Mike South <msouth at gmail.com> wrote:
> On Tue, Jun 2, 2009 at 10:54 AM, Mike South <msouth at gmail.com> wrote:
>> Hi,
>>
>> I have a form with a repeatable element (using
>> HTML::FormFu::Model::DBIC) representing an interviewee that lets me
>> edit the interviewee's firstname and lastname columns.  I also want a
>> link to appear next to each one that lets you edit the rest of the
>> details for an interviewee (like href="/interviewee/edit/[id]" ).
>> What is the best way to generate that link?
>>
>> I was guessing plugin.  My thought was that I could attach the plugin
>> to the Hidden field that gets the id.  Within the plugin, I would look
>> at the id field, create the link, and add the link to the end of the
>> HTML that generates the hidden form field.  (Kludgy, but I just wanted
>> to get something working).  I don't even know if I can do that from a
>> plugin, but here's what I tried so far:
>>
>> Using the code from StashValid, I tried to put a warning with the
>> value of the field.  However, 'valid' never returned true and the
>> param was always undefined (whether I asked for valid() or not).
>>
>> Here is the plugin:
>>
>> package HTML::FormFu::Plugin::Mike;
>> use strict;
>> use base 'HTML::FormFu::Plugin';
>>
>> sub post_process {
>>
>>    my ($self) = @_;
>>
>>    my $form = $self->form;
>>    my $name = $self->parent->nested_name;
>>    warn "name is [$name]";
>>
>>    if ( $form->valid($name) ) {
>>        warn "val is ".  $form->param($name);
>>    }
>>    else {
>>        warn "not valid!";
>>    }
>> }
>> 1;
>>
>> Here is how I attached it to the field
>>               elements:
>>                  - type: Hidden
>>                    name: interviewee_id
>>                    plugins:
>>                      - Mike
>>
>> The field is getting written out correctly with a value, like this:
>>
>> <input name="interviewees_1.interviewee_id" type="hidden" value="4" />
>>
>> The 'name' gets warned out correctly:
>>
>> name is [interviewees_1.interviewee_id]
>>
>> but it's followed by
>>
>> not valid!
>>
>> (or, if I take out the condition, an uninitialized value warning).
>>
>> I don't really know what to try next.
>>
>> Thanks in advance for any help anyone can give.
>>
>> mike
>>
>
> I realize now (now that the implications of Carl's recent posted
> plugin example have had a chance to cook in the ol' subconscious) that
> a $self->parent->value call gives me the value of the data.  I think I
> saw something that adds an element after a given element, so I'll see
> if I can make a src element in my plugin using that data and add it in
> there.  (Up to now my use of FormFu has been about 95%
> configuration-only, which I think is a great testament to how much
> "just works").
>
> Anyway, just wanted to let everyone know I think I've found my way on this.
>
> mike
>

Here's what I came up with:


package HTML::FormFu::Plugin::IntervieweeEditLink;
use strict;
use base 'HTML::FormFu::Plugin';

sub post_process {

    my ($self) = @_;

    my $form = $self->form;
    my $attached_field = $self->parent;
    # don't want to add a link to the extra 'add interviewee' row
    return unless $attached_field->value;


    my $position =
$attached_field->parent->get_element({type=>'Hidden',
name=>$attached_field->name});

    my $formfu = $attached_field;
    while ( not $formfu->isa('HTML::FormFu') ) {
        $formfu->can('parent') or die "no luck on finding the formfu object";
        $formfu = $formfu->parent;
    }
    my $uri = $formfu->stash->{context}->uri_for('/interviewee/edit/'.$attached_field->value);
    my $link =  qq{<a href="$uri">Edit Details</a>};
    warn "trying to add $link";

    $attached_field->parent->insert_after(
        HTML::FormFu->new->element( {
            type        => 'Src',
            content_xml => $link,
        }),
        $position,
    );

}
1;

Like this in the yml:

             - type: Block
                tag: td
                elements:
                  - type: Hidden
                    name: interviewee_id
                    plugins:
                      - IntervieweeEditLink


probably makes you want to gouge out your own eyes, but I'm working on
a deadline and it did the job.

mike



More information about the HTML-FormFu mailing list