Form Tools
Problems with tutorial and repopulating fields - Printable Version

+- Form Tools (https://forums.formtools.org)
+-- Forum: Modules / Other (https://forums.formtools.org/forumdisplay.php?fid=8)
+--- Forum: Form Validation: JS + PHP (https://forums.formtools.org/forumdisplay.php?fid=18)
+--- Thread: Problems with tutorial and repopulating fields (/showthread.php?tid=321)



Problems with tutorial and repopulating fields - jclermont - Oct 6th, 2009

I studied the tutorial at http://docs.formtools.org/tutorials/php_validation/ to setup server-side validation. I had no trouble getting the validation to work by following the tutorial exactly. However, if the validation rules failed, it would not repopulate the fields that the user had submitted.

In particular, if I output the contents of $fields like this:

PHP Code:
$fields ft_api_init_form_page(5);
print_r($fields); 

the only two values would be the form id and submission id, but none of the user data.

I was able to get it working as desired, but I had to deviate significantly from the tutorial. Here are the changes I made (I add comments to the areas I deviated from the tutorial code):

PHP Code:
require_once("formtools/global/api/api.php");
//I don't retrieve the $fields here
ft_api_init_form_page(5);
//I setup a partial $params array for basic processing
$params = array(
    
"submit_button" => "Submit",
    
"form_data" => $_POST
);
$errors = array();
if (isset(
$_POST['Submit'])) {
    
$rules = array();
    
$rules[] = "required,BusinessName,Please enter your business name.";
    
$rules[] = "required,FirstName,Please enter your first name.";
    
$rules[] = "required,LastName,Please enter your last name.";
    
$rules[] = "required,Address,Please enter your address.";
    
$rules[] = "required,City,Please enter your city.";
    
$rules[] = "required,State,Please enter your state.";
    
$rules[] = "required,Zip,Please enter your zip.";
    
$rules[] = "required,Phone,Please enter your phone number.";
    
$errors validate_fields($_POST$rules);

    if (empty(
$errors)) {
//i add the additional params to finalize and go to thank you page
//if we pass validation
        
$params["next_page"] = "thank-you-sample.php";
        
$params["finalize"] = true;
    }
//i always process the form, even if validation fails
    
ft_api_process_form($params);
}
//now i grab the $fields after the form has been processed
$fields ft_api_init_form_page(5); 

Is my approach valid? Did I miss something obvious in the tutorial?


RE: Problems with tutorial and repopulating fields - jclermont - Oct 13th, 2009

Any opinion on this one? Looking forward to some input. Thank you.


RE: Problems with tutorial and repopulating fields - jclermont - Oct 26th, 2009

Anyone willing to take a few minutes and respond to this? Either I'm missing something obvious, or there are likely other people who will be frustrated with the published tutorial in the documentation.


RE: Problems with tutorial and repopulating fields - Ben - Oct 29th, 2009

Hi jclermont,

Sorry for the wait - very busy week!

Yes, your approach looks fine - it's actually not bad at all! The only thing I'm not terribly thrilled about is calling the ft_api_init_form_page() function twice... nevertheless, it'll work fine without any side-effects that I can see.

The tutorial DOES needs updating - I'm very sorry for all the confusion its created. I'll make sure it gets done this Saturday.

Thanks for the very clear posts, jclermont! Very helpful!

- Ben


RE: Problems with tutorial and repopulating fields - jclermont - Oct 30th, 2009

Thanks Ben! I look forward to seeing the solution you come up with.

I must say that I'm very impressed with FormTools. I'm going to study the code more, then pitch in and help with some of the open bugs on your issue tracker.


RE: Problems with tutorial and repopulating fields - MatthewHenry - Sep 30th, 2020

Problems with tutorial and repopulating fields are hard to find that is the reason many developers want help. It is a great place to find the help from the top professionals here. When i read the 99papers I come to know that it makes us easy to find a solution.