/*--------------------------------------------------------------------------------------------------------------------------------
Parameters:
$formField1: The name of the first array
$formField2: The name of the second array
$formField1CompareWith: String to use as my comparison basis for first
array. Defaults to using $val unless it's 'key'
$formField2CompareWith: String to use as my comparison basis for
second array. Same default as $formField1CompareWith
$match: Boolean to look for exact key or value match (default will
compare if instances compare true for both array)
---------------------------------------------------------------------------------------------------------------------------------*/
function isValidArrayCompare($formField1, $formField2,
$formField1CompareWith = '', $formField2CompareWith = '', $match = '')
{
// BOOLEAN TO COMPARE DIFFERENT FORM ELEMENT ARRAYS TOGETHER
reset($this->post);
if (!$formField1 && !$formField2) return false; // BLANK PARAMETERS
if (!in_array($formField1, array_keys($this->errorMsgObjArray)) &&
!in_array($formField2, array_keys($this->errorMsgObjArray))
) return false; // NOT IN ERROR MESSAGE OBJECT ARRAY
if (!in_array($formField1, array_keys($this->post)) &&
!in_array($formField2, array_keys($this->post))
) return false; // NOT IN GET/POST COLLECTION
$hasFoundValFormField1 = false;
if (strcmp(strtolower($formField1CompareWith), 'key') == 0 &&
strcmp(strtolower($formField2CompareWith), 'key') == 0) {
// KEY TO KEY ARRAY COMPARISON
if (@sizeof($this->post[$formField1]) > 0) { // FIRST ARRAY HAS KEYS
$hasFoundValFormField1 = true; $hasFoundValFormField2 = true;
foreach ($this->post[$formField1] as $key) {
if (@!in_array($key, array_keys($this->post[$formField2]))) { //
MATCHING KEY FOUND IN SECOND ARRAY
$hasFoundValFormField2 = false;
break;
}
}
}
} elseif (strcmp(strtolower($formField1CompareWith), 'key') == 0) {
// KEY TO VAL ARRAY COMPARISON
if (@sizeof($this->post[$formField1]) > 0) { // FIRST ARRAY HAS KEYS
$hasFoundValFormField1 = true; $hasFoundValFormField2 = true;
foreach ($this->post[$formField1] as $key) {
if (@!in_array($key, array_values($this->post[$formField2]))) { //
MATCHING VAL TO KEY FOUND IN 2ND ARRAY
$hasFoundValFormField2 = false;
break;
}
}
}
} elseif (strcmp(strtolower($formField2CompareWith), 'key') == 0) {
// VAL TO KEY ARRAY COMPARISON
if (@sizeof($this->post[$formField1]) > 0) { // SECOND ARRAY HAS KEYS
$hasFoundValFormField1 = true; $hasFoundValFormField2 = true;
foreach ($this->post[$formField1] as $key => $val) {
if (!$key[$val] || strlen($key[$val]) == 0) {
$hasFoundValFormField2 = false;
break;
}
}
}
} else {
// VAL TO VAL ARRAY COMPARISON
foreach ($this->post[$formField1] as $key => $val) {
if ($val && strlen($val) > 0 && !empty($val)) {
$hasFoundValFormField1 = true; $hasFoundValFormField2 = true;
if (@!in_array($val, array_values($this->post[$formField2]))) { //
MATCHING VAL FOUND IN SECOND ARRAY
$hasFoundValFormField2 = false;
break;
}
}
}
}
if ($hasFoundValFormField1 && $hasFoundValFormField2) return true;
return false;
}
My class method isValidArrayCompare will be comparing two arrays found
within the form post (passed into the class becoming $this->post). The
method has to have the following conditions:
1) Both arrays can be enumerative or associative, or one can be
enumerative and the other associative
2) Both arrays have to be able to be compared either by key-to-key,
key-to-val, val-to-key, or val-to-val
I have run into snags when you have this unique case:
Array A comes from a checkbox group and thus is not only enumerative,
but DYNAMIC as well (that is, it will only have values you checked
prior and that's all!); Array B comes from a group of dropdowns; each
dropdown down is given the same name but its key is the corresponding
value of the checkbox to the left (like $myCheckbox[0] => 'A' maps to
$myDropdown['A'] => 5). I need to be sure that the comparison will be
either true or false depending on if the value of Array A is found as
a key-with-a-value in Array B, AND if the key-with-a-value in Array B
exists as a value in Array A.
That's just THAT case. There will be a WHOLE lot more variances, but
the 2 rules above will always be true. I thought my method would cover
them all but alas, it fails on the Array A - Array B scenario if I
check more of Array A than I select of Array B for each Array A
checkbox.
You can see why I want to find an out-of-the-box solution for form
validation! >:(
Phil