Form Tools
email validation not working (api.php) - Printable Version

+- Form Tools (
+-- Forum: Form Tools (
+--- Forum: General Discussion (
+--- Thread: email validation not working (api.php) (/showthread.php?tid=1283)

email validation not working (api.php) - crunchers - May 10th, 2011

A bit of an odd one...

Server-side email validation isn't working for some strange reason. I've tried changing the field name in case it conflicts with an already existing variable but no joy. The field is correctly repopulated in the event of a validation error elsewhere; it's merely refusing to validate (i.e. it's not being registered as a rule).

Everything else works a treat otherwise:
<?php require_once($_SERVER['DOCUMENT_ROOT'].'/path/to/formtools/global/api/api.php');

// form settings
$_POST['form_tools_form_id']     = 6;
$_POST['item_name']             = "PayPal Donation";
$_POST['next_page']                = "/path/to/paypal_submit.php";
$_POST['mode']                     = "";    // initialize

// validate form
    $fields = array();
    $rules = array();
    $rules[] = "required,firstname,Please enter your first name.";
    $rules[] = "required,lastname,Please enter your last name.";
    $rules[] = "valid_email,email,Please enter a valid email address.";
    $rules[] = "required,amount,Please enter an amount.";
    $errors = array();
    $errors = validate_fields($_POST, $rules);
        $fields = ft_api_init_form_page($_POST['form_tools_form_id']);
        $params = array("submit_button" => "submit",
                        "next_page"     => $_POST['next_page'],
                        "form_data"     => $_POST
        if($_POST['mode'] == "initialize")
            $params['finalize'] = true;
        $fields = array_merge($fields, $_POST);
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <title>Donation Form</title>
  <script type="text/javascript" src="includes/rsv.js"></script>
  <link rel="stylesheet" type="text/css" href="includes/main.css">
  <script type="text/javascript">
  var rules = [];
  //rules.push("required,firstname,Please enter your first name.");
  //rules.push("required,lastname,Please enter your last name.");
  //rules.push("required,email,Please enter your email address.");
<div class="title">Donate</div>
<p> Use the form below to donate. This is an <em>extended</em> example form for the <a href="" target="_blank">Form Tools 2 PayPal integration tutorial</a>. </p>
<div id="rsvError"></div>
    echo "<div class='error'>Please correct the following errors, and resubmit the form:".
    foreach ($errors as $error)
      echo "<li>$error</li>";
    echo "</ul></div><br />";
<form action="<?php echo $_SERVER["PHP_SELF"]?>" method="post" onSubmit="return rsv.validate(this, rules)">
    <input type="hidden" name="item_name" value="<?=@$fields['item_name']?>" />
    <table border="0" cellspacing="1">
        <td width="10" class="red">*</td>
        <td width="110">First Name</td>
        <td width="10"></td>
        <td><input type="text" name="firstname" id="firstname" value="<?=@$fields['firstname']?>" /></td>
        <td class="red">*</td>
        <td>Last Name</td>
        <td><input type="text" name="lastname" id="lastname" value="<?=@$fields['lastname']?>" /></td>
        <td class="red">*</td>
        <td>Email Address</td>
        <td><input type="text" name="email" id="email" value="<?=@$fields['email']?>" /></td>
        <td class="red"></td>
        <td><input type="text" name="city" id="city" value="<?=@$fields['city']?>" /></td>
        <td class="red">*</td>
        <td align="right"><b>&pound;</b></td>
        <td><select name="amount" id="amount">
            <option value="" <?php if (@$fields['amount'] == "") echo "selected"; ?>>Select</option>
            <option value="2.00" <?php if (@$fields['amount'] == "2.00") echo "selected"; ?>>2.00</option>
            <option value="5.00" <?php if (@$fields['amount'] == "5.00") echo "selected"; ?>>5.00</option>
            <option value="10.00" <?php if (@$fields['amount'] == "10.00") echo "selected"; ?>>10.00</option>
        <td><input type="checkbox" class="checkbox" name="cmd" id="cmd" value="_xclick-subscriptions" <?php if (@$fields['cmd']) echo 'checked="checked"'; ?> /></td>
        <td>How often?</td>
        <td><select name="p3" id="p3">
            <option value="" <?php if (@$fields['p3'] == "") echo "selected"; ?>>Select</option>
            <option value="1" <?php if (@$fields['p3'] == "1") echo "selected"; ?>>Monthly</option>
            <option value="4" <?php if (@$fields['p3'] == "4") echo "selected"; ?>>Quarterly </option>
            <option value="12" <?php if (@$fields['p3'] == "12") echo "selected"; ?>>Yearly</option>
        <td class="red"></td>
        <td>Gift Aid</td>
        <td><input type="hidden" name="on0" id="on0" value="Donation with Gift Aid" /></td>
        <td><input type="checkbox" name="os0" id="os0" value="YES" <?php if (@$fields['os0']) echo 'checked="checked"'; ?> /></td>
        <td colspan="2"></td>
        <td colspan="2"><div style="padding-top: 5px;">
            <input type="submit" name="submit" value="submit" />


Seems to be a problem with "valid_email" param; it works when i use this:

PHP Code:
$rules[] = "required,email,Please enter a valid email address."

but not this:

PHP Code:
$rules[] = "valid_email,email,Please enter a valid email address."

Update 2:

What a moron (me)! Just checked your validation script:

PHP Code:
// doesn't fail if field is empty
case "valid_email":
        if (isset(
$fields[$field_name]) && !empty($fields[$field_name]) && !preg_match($regexp$fields[$field_name]))
$errors[] = $error_message;

I need to include BOTH rules to the field concurrently: valid_email AND required.

Makes perfect sense.

RE: email validation not working (api.php) - Ben - May 16th, 2011

Excellent! Glad you got it working. Smile

- Ben