Posted by Pedro Graca on 01/19/06 22:17
Fernando Rodríguez wrote:
> 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).
<snip contents="code"/>
> 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:-)
The functions in the validating_fns.php do not access the $errors array
you defined in your main script; they access their own local variable by
the same name.
Either declare the $errors array global inside each function or pass it
as a parameter (I like this better)
/* declare $errors global */
function checkFirstName( $firstName ) {
global $errors;
if (empty($firstName)) {
$errors['first_name'] = 'Please enter your first name.';
}
}
/* pass $errors as a parameter (by reference, so that it can be changed) */
function checkLastName( $lastName, &$errors ) {
if (empty($lastName)) {
$errors['last_name'] = 'Please enter your last name.';
}
}
--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Navigation:
[Reply to this message]
|