[Catalyst] HTML::Widget, callback constraint, and multiple records per form

Jason Kohles email at jasonkohles.com
Fri Sep 1 16:46:49 CEST 2006


On 8/31/06, Ben Hopkins <hopkins.ben at gmail.com> wrote:
> Here's what I have:  a table (counties) that has county names in it.  I want
> to be able to add counties.  Just one county per screen seems pretty dumb,
> so I want a bunch of counties.  The sole constraint is that a county cannot
> already be in the database, and I figured the Widgets' constraint method
> would be perfect for that.
>
>  Here I make the widget:
>
>  sub make_counties_widget {
>      my ($self, $c) = @_;
>
>      my $w = $c->widget('county_form')->method('post');
>
>      for my $f (1..10) {
>          $w->element('Textfield', "name$f")->label('County Name:');
>          $w->constraint('Callback',
> "name$f")->callback(&see_if_exists);
>      }
>
>      $w->element('Submit', 'Submit')->value('Submit');
>      $w->element('Reset');
>      return $w;
>  }
>
>  Already, I'm wondering where the error message will go.  Anyhow, I got
> completely lost when writing 'see_if_exists' because the args are
> unintelligible.  It looks like the second arg is the Catalyst context, and
> within there I can find the form results, but if that's the case, why does
> it have to be called once for each field?  It stops being a field validation
> and becomes a form validation called once for each field!?!
>
>  Does anybody use callback?  Does anybody do multiple create/delete/updates
> per form?
>
You are missing one critical character in your code.  Because you left
off the backslash before the function call, your callback isn't a
callback, what is happening is that the function is being run at the
time you create the widget, and is running 10 times because it's
inside a loop.  What is actually getting set as the callback is
whatever &see_if_exists returns.  And because you are calling the
function with the &, but without parens, it is getting passed the same
arguments that were passed to make_counties_widget, which is why they
don't make any sense, and why the context is being passed as a second
argument.

-- 
Jason Kohles
email at jasonkohles.com - http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire



More information about the Catalyst mailing list