[html-formfu] Increment id on block elements

Carl Franks fireartist at gmail.com
Mon Dec 7 13:56:13 GMT 2009


2009/12/7 Stephen Shorrock <stephen.shorrock at googlemail.com>:
> On Mon, Dec 7, 2009 at 11:50 AM, Carl Franks <fireartist at gmail.com> wrote:
>> 2009/12/7 Stephen Shorrock <stephen.shorrock at googlemail.com>:
>>> Dear list,
>>>
>>> FormFu has been excellent at handling and displaying my forms, I've
>>> found a single call to render the form fantastic. However I've come up
>>> against some limitations which may not allow me to render a form as
>>> simple as this.
>>>
>>> I have a repeatable within which I have a number of block elements, and fields.
>>>
>>> By creating a custom Block element is there something I could override
>>> / modify to increment the field id? within the render I wish these
>>> block element to have the row number appended to their ids cf field
>>> elements.  Indeed I'd like to access these elements via the form
>>> object in my controller, so I imagine that I would also need to have
>>> a name attribute associated with them but this will render my XHTML as
>>> invalid.
>>
>> Hi Stephen,
>>
>> Could you give an example of exactly what you'd like the output xhtml
>> to look like - including the field and block IDs ?
>>
>> Cheers,
>> Carl
>>
> Hi Carl,
>
> Sure,  I've hacked out of my code an example 9removed a few filters
> and other elements).  In the specific case that I'm working on the
> block element (rt) is an anchor which is valid if it has a name, my
> main problem is not being able to increment the ids/names.  I could
> see wanting to do this with divs in the future.
>
>
> <form action="ts" method="post">
> <table id="card">
> <tr>
> <td>
> <Div id="rt">
> access me
> </Div>
> </td>
> <td>
> <div class="clear_left label_vert float_left text">
> <input name="day_task" type="text" class="clear_left float_left"
> maxlength="45" size="30" />
> </div>
> </td>
> </tr>
> <tr>
>
> <td>
> <Div id="rt_1">
> access me too
> </Div>
> </td>
> <td>
> <div class="text">
> <input name="day_task_1" type="text" id="day_task_1" />
> </div>
> </td>
> </tr>
> <tr>
> <td colspan="2">
> <input name="num_lines" type="hidden" value="3" />
> <div class="submit">
> <input name="submit" type="submit" value="Save" />
> </div>
>
> </td>
> </tr>
> </table>
> </form>
>
>
>
>
>   ---
>    indicator: submit
>    action: ts
>    elements:
>        - type: Block
>          tag: table
>          id: card
>          elements:
>          - type: Block
>            tag: tr
>            elements:
>              - type: Block
>                tag: td
>                elements:
>                 - type: Block
>                   tag: Div
>                   id: rt
>                   content: "access me"
>              - type: Block
>                tag: td
>                elements:
>                 - type: Text
>                   name: day_task
>                   render_processed_value: 1
>                   container_attrs:
>                     class: clear_left label_vert float_left
>                   attributes:
>                     size: 30
>                     maxlength: 45
>                     class: clear_left float_left
>          - type: Repeatable
>            tag: tr
>            name: timecard_rep
>            increment_field_names: 1
>            auto_id: %n
>            counter_name: num_lines
>            elements:
>              - type: Block
>                tag: td
>                elements:
>                 - type: Block
>                   tag: Div
>                   id: rt
>                   content: "access me too"
>              - type: Block
>                tag: td
>                elements:
>                 - type: Text
>                   name: day_task
>          - type: Block
>            tag: tr
>            elements:
>              - type: Block
>                tag: td
>                attributes:
>                   colspan:2
>                elements:
>                 - type: Hidden
>                   name: num_lines
>                   value: 3
>                   retain_default: 1
>                 - type: Submit
>                   name: submit
>                   value: Save
> Stephen

Hi Stephen,

The Block sub-class pasted below should handle the incrementing Block ID.

You would just need to change the appropriate:
    - type: Block
line to something like:
    - type: MyBlock
      auto_block_id: 'rt_%r'

Once I'm back on a development machine, I'll move this into the core
Block element - so after the next cpan release, you'll be able to
change the 'MyBlock' back to 'Block', and auto_block_id() will still
work.

BTW, you can give a 'name' to any Block to allow you to grab it easily
within your perl code - it won't be output in the rendered xhtml.

package HTML::FormFu::Element::MyBlock;
use strict;
use base 'HTML::FormFu::Element::Block';
use Class::C3;

__PACKAGE__->mk_item_accessors(qw/ auto_block_id /);

sub prepare_id {
	my ( $self, $render ) = @_;

	if (  !defined $render->{attributes}{id}
	    && defined $self->auto_block_id
	    && defined $self->repeatable_count
	    )
	{
        my $id    = $self->auto_block_id;
        my $count = $self->repeatable_count;

        $id =~ s/%r/$count/g;

        $render->{attributes}{id} = $id;
	}

	return $self->next::method( $render );
}

1;

Is there anything I've missed from your original request?

Cheers,
Carl



More information about the HTML-FormFu mailing list