[html-formfu] I need the most basic tutorial

Carl Franks fireartist at gmail.com
Wed May 12 21:51:00 GMT 2010


On 12 May 2010 09:16, Bart Lateur <bart.lateur at telenet.be> wrote:

>  * How do I put the data from the hash \%data into the form? Should I
> use default_values for that? Or something else? process?

yes, use $form->default_values( \%data )

http://search.cpan.org/~cfranks/HTML-FormFu-0.06001/lib/HTML/FormFu.pm#default_values

>  * I assume I must "process" the input to put the updated data into the
> form?

Always call $form->process() before rendering the form.

http://search.cpan.org/~cfranks/HTML-FormFu-0.06001/lib/HTML/FormFu.pm#process

>  * Apparently a partially updated form drops every default value? Is
> that correct behaviour?

Yes, that's the expected behaviour.
To override it, set $field->retain_default( 1 )

http://search.cpan.org/~cfranks/HTML-FormFu-0.06001/lib/HTML/FormFu/Element/_Field.pm#retain_default

>  * How do I get the data back out of the processed form, in order to
> update the DB? I'm especially worried about that boolean

$data = $form->params;

http://search.cpan.org/~cfranks/HTML-FormFu-0.06001/lib/HTML/FormFu.pm#params

>  * How do I replace the Checkbox with a pair of radio buttons? Just
> replacing "Checkbox" with "Radio" produces an unusable result, and the
> docs for  HTML::FormFu::Element::Checkbox are useless.

Radio will only give you 1 radio field - use Radiogroup instead.

http://search.cpan.org/~cfranks/HTML-FormFu-0.06001/lib/HTML/FormFu/Element/Radiogroup.pm

>  * How does basic validation fit in? How do I flag an error, for
> example if text in a field is too long, or contains illegal characters?

Add an appropriate constraint to the field, e.g.

    element:
      - name: foo
        constraint:
          - type: MaxLength
            max: 8

When you receive the submitted form, check if it contains errors, and
if it does, return the form to the user again.
FormFu will automatically output appropriate error messages.

http://search.cpan.org/~cfranks/HTML-FormFu-0.06001/lib/HTML/FormFu/Constraint.pm#CORE_CONSTRAINTS

>  * Where does validation in Javascript, i.e; without round trip to the
> server, fit in?

This is being developed in a separate subversion branch, but still has
a fair amount of work needing done to it.

There's a note about how to achieve this with HTML::FormFu::ExtJS

http://search.cpan.org/~perler/HTML-FormFu-ExtJS-0.076/lib/HTML/FormFu/ExtJS.pm#And_how_do_I_add_client_side_validation?

>  * What's the best way to have a single form for both "add" and "edit",
> depending on whether "id" is set or not?

A form can only have one 'action' attribute, so this must be handled
in your server-side code.
Something like this (assuming you've already checked
$form->submitted_and_valid):

    if ( $form->valid('id') ) {
        # edit
    }
    else {
        # create
    }

>  * How do you change the way items of the form are rendered, for
> example, what if I prefer the form to be in a 2 column table, labels in
> column 1, fields in column 2?

Probably the easiest is to set FormFu to use the TT template files,
and edit the 'field' template to meet your needs.

You'll need to set $form->render_method('tt') and also
$form->tt_args({ INCLUDE_PATH => $path_to_template_files })

http://search.cpan.org/~cfranks/HTML-FormFu-0.06001/lib/HTML/FormFu.pm#ADVANCED_CUSTOMISATION

>  * What's the best practice to flag to a user a field is required? How
> does it fit into FormFu?

If you set $form->auto_constraint_class('field_%t')
then the div around each field will have a class name added for each
constraint added to that field.
You can then target that class with CSS,

    div.field_required label:after {
        content: ' *'
    }

http://search.cpan.org/~cfranks/HTML-FormFu-0.06001/lib/HTML/FormFu.pm#auto_constraint_class

Cheers,
Carl



More information about the HTML-FormFu mailing list