[QF] Patch to Fix addRule to Group

From: Date: Tue, 22 Jun 2004 20:38:34 +0000
Subject: [QF] Patch to Fix addRule to Group
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-31120@lists.php.net to get a copy of this message
Hey, I ran into a problem earlier today when I was trying to add a rule to a group that represented an IP address. Adding the rule with addRule() wasn't correctly checking each of the four text boxes in the group, and adding the rule with addGroupRule() would generate 4 errors in the case that all the text boxes were left blank. I posted my concerns to the general PEAR list: http://marc.theaimsgroup.com/?l=pear-general&m=108791609403169&w=2 I offered to supply a patch for the problem so that when a rule is added to a group, the rule is checked against all of the members of the group. That patch is attached to this email. Basically, it checks to see if the element that the rule is being applied to is a group, and if it is, places the necessary javascript inside of a loop and checks each member of the group. If there are any questions about the patch, don't hesitate to ask. --Mark. The patch follows: --- RuleRegistry.php.orig 2004-06-22 16:24:01.000000000 -0400 +++ RuleRegistry.php 2004-06-22 16:24:06.000000000 -0400 @@ -173,15 +173,26 @@ $jsReset .= $tmp_reset; } } + + if ((!is_array($element)) && ($element->getType() == "group")) { + $jsValueLoopStart = "\n for (var i = 0; i < value.length; i++) {\n"; + $jsValueLoopEnd = " }\n"; + $jsVar = "value[i]"; + } else { + $jsValueLoopStart = ""; + $jsValueLoopEnd = ""; + $jsVar = "value"; + } + $jsField = isset($ruleData['group'])? $ruleData['group']: $elementName; list ($jsPrefix, $jsCheck) = $rule->getValidationScript($ruleData['format']); if (!isset($ruleData['howmany'])) { - $js = $jsValue . "\n" . $jsPrefix . - " if (" . str_replace('{jsVar}', 'value', $jsCheck) . " && !errFlag['{$jsField}']) {\n" . + $js = $jsValue . "\n" . $jsPrefix . $jsValueLoopStart . + " if (" . str_replace('{jsVar}', $jsVar, $jsCheck) . " && !errFlag['{$jsField}']) {\n" . " errFlag['{$jsField}'] = true;\n" . " _qfMsg = _qfMsg + '\\n - {$ruleData['message']}';\n" . $jsReset . - " }\n"; + " }\n" . $jsValueLoopEnd; } else { $js = $jsValue . "\n" . $jsPrefix . " var res = 0;\n" . -- /////////////////////////////////////////////////////////////// // // // Mark Drago Phone: 631-434-6600 // // Associate Software Developer Email: mdrago@bascom.com // // BASCOM Global Internet Services, Inc. // // // ///////////////////////////////////////////////////////////////

--- RuleRegistry.php.orig 2004-06-22 16:24:01.000000000 -0400 +++ RuleRegistry.php 2004-06-22 16:24:06.000000000 -0400 @@ -173,15 +173,26 @@ $jsReset .= $tmp_reset; } } + + if ((!is_array($element)) && ($element->getType() == "group")) { + $jsValueLoopStart = "\n for (var i = 0; i < value.length; i++) {\n"; + $jsValueLoopEnd = " }\n"; + $jsVar = "value[i]"; + } else { + $jsValueLoopStart = ""; + $jsValueLoopEnd = ""; + $jsVar = "value"; + } + $jsField = isset($ruleData['group'])? $ruleData['group']: $elementName; list ($jsPrefix, $jsCheck) = $rule->getValidationScript($ruleData['format']); if (!isset($ruleData['howmany'])) { - $js = $jsValue . "\n" . $jsPrefix . - " if (" . str_replace('{jsVar}', 'value', $jsCheck) . " && !errFlag['{$jsField}']) {\n" . + $js = $jsValue . "\n" . $jsPrefix . $jsValueLoopStart . + " if (" . str_replace('{jsVar}', $jsVar, $jsCheck) . " && !errFlag['{$jsField}']) {\n" . " errFlag['{$jsField}'] = true;\n" . " _qfMsg = _qfMsg + '\\n - {$ruleData['message']}';\n" . $jsReset . - " }\n"; + " }\n" . $jsValueLoopEnd; } else { $js = $jsValue . "\n" . $jsPrefix . " var res = 0;\n" .
« previous php.pear.dev (#31120) next »