Oct 6th, 2009, 8:41 AM
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:
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):
Is my approach valid? Did I miss something obvious in the tutorial?
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?