The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 871 - File: showthread.php PHP 7.2.34 (Linux)
File Line Function
/showthread.php 871 errorHandler->error



FORUMS


The Form Tools forums are no longer active, but the old posts have been archived here. Please see the Help page on how to get help / report issues.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with tutorial and repopulating fields
#1
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?
Reply


Messages In This Thread
Problems with tutorial and repopulating fields - by jclermont - Oct 6th, 2009, 8:41 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)