|
Posted by Fernando Rodrνguez on 01/19/06 20:30
Hi,
I'm writing code to validate fields in a form before saving to a db. All
the validating functions are in a separate script which is required. All
the validating functions add an error message to an array if the data doesn't
validate. I check if something went wrong with count($theArray).
Here's my code:
---------------------------------------------------------------------------
// An array to keep all the error messages
$errors = array();
// get the checking functions (which use $errors)
require('validating_fns.php');
// Process errors only if the submit button has been pressed.
if (!empty($_POST['Submit'])) {
// Each time there's an error, add an error message to the error array
// using the field name as the key.
//checkFirstName( $_POST['first_name']);
checkLastName( $_POST['last_name'] );
checkEmail( $_POST['email_address'] );
checkEmailAgain( $_POST['email_address'], $_POST['email_address_2'] );
checkPhoneNumber( $_POST['phone_number']);
checkAddress( $_POST['address_line_1']);
checkCity( $_POST['city'] );
checkZipCode( $_POST['zip_code'] );
@checkCountry( $_POST['country'] ); // It might not be set, if there was
nothing selected in the combobox
@checkState( $_POST['state_or_province'] ); // It might not be set, if
there was nothing selected in the combobox
checkPassword( $_POST['password'] );
etc...
---------------------------------------------------------------------------
validating_fns.php looks like this:
---------------------------------------------------------------------
<?php
// Functions that check all fileds
// first and last name
function checkFirstName( $firstName ) {
if (empty($firstName)) {
$errors['first_name'] = 'Please enter your first name.';
}
}
function checkLastName( $lastname ) {
if (empty($lastName)) {
$errors['last_name'] = 'Please enter your last name';
}
}
etc....
---------------------------------------------------------------------
Even though some fields do not validate, when I check the count($errors)
I get 0. Nothing was added! =:-O
What am I doing wrong? O:-)
Thanks!
Navigation:
[Reply to this message]
|