[Catalyst] Forms: Chapter 1647

Christopher H. Laco claco at chrislaco.com
Thu May 24 15:37:57 GMT 2007


Carl Franks wrote:
> On 24/05/07, Christopher H. Laco <claco at chrislaco.com> wrote:
>> One of the never ending topics for Catalyst seems to be forms: how to
>> build them, validate them, localize them and generally just live with
>> them without hating the toolset or wanting to strangle someone.
>>
>> I don't have a solution for everyone, but I did create a solution for
>> me. I offer it here as yet another piece of the puzzle for those in need
>> of another path. Feel free to steal the code as you see fit.
>>
>> In general, I like FormBuilder for building forms, but I hate its
>> validation. I like FormValidator::Simples ability to accept yaml profile
>> configs (ProfileManager::YAML). I hate that it can't also take messages
>> from yaml, or both in the same shot. And most of all, I hate having to
>> have at least 3 configs: FB, Profile, Messages) and nothing localizing
>> everything in the same way. This is a marriage of all three.
>>
>> The config file format is simple. The root data is FB options. The
>> fields collection holds FB field config and constraints (FVS options)
>> and messages (FVS messages)
>>
>> >     ---
>> >     name: form_name
>> >     method: POST
>> >     javascript: 0
>> >     stylesheet: 1
>> >     sticky: 1
>> >     submit: LABEL_CREATE
>> >     fields:
>> >       - sku:
>> >           type: text
>> >           size: 25
>> >           maxlength: 25
>> >           constraints:
>> >             - NOT_BLANK
>> >             - LENGTH, 1, 25
>> >             - UNIQUE
>> >           messages:
>> >             - NOT_BLANK: "sku may not be blank"
>> >       - name:
>> >           type: text
>> >           size: 25
>> >           maxlength: 25
>> >           constraints:
>> >             - NOT_BLANK
>> >             - LENGTH, 1, 25
>>
>>
>> If you don't specify a message for a constraint, it defaults to
>> FIELDNAME_CONSTRAINT. If you don't label a field, it defaults to
>> LABEL_FIELDNAME. In my case, I use those as keys to a %Lexicon in a I18N
>> module...they could just as well be real names/messages.
>>
>> If you won't want to use yaml, you can just pass the same data structure
>> to parse/new
>>
>> > {
>> >    name =3D> 'form_name',
>> >    method =3D> 'POST'
>> >    fields =3D> [
>> >       sku =3D> {
>> >           type =3D> 'text',
>> >           constraints =3D> [
>> >               'NOT_BLANK',
>> >               'LENGTH, 1, 25'
>> >           ]
>> >       }
>> >    ]
>> > }
>>
>> When the form is validated/rendered, the labels and messages are all
>> passed to an I18N sub you specify for localization...
>>
>> The api mostly mimics FB usage:
>>
>> > my $form =3D Mango::Form->new({
>> >   source =3D> 'path/to/some/config.yml',
>> >   values =3D> $object->get_columns,
>> >   localizer =3D> sub {MyI18N->localize(@_)},
>> >   params =3D> $c->request
>> > });
>> >
>> > $form->field(name =3D> 'someselect', options =3D> \@options);
>> >
>> > $form->render;
>> >
>> > if ($form->submitted) {
>> >   my $results =3D $form->validate;
>> >   if (!$results->success) {
>> >       print @{$results->errors};
>> >   };
>> > };
>>
>> The code isn't the greatest under the covers and does some evil , but it
>> works for me and at least separated my form handling from my controllers
>> with an API.
>>
>> You can steal the form code from here:
>> http://svn.mangoframework.com/CPAN/Mango/trunk/lib/Mango/Form.pm
>>
>> Here's a real config:
>> http://svn.mangoframework.com/CPAN/Mango/trunk/share/forms/admin/product=
s/create.yml
>>
>>
>> It'll change. It'll evolve. It works for me, for now. Have fun with it
>> if you need it, or parts of it. :-)
> =

> The syntax, the usage, the I18N, it all seems strangely familiar...
> http://html-formfu.googlecode.com/svn/
> ;)
> =

> Carl

Right. I just don't personally like FormFu (for mostly the same reasons
I don't like Widget). It's too big for my needs and I already had yaml
configs for FB/FVS everywhere that I didn't want to convert. (Your pod
for yaml file doesn't mention whether messages can be in there as well)...

And I really don't like having to have all that template crud in my root...

Don't get me wrong, I've looked at FormFu a few times, and it's great at
what it does...it just wasn't for me.

And god knows, I don't need another unreleased dependency in my life
right now. :-)

-=3DChris

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 187 bytes
Desc: OpenPGP digital signature
Url : http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070524/cd9b=
8c33/signature.pgp


More information about the Catalyst mailing list