Re[2]: [PEAR] RE: HTML/QuickForm: Setting rules for Groupelements
| From: | Stanislav Okhvat | Date: | Fri, 07 Jun 2002 10:57:40 +0000 |
| Subject: | Re[2]: [PEAR] RE: HTML/QuickForm: Setting rules for Groupelements | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-1604@lists.php.net to get a copy of this message | ||
Hi, Bertrand!
I used to have the same problem with the good old OOHForms once.
I had to validate groups of fields at the same time without needing to
validate them separately. To solve the issue, I added certain
functionality (not 100% tested, but works as expected on our corporate
site) to OOHForms which made it more robust. Here's how:
Both Javascript and PHP support the eval() statement which is very
handy for such things. I implemented a method called
add_cond($cond_name, $cond_expression, $error_message) on the form
class. Here,
cond_name is the alias (id) of the condition, cond_expression is a
conditional expression of type '(email || tel) && fax', and
error_message is the error message displayed if the condition fails.
The priority of these conditions is determined on the first-come
basis. The expression to check for validity of an individual marker
like fax, email, tel is determined by the condition of the element
itself. Every element needs to implement self_get_short_js method
which returns the javascript to be inserted in the group checking
function. Thus, when the form is returned as HTML, it has a javascript
function named $jvs_name.'_'.$cond_name.'_Validator(f)'; (if you
remember the old OOHForms). This function is checked in the main
validation function further on in $jvs_name.'_Validator(f)';
As to PHP, it is much easier since all one needs to do is eval the
$cond_expression with markers replaced by either true or false
depending on whether the server-side validation for individual
elements is successful/fails.
As to your suggestions on possible sum of fields / subtraction of
fields, I think this is easier to do by providing a way to set a name
of an additional Javascript function which needs to be called
client-size, and providing a similar rule as above which is passed to
PHP for server-side validation. The reason why client-side code should
not be generated is that it is much more difficult to construct such
code with Javascript and eval alone could probably not handle that.
I know it sounds a bit messy, if you need the updated OOHForms with
this functionality, email me.
Best regards,
Stanislav
BM> le 7/06/02 1:06, Greg Gilbert à greg@treke.net a écrit :
>> * Bertrand Mansion (bmansion@mamasam.com) wrote:
>>> You can't validate against groups in QuickForm. You are the second person
>>> who's asking for that. It might be implemented some day as I see it can be
>>> useful. For now, just make a single textfield of your group.
>>
>> I'd also like to see this, or even better, a way to use multiple
>> elements in a validation rule for a single element. For example,
>> using an items text field, term text field, and borrower text field
>> as seperate fields, but needing all of that data to validate the
>> items field.
BM> Ok. I think this can be done but there are many ways to validate against a
BM> group of elements. For instance, you can choose to validate against a
BM> concatenation of all the elements, this is useful for phone numbers. Or you
BM> can choose to validate against a sum of elements, this is useful for an
BM> invoice. Or you can choose to validate all group elements separately, this
BM> is useful for a first-lastname group. And there are probably others.
BM> How do you think this problem can be solved elegantly ?
BM> Bertrand Mansion
BM> Mamasam
Best regards,
Stanislav mailto:stanis@sibfair.nsk.su