Re: [Catalyst] State of the art in form validat =?WINDOWS-1252?Q?ion; _opinion_poll=85_Model_based_forms/validation=3F?=

Carl Franks fireartist at gmail.com
Wed Dec 5 09:16:08 GMT 2007


On 05/12/2007, Ashley Pond V <apv at sedition.com> wrote:
> What are best practices? DBIx::Class::Validation reusing your form
> package profiles? But you still have to write your own forms. I've
> searched around a lot and can't find a thorough, coherent Catalyst
> related doc about it.
>
> I really want one, final approach to this I'll use every time from
> now on instead of experimenting with packages and home rolled stuff
> over and over. If someone is there, or even close. Please kick down.

I'm really happy with how HTML::FormFu is coming along.
Like your example, you can define your entire form in any config file
Config::Any supports. Although I (predictably) prefer the FormFu style
declarations.

It has lots of little shortcuts that make things easier, such as auto_label().
If you set, for example, auto_label('label_%n') at the form-level,
then all fields will automatically use localize() to set their own
label (replacing the '%n' with their field name).
Like many settings, this can be set at either the form-, the
fieldset-, or the field-level, and when the method's used as a
'getter', it traverses the field's parents, searching for a value.

There's also an auto_error_message() which can get error messages from
your localize() object, based on the field's name and/or type.

(the next couple of features are currently only in svn, and will be
released to cpan in the next week...)

By default, the form's markup is generated internally by perl
subroutines. However, by simply setting render_method('tt') it
switches to using TT templates (which are installed using
File::ShareDir) and generates the exact same markup.
If you wish to customise the form markup, you can create a local copy
of the templates, and change the form however you want.
You can also use your own, single TT file, much like claco's example
earlier in this thread, explicitly placing individual fields.

There's also DBIC integration, which I'm currently in the process of
documenting - again, on it's way to cpan soon.

If you had, say, these 2 tables:

    table: user
    cols: name
    might_have: address

    table: address
    cols: user, street, city
    belongs_to: user

Then you could create a form like so:

---
elements:
  - name: name

  - type: Fieldset
    nested_name: address
    elements:
      - name: street
      - name: city

  - type: Submit

If you then pass that form a 'user' row, it will automatically
traverse the 'address' relationship, filling in the form fields:
    $form->values_from_model( $user_row );

And when the form's submitted, you can save the changes back to the
database, again, automatically following the relationship:
    $form->save_to_model( $user_row );

It also handles has_one, has_many, and many_to_many relationships.

To finish off, here's an example of how I'd probably write your form
(as far as I can interpret it):

---
auto_fieldset:
  legend: user

elements:
  - name: last_name
    constraints:
      - type: Required
      - type: Length
        max: 50

  - name: suffix
    constraints:
      - type: Regex
        regex: '?-xism:\A[IVX]{1,3}\z'

  - name: email
    constraints:
      - type: Required
      - type: Email
      - type: Length
        max: 80

  - type: Submit

If you're interested in looking further, the svn location is:
http://html-formfu.googlecode.com/svn/trunk/HTML-FormFu
or you can ask any questions on the mailing list:
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu

Cheers,
Carl



More information about the Catalyst mailing list