[Catalyst] help with Catalyst::Plugin::Form::Processor

Bill Moseley moseley at hank.org
Thu Dec 6 23:53:43 GMT 2007


On Thu, Dec 06, 2007 at 02:45:51PM -0800, Michael Higgins wrote:
> 
> So, to revisit this overall...
> 
> It looks like, short of writing a hook to DBIC, I have to somehow get
> data into my forms... in particular, a select list. 
> 
> I think the list comes from a method ->options, from the form field
> select object ( but I don't know). Could it be possible to override such
> a thing to accept, say, an array of arrays?

In the form you can define a field as a select:

    favorite_fruit  => Select,

Now, if you had a model class it would see that is a "Select" and then
look for a 1-to-many relationship and fetch the items from the
database and place them in the field's option list.

You can manually set the options in your form class by creating a
method like this when not using a "model" to automatically populate
the list, or when you just want to manually create the options:

    sub favorite_fruit_options {
        my $form = shift;

        return (
            1 => 'Apple',
            2 => 'Grape',
            3 => 'Cherry',
        );
    }


And then later you get that list back:

    $options = $form->field('favorite_fruit')->options;

> $Form::Processor::Field::Select::init_options = sub {
> return [ {label=>'this',value=>'that'}, {label=>'some',value=>'other'}];
> };

I suppose that would work.  You would likely do that in a sub-class
for a custom field.

    package Form::Processor::Field::Fruit;
    use strict;
    use warnings;
    use base 'Form::Processor::Field::Select';

    sub init_options {
        return [
            {label=>'this',value=>'that'},
            {label=>'some',value=>'other'}
        ];
    }
    1;

Or I think so, anyway.



> How can I get my 'select' to look like a drop down box?

Well, a drop down box is a presentation issue.  I'm not sure if this
is in that example "form_widgets.tt" file, but I have had it where

    field( "What's your favorite fruit?", 'favorite_fruit' );

Will display either a drop-down select list or a set of radio buttons
depending on how many options there are.  But, Form::Processor doesn't
determine that.

Well, a field has a "widget" attribute that will give a hint to what
kind of, eh, widget to generate.  So you could define the fields:

    favorite_fruit  => 'Fruit',  # use the Fruit field from above

    dislike_fruit => {
        type    => 'Fruit',
        widget  => 'radio',
    },

And that would give a *hint* to the presentation layer to show the
second field as a radio instead of a drop-down.


>  return {
>         required => {
>             sh_name => {type      => 'Select', size => 20},

By the way, if "sh_name" is something specific to your application and
might be used in multiple forms than you might make a custom field
(like Fruit above) so you can use it in multiple forms and just say:

         required => {
             sh_name => SH_Name,

Then that field can the size set and any custom validation.

-- 
Bill Moseley
moseley at hank.org




More information about the Catalyst mailing list