At 06.28 -0700 2009-06-29, Steve Piercy - Web Site Builder wrote:
>First, I assume that the application logic should go into the action file, correct?
The application logic to take care of the form input, correct.
>If so, then here is some sample code I pulled from the Knop demo files.
>
>_action/act_advanced_edit.inc
>-----------------------------
>
> if($f -> isvalid); // edited for brevity
> // add record
> // update session variable
> // uhh.... magic? use an include or redirect_url?
Nothing magic at all since -formaction tells where to go next.
> /if;
>
> if: (!($f -> isvalid) || $f -> error_code)
> && ($f -> getbutton == 'add' || $f -> getbutton == 'save');
> // remain on the same path to show form again
> $nav -> (setlocation: ($nav -> actionpath));
> // show error message
> $message -> (insert: ($f -> error_msg) + ' ' + ($f -> error_code));
> /if;
>
>If I understand that correctly, then the value of the -formaction parameter in the knop form object in the config file determines what is the next page in the sequence:
Correct, -formaction is what you see in the url after submitting.
If the validation fails then the current path does not correspond to the -formaction (or the url) since the path is reverted to the previous form by ->setlocation.
>Is -formaction the "magic" that tells Knop what to load next?
That is totally correct, and this is exactly what I'm trying to say with the "Knop Application Flow" description in the Knop manual (page 15).
Now, on to a different way to handle multistep forms.
URL examples:
checkout/1
checkout/2
checkout/3
In this case only "checkout" is defined in the nav object, so the 1, 2 and 3 are "leftovers" after nav->getlocation is called. The first leftover is returned by nav->getargs(1), for example 1, 2 or 3.
Using the same nav path for all steps means that the same set of config, lib, content and action files are used for all the steps, so inside each of them there is a select/case tree that looks at getargs(1) and produces the appropriate result for each step. This is useful for multistep forms where some or much of the logic can be reused between steps.
The form for each step uses the same -formaction as -actionpath, which is a bit contrary to the above description. So the form doesn't actually advance to the next step upon submission, but instead the action file redirects to the next step if the submission is successful. This gives clean URLs that correspond to the actual current step in the multistep form no matter if the form is successful or not, and it also makes it really easy to go backwards in the chain simply by redirecting to the previous step. The form posts are also "refresh safe" that way.
With this method it's also easy to handle multistep forms that branch off or skip steps.
Some code samples that hopefully illustrates this
config_checkout.inc (just the beginning)
-------------------
var('checkout_stage'=1);
if($nav -> getargs -> size && $nav -> getargs(1) -> size);
$checkout_stage = integer($nav -> getargs(1));
/if;
var('ordersent'=false);
// then configure some forms depending on current $checkout_stage
// ...
action_checkout.inc (sample code)
-------------------
var('continue'=true);
// handle abnormal flow first
if(action_param('button_prev') -> size);
// Got to previous step
$continue=false;
redirect_url(($nav -> url ) + ($checkout_stage - 1));
/if;
// perform actions
select($checkout_stage);
case(1);
// update cart contents
// ...
case(2);
// update shipping and payment details
// ...
case(3);
if($continue);
// add order
inline($db_connect);
var('updatefields'=$s_checkoutform -> updatefields);
$db_order -> addrecord($s_checkoutform -> updatefields);
var('order_id'=$db_order -> keyvalue);
if($db_order -> error_code);
$continue = false;
$message_form -> insert('error'='COuld not cerate order ');
/if;
if($continue && $order_id > 0);
// add order items
// ...
/if;
$ordersent = true;
/inline;
/if;
/select;
// follow normal flow
if(action_param('button_next') -> size
&& $checkout_stage < 3
&& $continue);
// Go to next step
redirect_url(($nav -> url ) + ($checkout_stage + 1));
/if;
--
Johan Sölve [FSA Member, Lasso Partner]
Web Application/Lasso/FileMaker Developer
MONTANIA SOFTWARE & SOLUTIONS
http://www.montania.se mailto:
joh-n@...
(spam-safe email address, replace '-' with 'a')
--
#############################################################
This message is sent to you because you are subscribed to
the mailing list <
knop@...>.
To unsubscribe, E-mail to: <
knop-off@...>
Send administrative queries to <
knop-request@...>
List archive
http://www.nabble.com/Knop-Framework-Discussion-f29076.htmlProject homepage
http://montania.se/projects/knop/Google Code has the latest downloads at
http://code.google.com/p/knop/