< Validation: Resulting Arrays   (Previous) Table of Contents (Next)   More Info >

Validation: Performing the Check

/**
 * Validates data under the current element using the DataFields array.
 *
 * @param   string   $Elem  the current element name
 * @return  integer  1 if valid, 0 if not
 */
function validateDataFields($Elem) {
    //  Ensure $DataFields array has validation types ready for this element.
    if ( empty($this->DataFields[$this->ParentElements[0]][$Elem]) ) {
        $this->Probs[] = "$Elem: DataFields[{$this->ParentElements[0]}][$Elem] is empty";
        $this->IgnoreTheRest = 'Y';
        return 0;
    }

    $Problems = 0;
    reset($this->DataFields[$this->ParentElements[0]][$Elem]);

    // > >  GO THROUGH EACH FIELD NEEDED FROM THIS PARENT ELEMENT.  < <
    foreach ($this->DataFields[$this->ParentElements[0]][$Elem] AS $Field => $Type) {

        //  Ensure $DataTypes array has validation types ready for this type.
        if ( empty($this->DataTypes[$Type]) ) {
            $this->Probs[] = "$Elem: DataTypes[$Type] is empty";
            $Problems++;
            continue;
        }

        //  If this field isn't set, don't even bother checking type.
        if ( !isset($this->Data[$Field]) ) {
            $this->Probs[] = "$Elem: $Field isn't set";
            $Problems++;
            continue;
        }

        // > >  DOES THE DATA IN THIS FIELD MATCH THE EXPECTED TYPE?  < <
        if ( !preg_match($this->DataTypes[$Type], $this->Data[$Field]) ) {
            $this->Probs[] = "$Elem: $Field does not match $Type: {$this->Data[$Field]}";
            $Problems++;
        }
    }

    if ( !empty($Problems) ) {
        $this->IgnoreTheRest = 'Y';
        return 0;
    }
    
    return 1;
}