[Maypole] CDBI::AsForm and checkboxes
Joshua Keroes
jkeroes@eli.net
Fri, 15 Apr 2005 10:23:22 -0700
Thank-you both, Peter and Ian. This is exactly what I'm after.
Cheers,
Joshua
On Apr 14, 2005, at 7:54 PM, Ian VanDerPoel wrote:
> See below.
>
>>>> Peter Speltz <peterspeltz@yahoo.com> 15/04/05 12:23:08 PM >>>
>
> --- Joshua Keroes <jkeroes@eli.net> wrote:
>>
>> What's the smart way to go about rendering boolean values (i.e.
>> TINYINT(1)) in the database as checkboxes? More specifically, is there
>> an update to CDBI::AsForm that includes checkbox support? I thought
>> we'd
>> gone over this before on the list, but google's not finding any
>> matches.
>>
>
>> Ian on this and the cdbi list has checkbox and radio button support
>> in his
>> AsForm. I used to have a copy but can't find it at the moment.
>> Basically just
>> make a "_to_bool_checkbox" sub and put a clause like this in
>> "to_field" with
>> all the other clauses like this:
>
>> return $self->_to_bool_checkbox($col) if $type && $type =~
>> /tinyint(1)/i;
>
>> HTH
>
>> pjs
>
> Peter/Joshua,
>
> We use a suffix on the column name to determine what type of element
> to build. For Checkbox we end the col name with "_FLAG" and for radio
> buttons "_YN". Below is a copy of the code for creating checkbox and
> radio button elements.
>
> # checkboxes: if no data in hand (ie called as class method), replace
> with a radio button,
> # in order to allow this field to be left unspecified in search / add
> forms.
> sub _to_checkbox {
> my ($self, $col) = @_;
> my $nullable = $self->columnnul->{$col};
> return $self->_to_radioyn($col) if !ref($self) || $nullable;
> my $value = $self->$col;
> my $a = HTML::Element->new("input", type=> "checkbox", name =>
> $col);
> $a->attr("checked" => 'true') if $value eq 'Y';
> $OLD_STYLE && return $a->as_HTML;
> $a;
> }
>
> sub _to_radioyn {
> my ($self, $col) = @_;
> my $value = ref $self && $self->$col || '';
> my $nullable = $self->columnnul->{$col};
> my $a = HTML::Element->new("span");
> my $ry = HTML::Element->new("input", type=> "radio", name=>$col,
> value=>'Y' );
> my $rn = HTML::Element->new("input", type=> "radio", name=>$col,
> value=>'N' );
> my $ru = HTML::Element->new("input", type=> "radio", name=>$col,
> value=>'' ) if $nullable;
> $ry->push_content('Yes'); $rn->push_content('No');
> $ru->push_content('n/a') if $nullable;
> if ($value eq 'Y') { $ry->attr("checked" => 'true') }
> elsif ($value eq 'N') { $rn->attr("checked" => 'true') }
> elsif ($nullable) { $ru->attr("checked" => 'true') }
> $a->push_content($ry, $rn);
> $a->push_content($ru) if $nullable;
> $OLD_STYLE && return $a->as_HTML;
> $a;
> }
> ####
>
> Please feel free to request explanations.
>
> Regards
>
> IAN
>
>
>
> _______________________________________________________________________
> _________
>
> This message (including any attachments) is intended solely for the
> addressee
> named and may contain confidential information. If you are not the
> intended
> recipient, please delete it and notify the sender. Views expressed in
> this
> message are those of the individual sender, and are not necessarily
> the views
> of the Ministry of Transport. The whole or parts of this e-mail may be
> subject to copyright of the Ministry or third parties. You should only
> re-transmit, distribute or use the material for commercial purposes if
> you are
> authorised to do so.
>
> Visit us at:
>
> www.transport.nsw.gov.au or www.131500.com.au
>
> _______________________________________________________________________
> _________
>
>