Re: HTML/QuickForm: Setting rules for Groupelements
| From: | Bertrand Mansion | Date: | Fri, 07 Jun 2002 10:45:25 +0000 |
| Subject: | Re: HTML/QuickForm: Setting rules for Groupelements | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-1606@lists.php.net to get a copy of this message | ||
le 7/06/02 12:57, Stanislav Okhvat à stanis@sibfair.nsk.su a écrit :
> 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.
Hi Stanislav,
This looks good and could probably be implemented in QuickForm.
There is already the possibility in QuickForm to use your own javascript
validation function if it is already coded in the page code. But there is no
way to do ((firstname && lastname) || email) validation type. This is
interesting, could you show me your code.
I will try to find the time to do it this weekend.
Thank you,
Bertrand Mansion
Mamasam