[html-formfu] Increment id on block elements

Stephen Shorrock stephen.shorrock at googlemail.com
Mon Dec 7 20:06:24 GMT 2009


On Mon, Dec 7, 2009 at 7:24 PM, Carl Franks <fireartist at gmail.com> wrote:
> 2009/12/7 Stephen Shorrock <stephen.shorrock at googlemail.com>:
>>
>> I created a new Block element class with the prepare_id method.  I
>> thought it would be tidier to access my data via a filter rather than
>> an action but unfortunately I seem unable to access it in either
>> plugins or filters:
>>
>> in my plugin/filter
>>
>> $var = "rt_1";
>> $self->parent()->form->get_all_element($var)->content('value was: '.$value);
>>
>> fails with 'Can't call method "content" on an undefined value ' .
>> Viewing the source the elements exist.  Am I trying to access the
>> values before prepare_id has been run?  hence is this not possible?
>
> If this is from the same form config as earlier, none of the Blocks
> have a *name* "rt_1".
>
> If you gave the Block within the Repeatable a name, though it wouldn't
> be incremented by auto_block_id(), you could still grab it using
> something like:
>    $form->get_all_elements('rt')->[0]
> using an index to get the right element from the returned arrayref.
>
> prepare_id() is called during render_data(), which is called by render().
> It doesn't affect the data structure of $form - it builds a
> non-blessed data structure, returned by render_data().
> I suppose you could do something like:
>
>    use List::Util qw( first );
>    my $repeatable = $form->get_all_element({ type => repeatable });
>    my $repeatable_children = $repeatable->render_data->{elements};
>    my $elem = first { $_->{name} eq 'rt_1' } @$repeatable_children;
>
> Cheers,
> Carl
>

That's sorted the problem.

I began investigating how the _Inputs become accessible through their
incremented names, and thought you tried a similar implementation for
the Blocks. I misunderstood you comment earlier about accessing blocks
via their names later.

Thank you v.much

Stephen



More information about the HTML-FormFu mailing list