NOOB questions:

10 views
Skip to first unread message

Hoppfrosch

unread,
Jan 12, 2012, 5:29:34 AM1/12/12
to formhandler
I'm trying to use FormHandler within my Catalyst-application (also
using DBIC) - but I have some startup problems which I cannot solve
yet ... Hope anybody can give me some hints here:

What I have is a form, defined in a own package:
--START CODE---
package BDBInfo2::Form::Userinfo::Edit;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Model::DBIC';
with 'HTML::FormHandler::Render::Simple';
use namespace::autoclean;

has '+item_class' => ( default =>'Userinfo', );
has_field 'userinfo_kuerzel' => ( type => 'Text', label =>
'Userkuerzel' );
has_field 'fk_bdbrolle_pk' => ( type => 'Select', label => "Rolle",
label_column => 'bdbrolle_name');
has_field 'submit' => ( type => 'Submit', value => 'Uebernehmen');
has_field 'cancel' => ( type => 'Button', value => 'Abbrechen' );
---END CODE---

Now I try to use this within my catalyst controller:
--START CODE---
has 'edit_form' => ( isa => 'BDBInfo2::Form::Userinfo::Edit', is =>
'rw',
lazy => 1, default => sub { BDBInfo2::Form::Userinfo::Edit-
>new } );
...
sub edit :Chained("base") :PathPart("edit") :Args(1) {
my ( $self, $c, $arg1 ) = @_;
my $userinfo = $c->model('BDB::Userinfo')->find($arg1);

my $result = $self->edit_form->run( item => $userinfo, params => $c-
>req->parameters);
$c->stash( template => 'userinfo_edit.tt', form => $result );

if (!$result->validated) {
use Data::Dumper;
$c->log->debug( "------------------------------"));
$c->res->redirect( $c->uri_for('edit/'.$arg1) );
}
else {
$c->log->debug("+++++++++++++++++++++++++++"));
$c->res->redirect( $c->uri_for('edit/'.$arg1) );
}
return
}
---END CODE---

What currently happens is running an endless loop when calling the
"edit"-URI. I know this comes from my "redirect"-call, but I assumed
this happens only after validation - and validation should only be
performed (as I thought) after pressing the "submit" or "cancel"
button. This seems not be the case.

I don't understand where the form processing loop is done, waiting for
the event of "pressing a button"?
How is the "proper" code for processing the two possible events
"cancel" and "submit"? (I don't even know how to determine whether
"cancel" or "submit" button was pressed ...)

Any hints welcome - and excuse those obvious NOOB questions, but I
can't find a solution yet, unless I researched/tried/googled a lot ...

Hoppfrosch

Matthias Dietrich

unread,
Jan 12, 2012, 6:22:00 AM1/12/12
to formh...@googlegroups.com
Hi,

this is not a HFH error but a logical mistake.  You're redirecting in every possible way:

 if (!$result->validated) {

   $c->res->redirect( $c->uri_for('edit/'.$arg1) );
 } else {
   $c->res->redirect( $c->uri_for('edit/'.$arg1) );
 }

So it's no wonder you get redirected ;-).

BTW. when you're doing a redirect like this you're losing your params, which will result in an empty $c->req->parameters.  $result->validated will return false then and redirect you over and over again.  But I wonder why you want to do a redirect at all here...

What you need to do is to check whether your form was submitted or not.  One way would be to define a field that is submitted with the form every time, like 'submit'.  In your controller check if $c->req->params->{submit} is set, if so pass $c->req->params to HFH and validate.  If it doesn't exist, show only the form.  No redirect involved.

Matthias

-- 
rainboxx Software Engineering
Matthias Dietrich

rainboxx Matthias Dietrich   |  Phone: +49 7141 / 2 39 14 71
Königsallee 43               |  Mobil: +49  151 / 50 60 78 64
71638 Ludwigsburg            |  WWW  :  http://www.rainboxx.de




Hoppfrosch

unread,
Jan 12, 2012, 6:38:37 AM1/12/12
to formhandler


On 12 Jan., 12:22, Matthias Dietrich <rainb...@googlemail.com> wrote:
> Hi,
>
> this is not a HFH error but a logical mistake.  You're redirecting in every possible way:
>
> >  if (!$result->validated) {
> >    $c->res->redirect( $c->uri_for('edit/'.$arg1) );
> >  } else {
> >    $c->res->redirect( $c->uri_for('edit/'.$arg1) );
> >  }
>
> So it's no wonder you get redirected ;-).

I also understood that I got redirected over and over again ...
I'm/was in a position where I cannot differ between logical error and
HFH error - hence I asked here ... ;-)

> BTW. when you're doing a redirect like this you're losing your params ...

Good hint ...

> But I wonder why you want to do a redirect at all here...

It has nothing to do with my real intentions ... consider it as a
simple placeholder.

> What you need to do is to check whether your form was submitted or not...

Will try this ...

Thanks
Hoppfrosch

> 71638 Ludwigsburg            |  WWW  :  http://www.rainboxx.de

P.S: Hab au 15 Johr in Schdugerd und LuBu (Oßweil) gwohnt (jetzt nicht
mehr im geliebten Schwabenländle) ...
Reply all
Reply to author
Forward
0 new messages