Thread: Curly brackets!
View Single Post
Old 05-06-2009, 06:37 PM   #18 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,258
Thanks: 90
Wildhoney is on a distinguished road
Default

This may seem quite complex and superfluous, and it probably is just that for an example. However, I've tried to keep it as modular as possible (No use of exceptions, classes, et cetera...) as I remember what I was like when I first began learning PHP. Didn't know a damn thing!

The theory for the script is as follows:
  • Notify the script of what we require from the form: required fields, and regular expressions (regex);
  • Weed out all the bad items and return those immediately if they fail the validation process;
  • If all is well with the items in the form data then return true;

php Code:
function TalkPHP_Is_This_Form_Valid(array $aFormData)
{
    /* Set a default return that will be returned if validation fails. */
    $aValidatedForm     = array('status' => false);
   
    /* The validation rules, specifying an optional regular expression to validate
    that field. Fields without expressions will be ignored. */

    $aValidationRules   = array('Username' => null, 'Name' => null, 'Age' => '\d', 'DoB' => '\d{2}-\d{2}-\d{4}');
   
    /* Loop through the validation rules specified above. We're going to be working in
    the fashion of: exclude, include. Check all the exclusions first (those that return false).
    If all is well at the end of the loop, return true as it passed! */

    foreach ($aValidationRules as $szKey => $szRegex)
    {
        /* Check to see if the item even exists in the form data. */
        if (!array_key_exists($szKey, $aFormData))
        {
            /* Specify a message so that the user knows what he or she did wrong. */
            $aValidatedForm['message'] = $szKey . ' does not exist';
            return $aValidatedForm;
        }
       
        /* Check to see if the item is simply empty (null, blank). */
        if (empty($aFormData[$szKey]))
        {
            /* Do the same as above to inform the individual. */
            $aValidatedForm['message'] = $szKey . ' cannot be empty';
            return $aValidatedForm;
        }
       
        /* Check to see if we have specified a regular expression for this field. */
        if (is_null($aValidationRules[$szKey]))
        {
            /* Continue the loop if not. This item passed validation if it got this far. */
            continue;
        }
       
        /* Validate the value against the regular expression specified for this field at the top of
        the function. */

        if (!preg_match('~' . $aValidationRules[$szKey] . '~i', $aFormData[$szKey]))
        {
            /* Set the message as usual to notify them of this error. */
            $aValidatedForm['message'] = $szKey . ' syntax is invalid';
            return $aValidatedForm;
        }
    }
   
    /* Everything passed and validated as we expected and so we can
    return true knowing that if something failed in the form data, our script
    wouldn't have gotten this far. */

    $aValidatedForm['status'] = true;
    return $aValidatedForm;
}

php Code:
/* Our array that contains all the form data. Such form data fields can be
constructed using either PHP (Looping through _GET/_POST) or by naming
your form items as like: name="my_form[Name]" */

$aVariables = array
(
    'Username' => 'Wildhoney',
    'Name' => 'Adam',
    'Age' => 23,
    'DoB' => '10-10-1985'
);

/* Validate the form using the values above. */
$aValidatedForm = TalkPHP_Is_This_Form_Valid($aVariables);

if (!$aValidatedForm['status'])
{
    /* If the validation failed then display the message to the user. */
    echo 'Oops: ' . $aValidatedForm['message'];
}
else
{
    /* Otherwise... voila! */
    echo 'Thank you.'
}

I apologise, as it does look confusing to begin with, even to me! However, you'll begin to understand it, I am sure, after a few glances, and after a few questions.
Attached Files
File Type: php Validation.php (2.7 KB, 134 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
Guezala (05-06-2009)