HTML_QuickForm2 / container rules
| From: | Christian Weiske | Date: | Thu, 15 Jan 2009 07:47:01 +0000 |
| Subject: | HTML_QuickForm2 / container rules | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-51426@lists.php.net to get a copy of this message | ||
Hello together,
As Alexey suggested subversively, we should discuss about rules in
HTML_QuickForm2 containers.
Currently, there is no really easy way to create rules that apply to
multiple elements in an container/group. We solved a specific problem
of ours by summing up all values in the container and applying the rule
to it. This doesn't solve problems for non-numeric elements, nor for
other rules.
What we also try to achieve is setting the error message to the
container, not to each of the single items involved.
The current implementation of rules in QF2 uses $element->getValue() on
the element the rule is associated to. This is something that hinders
us to e.g. validate the sum of several elements.
A solution to this problem could be that the rule got a callback
function that is executed to return the value. The callback would need
at least one parameter: The element the rule is attached to. By
iterating over the elements, one could calculate the exact value one
desires.
While this would be handy, it would require a new callback for each
setting/combination of elements. Passing an array of elements to be
taken into consideration would make it more flexible.
That way QF2 could provide some pre-defined oftenly needed functions.
An example:
I have three elements and want to make sure the sum of two of them is
lesser than 5.
I'd do that:
$form->addRule(
'lte',
'Sum may not be greater than 4',
4,
'valueCallbackFunctionSum',
array('number_1', 'number_3')
);
the callback 'valueCallbackFunction' would be called with the $form
element as first, and the array of elements to get values from as
second parameter.
It could look like this:
function valueCallbackFunctionSum($form, $arElements) {
$nSum = 0;
foreach ($arElements as $id) {
$nSum += $arElements[$id]->getValue();
}
return $nSum;
}
Thouhts on this?
--
Regards/Mit freundlichen Grüßen
Christian Weiske
-= Geeking around in the name of science since 1982 =-
Attachment: [application/pgp-signature] signature.asc
Attachment: [application/pgp-signature] signature.asc