Re: QF update validation
| From: | Bertrand Mansion | Date: | Thu, 06 Nov 2003 00:34:04 +0000 |
| Subject: | Re: QF update validation | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-8798@lists.php.net to get a copy of this message | ||
<pear@daniu.de> wrote :
> Hi!
>
> I also _had_ a problem with the new QF-module. I have a form in a
> QFC-Controller. In the buildForm-function of one Form I had 2 groups which
> were validated by the function checkMax which I discussed earlier here:
>
> function checkMax ($elementName, $array, $max)
> {
> $count=0;
> foreach ($array as $key=>$value)
> if ($value!='') $count++;
> if ($count>$max)
> return false;
> else
> return true;
> }
> *
>
> In buildForm I used:
>
> $this->registerRule('checkmax', 'function', 'checkMax');
> $this->addGroupRule('group',
> 'please either choose a value or enter a new value - not
> both',
> 'checkmax', 1);
>
> Before I had the validation function in the class itself (actually as a
> class method). The new modul needs the function to be accessible global. So
> - for me it worked to put it in a global include file (before I got a Error
> Message telling me the function checkMax could not be found).
>
> Maybe this helps someone else or maybe there is something else wrong with
> the way I did it :-)
>
> Regards
> Daniel
>
> * to get this in JS I guess I need to use the new validation class? Or does
> even that not help me? Maybe someone can explain this as at least I don't
> get how it is supposed to work by the provided example only. I think my
> example is simple enough to make it a further 'case study'.
If you want javascript generated directly by your class, the best way is to
use the following and complete the getValidationScript() method:
class Rule_CheckMax extends HTML_QuickForm_Rule
{
function validate($array, $max = 1)
{
$count = 0;
foreach () {
...
}
return ($count > $max);
}
function getValidationScript($options = null)
{
// Writing the code for javascript here
// Look at the existing rules for examples
}
}
$rule = new Rule_CheckMax();
$form->registerRule('checkMax', null, $rule);
...
$form->addGroupRule('group', 'please either choose a value or enter a new
value - not both', 'checkMax', 1);
Note that you are not forced to register the rule if it is an extension of
HTML_QuickForm_Rule. So you could also simply do:
$form->addGroupRule('group', 'please either choose a value or enter a new
value - not both', 'Rule_CheckMax', 1);
Without the 2 first lines.
The other way would be to write your javascript function manually and
include it in your html page. The function will have to have the same name
as the PHP function and return true or false. Don't forget to add 'client'
parameter to make it work.
Then you could write:
function checkMax($array, $max)
{
$count = 0;
foreach ($array as $key => $value)
if ($value != '') $count++;
return ($count>$max);
}
$this->registerRule('checkmax', 'callback', 'checkMax');
$this->addGroupRule('group', 'please either choose a value or enter a new
value - not both', 'checkmax', 1);
Note that if you use 'callback' instead of 'function', there is no need to
add the 'element name' parameter in the function declaration. As I said,
'function' is kept for BC reasons, the recommended way is 'callback'.
Bertrand Mansion
Mamasam