[Html-widget] formfu - new features

Carl Franks fireartist at gmail.com
Tue Mar 6 19:25:25 GMT 2007


The following new features have been added to formfu...

*** $form->auto_fieldset( 1 )

This is suitable for most typical forms, and means you can generally
ignore fieldsets.
auto_fieldset(1) immediately adds a fieldset element to the form.
Thereafter, $form->element() will add elements to the fieldset, rather
than the form
(or to the last fieldset, if there's more than 1).

If you add another fieldset, that gets added to the form, not the
existing fieldset, as fieldsets shouldn't be nested.
Any further elements will be added to that second fieldset, instead of
the first.

Also, you may pass a hashref to auto_fieldset(), and this will be used
to set defaults for the first fieldset created.

Consider the following convoluted config:

---
elements:
  - type: fieldset
    legend: The Form
    elements:
      - type: text
        name: name
      - type: text
        name: age
  - type: fieldset
    elements:
      - type: submit
        name : submit

This is much clearer:

---
auto_fieldset:
  legend: The Form
elements:
  - type: text
    name: name
  - type: text
    name: age
  - fieldset
  - type: text
    name: submit

*** AutoSet

There is also a new AutoSet constraint.
(The 'In' constraint from HTML::Widget has been renamed 'Set' in HTML::FormFu)

The AutoSet constraint is only for use with 'select' and 'radiogroup'
elements (or, specifically, anything which inherits from
HTML::FormFu::Element::group).

It ensures the input value is one of the set of values defined in the
element options.
(similar to HTML::Widget::Element::Select's constrain_values() setting)

  - type: select
    values: [yes, no]
    constraints: [AutoSet]

... is the same as:

  - type: select
    values: [yes, no]
    constraints:
      - type: Set
        set: [yes, no]

*** parent(), form()

And for anyone interested in developing their own elements, constraints, etc,
all elements now have parent() and form() methods.

For any element added directly to a form, parent() will return a
reference to the form.
For any element added to a fieldset or other block, parent() returns a
reference to that fieldset or block.
form() automatically traverses the trail of parents to return a
reference to the form.

All constraints, filters, deflators and inflators also have parent()
and form() methods.
Unlike HTML::Widget, these constraints, etc are always immediately
attached to the relevant form field, and never to the form or block
element.
This means that inside the constraint (or whatever), parent() always
return a reference to the relevant form field element.
This is how, for example, the AutoSet constraint works, automatically
getting the list of valid values each time the constraint is run.

*** Regexp::Common

The Regex constraint also now has support for Regexp::Common.
So, if you wanted to check against Regexp::Common's
    $RE{URI}{HTTP}{ -scheme => 'https?' }
to allow both secure and non-secure url's, you can now do:

    $field->constraint('Regex')->common([ 'URI', 'HTTP', { -scheme =>
'https?' } ]);

or, in yaml:

elements:
  - type: text
    name: foo
    constraints:
      - type: Regex
        common: [URI, HTTP, {-scheme, https?}]

*** Localisation

Just as the field methods label(), comment(), value() and default()
have the variants:
    label_xml()
    comment_xml()
    value_xml()
    default_xml()
to ensure that the supplied string isn't xml-escaped...

There are new variants:
    label_loc()
    comment_loc()
    value_loc()
    default_loc()
to allow you to load your label from the I18N object.

---
elements:
  - type: text
    name: name
    label_loc: label_name
  - type: text
    name: age
    label_loc: label_age

Keep an eye out for an upcoming feature that will give you control
over auto-generating the I18N key. For example, the above yaml will
become something like this:

---
auto_label: label_%n
elements:
  - type: text
    name: name
  - type: text
    name: age

Well, I think that's all for now, though I'm sure you can guess that
more will be coming soon!

Cheers,
Carl



More information about the Html-widget mailing list