FAQ
Hi,

As a first post I would like to say what a great framework FormFu is.

I have a feeling my first question is not the smartest to ask but here goes.

My form has a series of checkboxs that correspond to true/false fields.

I am doing this to

if ($c->model->{val} == 1) {
$box->attributes->{checked} = 'true';
}


This gives me a source like this


<label>Has type</label>
<input name="hastype" type="checkbox" checked="true" />


My query is whether this is correct HTML (4)?

I had a look at the WC3 site and I think checked is an attribute but I
thought I should be getting a source like
<input name="hastype" type="checkbox" checked />

Am I using the right methods?
Thanx,
Dp.

Search Discussions

  • Moritz Onken at Aug 27, 2008 at 1:25 pm

    Am 27.08.2008 um 14:19 schrieb Dermot:

    Hi,

    As a first post I would like to say what a great framework FormFu is.

    I have a feeling my first question is not the smartest to ask but
    here goes.

    My form has a series of checkboxs that correspond to true/false
    fields.

    I am doing this to

    if ($c->model->{val} == 1) {
    $box->attributes->{checked} = 'true';
    }


    This gives me a source like this


    <label>Has type</label>
    <input name="hastype" type="checkbox" checked="true" />


    My query is whether this is correct HTML (4)?

    I had a look at the WC3 site and I think checked is an attribute but I
    thought I should be getting a source like
    <input name="hastype" type="checkbox" checked />

    Am I using the right methods?
    Thanx,
    Dp.
    Hi,

    this checked="checked" is valid html (4) and especially xhtml. Because
    xhtml does not allow stand-alone attributes like checked.

    You can always edit the xhtml templates which generate this checkbox.


    moritz
  • Carl Franks at Aug 27, 2008 at 2:49 pm

    2008/8/27 Dermot <paikkos@googlemail.com>:
    Hi,

    As a first post I would like to say what a great framework FormFu is.

    I have a feeling my first question is not the smartest to ask but here goes.

    My form has a series of checkboxs that correspond to true/false fields.

    I am doing this to

    if ($c->model->{val} == 1) {
    $box->attributes->{checked} = 'true';
    }


    This gives me a source like this


    <label>Has type</label>
    <input name="hastype" type="checkbox" checked="true" />


    My query is whether this is correct HTML (4)?

    I had a look at the WC3 site and I think checked is an attribute but I
    thought I should be getting a source like
    <input name="hastype" type="checkbox" checked />

    Am I using the right methods?
    Hi Dermot,

    Don't worry - this isn't a silly question at all!

    As Moritz said, checked="checked" is equivalent to the old `checked`,
    and should work just fine in HTML4 - it's just that our output is
    geared towards being valid xhtml.

    I think there's a more "correct" way of doing this, though.
    You should always be setting the Checkbox's value(), so that the
    browser knows what to send back when it's checked.
    In this case, you could just do this

    $box->default( $c->model->{val} );

    And the Checkbox is smart enough to set checked="checked" when
    default() is the same as value().
    And if they're different, the default() will just be ignored.

    Carl
  • Dermot at Aug 27, 2008 at 3:11 pm

    2008/8/27 Carl Franks <fireartist@gmail.com>:
    2008/8/27 Dermot <paikkos@googlemail.com>:
    Hi,

    As a first post I would like to say what a great framework FormFu is.

    I have a feeling my first question is not the smartest to ask but here goes.

    My form has a series of checkboxs that correspond to true/false fields.

    I am doing this to

    if ($c->model->{val} == 1) {
    $box->attributes->{checked} = 'true';
    }


    This gives me a source like this


    <label>Has type</label>
    <input name="hastype" type="checkbox" checked="true" />


    My query is whether this is correct HTML (4)?

    I had a look at the WC3 site and I think checked is an attribute but I
    thought I should be getting a source like
    <input name="hastype" type="checkbox" checked />

    Am I using the right methods?
    Hi Dermot,

    Don't worry - this isn't a silly question at all!

    As Moritz said, checked="checked" is equivalent to the old `checked`,
    and should work just fine in HTML4 - it's just that our output is
    geared towards being valid xhtml.

    I think there's a more "correct" way of doing this, though.
    You should always be setting the Checkbox's value(), so that the
    browser knows what to send back when it's checked.
    In this case, you could just do this

    $box->default( $c->model->{val} );

    And the Checkbox is smart enough to set checked="checked" when
    default() is the same as value().
    And if they're different, the default() will just be ignored.

    Thank you both you your quick response and I can see what you mean by
    being xhtml compliant. I have tried to use Carl's suggested default
    method but I can't be using it correctly. I imagine I should see a
    source like

    <input type="checkbox" name="hastype" checked="true"/> or <input
    type="checkbox" name="hastype" checked="false"/>

    Here's my complete subroutine, perhaps that might explain why that
    method doesn't appear to work.

    use base qw(Catalyst::Controller::HTML::FormFu);
    ...

    sub updateCheckboxes : Private {
    my ($c,$data) = @_; # Data contains the model.
    my $form = $c->stash->{form};
    my $checkboxes = $form->get_all_elements({type => 'Checkbox'});
    foreach my $box (@{$checkboxes}) {
    my $name = $box->name;
    my $val = $data->sub_id->$name;
    $c->log->debug("Box=$box->{name} $data->sub_id->$box->name
    VAL=$val");
    # if ($val && $val == 1) {
    $box->default($val);
    # }
    }
    }


    Thanx,
    Dp.
  • Carl Franks at Aug 27, 2008 at 3:34 pm

    2008/8/27 Dermot <paikkos@googlemail.com>:
    2008/8/27 Carl Franks <fireartist@gmail.com>:
    2008/8/27 Dermot <paikkos@googlemail.com>:
    Hi,

    As a first post I would like to say what a great framework FormFu is.

    I have a feeling my first question is not the smartest to ask but here goes.

    My form has a series of checkboxs that correspond to true/false fields.

    I am doing this to

    if ($c->model->{val} == 1) {
    $box->attributes->{checked} = 'true';
    }


    This gives me a source like this


    <label>Has type</label>
    <input name="hastype" type="checkbox" checked="true" />


    My query is whether this is correct HTML (4)?

    I had a look at the WC3 site and I think checked is an attribute but I
    thought I should be getting a source like
    <input name="hastype" type="checkbox" checked />

    Am I using the right methods?
    Hi Dermot,

    Don't worry - this isn't a silly question at all!

    As Moritz said, checked="checked" is equivalent to the old `checked`,
    and should work just fine in HTML4 - it's just that our output is
    geared towards being valid xhtml.

    I think there's a more "correct" way of doing this, though.
    You should always be setting the Checkbox's value(), so that the
    browser knows what to send back when it's checked.
    In this case, you could just do this

    $box->default( $c->model->{val} );

    And the Checkbox is smart enough to set checked="checked" when
    default() is the same as value().
    And if they're different, the default() will just be ignored.

    Thank you both you your quick response and I can see what you mean by
    being xhtml compliant. I have tried to use Carl's suggested default
    method but I can't be using it correctly. I imagine I should see a
    source like

    <input type="checkbox" name="hastype" checked="true"/> or <input
    type="checkbox" name="hastype" checked="false"/>

    Here's my complete subroutine, perhaps that might explain why that
    method doesn't appear to work.

    use base qw(Catalyst::Controller::HTML::FormFu);
    ...

    sub updateCheckboxes : Private {
    my ($c,$data) = @_; # Data contains the model.
    my $form = $c->stash->{form};
    my $checkboxes = $form->get_all_elements({type => 'Checkbox'});
    foreach my $box (@{$checkboxes}) {
    my $name = $box->name;
    my $val = $data->sub_id->$name;
    $c->log->debug("Box=$box->{name} $data->sub_id->$box->name
    VAL=$val");
    # if ($val && $val == 1) {
    $box->default($val);
    # }
    }
    }
    No, if value() eq default(), you'll get:
    <input type="checkbox" name="hastype" checked="checked"/>
    otherwise, you'll get:
    <input type="checkbox" name="hastype"/>

    Is that what was happening?

    Carl
  • Dermot at Aug 27, 2008 at 4:10 pm

    2008/8/27 Carl Franks <fireartist@gmail.com>:
    2008/8/27 Dermot <paikkos@googlemail.com>:
    2008/8/27 Carl Franks <fireartist@gmail.com>:
    2008/8/27 Dermot <paikkos@googlemail.com>:
    Hi,
    use base qw(Catalyst::Controller::HTML::FormFu);
    ...

    sub updateCheckboxes : Private {
    my ($c,$data) = @_; # Data contains the model.
    my $form = $c->stash->{form};
    my $checkboxes = $form->get_all_elements({type => 'Checkbox'});
    foreach my $box (@{$checkboxes}) {
    my $name = $box->name;
    my $val = $data->sub_id->$name;
    $c->log->debug("Box=$box->{name} $data->sub_id->$box->name
    VAL=$val");
    # if ($val && $val == 1) {
    $box->default($val);
    # }
    }
    }
    No, if value() eq default(), you'll get:
    <input type="checkbox" name="hastype" checked="checked"/>
    otherwise, you'll get:
    <input type="checkbox" name="hastype"/>

    Is that what was happening?

    Carl

    Yes that's what's happening but it indicates an error somewhere.

    Some of the data ($data>sub_id->$name) will be either 1 or undef. In
    it's original form my subroutine would display checked="true" when the
    value ==1. Now none of the checkboxs are displayed as checked.

    I can't find the perldoc for this default method you've mentioned.
    sub_id is a foriegn key, and $name contains the checkbox name
    ('hastype'). I didn't think I could use $form->model->default_values()
    in this context which is why I used the attributes method.

    Sorry, am I confusing matters? What I am trying to do is, loop through
    all the checkbox elements, take the name, cross-reference that name
    (hastype1, hastype2..etc) in a different table by using the foriegn
    key, sub_id. If sub_id->hastype1 == 1, the checkbox should be checked,
    otherwise not.
    From a FormFu point of view the HTML is rendered correctly when I used
    attributes.

    Am I really off-track by using attributes rather than default?
    Thanx,
    Dp.
  • Carl Franks at Aug 28, 2008 at 8:50 am

    2008/8/27 Dermot <paikkos@googlemail.com>:
    2008/8/27 Carl Franks <fireartist@gmail.com>:
    No, if value() eq default(), you'll get:
    <input type="checkbox" name="hastype" checked="checked"/>
    otherwise, you'll get:
    <input type="checkbox" name="hastype"/>

    Is that what was happening?

    Carl
    Yes that's what's happening but it indicates an error somewhere.

    Some of the data ($data>sub_id->$name) will be either 1 or undef. In
    it's original form my subroutine would display checked="true" when the
    value ==1. Now none of the checkboxs are displayed as checked.
    Are you setting value(1) on all the checkboxes?

    It'll only give you checked="checked" if value() eq default()
    You should be setting a value() on all checkboxes anyway, so that the
    browser does something sane.

    If you want value() to be set to '1' on all the checkboxes you create,
    you can do it the easy way with the following yaml:

    default_args:
    elements:
    Checkbox:
    value: 1
    I can't find the perldoc for this default method you've mentioned.
    sub_id is a foriegn key, and $name contains the checkbox name
    ('hastype'). I didn't think I could use $form->model->default_values()
    in this context which is why I used the attributes method.
    Both value() and default() are documented in:
    lib/HTML/FormFu/Element/_Field.pm
    From a FormFu point of view the HTML is rendered correctly when I used
    attributes.

    Am I really off-track by using attributes rather than default?
    Setting the attributes will only work so far - if you look at
    prepare_attrs() in Checkbox.pm, there's a lot of logic to ensure it
    works properly.
    And several fields override either process_value() or prepare_attrs()
    for specialized behaviour.
    So - if you can, I'd encourage you to try and get it working without
    setting attributes() yourself - but I know that at the end of the day,
    you just need to get it working ;)

    Carl
  • Dermot at Aug 28, 2008 at 9:30 am

    2008/8/28 Carl Franks <fireartist@gmail.com>:
    2008/8/27 Dermot <paikkos@googlemail.com>:
    2008/8/27 Carl Franks <fireartist@gmail.com>:
    No, if value() eq default(), you'll get:
    <input type="checkbox" name="hastype" checked="checked"/>
    otherwise, you'll get:
    <input type="checkbox" name="hastype"/>

    Is that what was happening?

    Carl
    Yes that's what's happening but it indicates an error somewhere.

    Some of the data ($data>sub_id->$name) will be either 1 or undef. In
    it's original form my subroutine would display checked="true" when the
    value ==1. Now none of the checkboxs are displayed as checked.
    Are you setting value(1) on all the checkboxes?
    No I am not. Here is the debug output I get from this code snippet:

    my $checkboxes = $form->get_all_elements({type => 'Checkbox'});
    foreach my $box (@{$checkboxes}) {
    my $name = $box->name;
    my $val = $data->sub_id->$name;
    $c->log->debug("$name VAL=$val");
    $box->default($val);

    }
    [debug] Catalyst::Controller::HTML::FormFu::Action::FormConfig
    searching for file ....
    [debug] hastype1 VAL=
    [debug] hastype2 VAL=
    [debug] hastype3 VAL=
    [debug] hastype4 VAL=1
    [debug] hastype5 VAL=1
    [debug] hashtype6 VAL=1
    [debug] hastype7 VAL=1
    [debug] hastype8 VAL=1
    [debug] hastype9 VAL=1

    My form is rendered with all checkbox's unchecked.

    Am I mistaken in thinking a true value for a checkbox element should
    render 'checked="checked"' and an undef (or 0) will render the
    checkbox unchecked?


    It'll only give you checked="checked" if value() eq default()
    You should be setting a value() on all checkboxes anyway, so that the
    browser does something sane.
    Fair enough.
    I can't find the perldoc for this default method you've mentioned.
    sub_id is a foriegn key, and $name contains the checkbox name
    ('hastype'). I didn't think I could use $form->model->default_values()
    in this context which is why I used the attributes method.
    Both value() and default() are documented in:
    lib/HTML/FormFu/Element/_Field.pm
    Thanks that's useful.
    From a FormFu point of view the HTML is rendered correctly when I used
    attributes.

    Am I really off-track by using attributes rather than default?
    Setting the attributes will only work so far - if you look at
    prepare_attrs() in Checkbox.pm, there's a lot of logic to ensure it
    works properly.
    And several fields override either process_value() or prepare_attrs()
    for specialized behaviour.
    So - if you can, I'd encourage you to try and get it working without
    setting attributes() yourself - but I know that at the end of the day,
    you just need to get it working ;)
    I think I may have to go this way for the moment as I can't seem to
    get the desired result from $checkbox->value();

    Thanx,
    Dp.
  • Carl Franks at Aug 28, 2008 at 10:44 am

    2008/8/28 Dermot <paikkos@googlemail.com>:
    2008/8/28 Carl Franks <fireartist@gmail.com>:
    Are you setting value(1) on all the checkboxes?
    No I am not. Here is the debug output I get from this code snippet:
    Am I mistaken in thinking a true value for a checkbox element should
    render 'checked="checked"' and an undef (or 0) will render the
    checkbox unchecked?
    Yes - you're mistaken ;)

    It'll only give you checked="checked" if value() equals default()

    That's the important part ^^^

    If you see checkboxes as simply true/false boolean - and you're using
    Catalyst-Controller-HTML-FormFu, then you can simply make all
    checkboxes have a value="1" by setting this in your myapp.yml config
    file:

    Controller::HTML::FormFu:
    constructor:
    default_args:
    elements:
    Checkbox:
    value: 1

    If you set that, the problem will be solved!

    The reason that's not a default, is that checkboxes aren't always a
    simple boolean - and it's so simple to set your own application-wide
    defaults using the above construct.

    <sidetrack>
    Ok, I had been mistaken that the browser wouldn't send anything if a
    checkbox doesn't have a value.
    I've just checked all the browsers at my disposal, and they all send
    the value "on" if there's no explicit value - so at least they're
    consistent.
    However - I still don't think it's good to rely on that behaviour -
    particularly if you intend on using HTML-FormFu-Model-DBIC.

    If the checkbox relates to a BOOL column, then the checkbox should
    have value="1", so that Model-DBIC can update the database correctly.
    </sidetrack>

    btw - If you do want to try using Model-DBIC for this - can you start
    a new thread, and post the relevant DB schema and form config - I
    couldn't quite follow your earlier description, so I'm not sure
    whether Model-DBIC can do what you want (yet) ;)

    Carl

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouphtml-formfu @
categoriesperl, catalyst
postedAug 27, '08 at 1:19p
activeAug 28, '08 at 10:44a
posts9
users3
websitemetacpan.org...

People

Translate

site design / logo © 2022 Grokbase