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

+- Form Tools (https://forums.formtools.org)
+-- Forum: Form Tools (https://forums.formtools.org/forumdisplay.php?fid=1)
+--- Forum: General Discussion (https://forums.formtools.org/forumdisplay.php?fid=5)
+--- 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:
Code:
<?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
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $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);
    
    if(empty($errors))
    {
        $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;
        }
        ft_api_process_form($params);
    }
    else
    {
        $fields = array_merge($fields, $_POST);
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <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.");
  -->
  </script>
  </head>
  <body>
<div class="title">Donate</div>
<p> Use the form below to donate. This is an <em>extended</em> example form for the <a href="http://docs.formtools.org/tutorials/paypal/" target="_blank">Form Tools 2 PayPal integration tutorial</a>. </p>
<div id="rsvError"></div>
<?php
if(!empty($errors))
{
    echo "<div class='error'>Please correct the following errors, and resubmit the form:".
         "<ul>";
    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">
    <tr>
        <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>
      </tr>
    <tr>
        <td class="red">*</td>
        <td>Last Name</td>
        <td></td>
        <td><input type="text" name="lastname" id="lastname" value="<?=@$fields['lastname']?>" /></td>
      </tr>
    <tr>
        <td class="red">*</td>
        <td>Email Address</td>
        <td></td>
        <td><input type="text" name="email" id="email" value="<?=@$fields['email']?>" /></td>
      </tr>
    <tr>
        <td class="red"></td>
        <td>City</td>
        <td></td>
        <td><input type="text" name="city" id="city" value="<?=@$fields['city']?>" /></td>
      </tr>
    <tr>
        <td class="red">*</td>
        <td>Amount</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>
          </select></td>
      </tr>
    <tr>
        <td></td>
        <td>Subscription</td>
        <td></td>
        <td><input type="checkbox" class="checkbox" name="cmd" id="cmd" value="_xclick-subscriptions" <?php if (@$fields['cmd']) echo 'checked="checked"'; ?> /></td>
      </tr>
    <tr>
        <td></td>
        <td>How often?</td>
        <td></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>
          </select></td>
      </tr>
    <tr>
        <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>
      </tr>
    <tr>
        <td colspan="2"></td>
        <td colspan="2"><div style="padding-top: 5px;">
            <input type="submit" name="submit" value="submit" />
          </div></td>
      </tr>
  </table>
  </form>
</body>
</html>

Update:

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":
        
$regexp="/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
        if (isset(
$fields[$field_name]) && !empty($fields[$field_name]) && !preg_match($regexp$fields[$field_name]))
          
$errors[] = $error_message;
        break; 

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