Re: HTML_QuickForm2_Rule_Callback: inverting callback rule results

From: Date: Wed, 05 May 2010 15:26:36 +0000
Subject: Re: HTML_QuickForm2_Rule_Callback: inverting callback rule results
References: 1 2 3  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-30511@lists.php.net to get a copy of this message
Hi Christian, On 04.05.2010 21:10, Christian Weiske wrote:
Of course I would prefer number 2, since adding a "'inverted' => true" option to the callback rule is much easier and faster than defining an own function/adapting the API. What do you think about that?
While it won't be difficult to add such a parameter, I don't see how $user->addRule('callback', 'Username already taken',
	       array('callback' =>  array($userservice, 'existsUser'),
                       'inverted' =>  true));
is better than $user->addRule('callback', 'Username already taken',
                 array('callback'  =>  create_function(
                  '$v, $userservice',
'return !$userservice->existsUser($v);'), 'arguments' => array($userservice)); or even, in PHP 5.3 $user->addRule('callback', 'Username already taken',
                 function ($v) use ($userservice) {
                    return !$userservice->existsUser($v);
                 });
For me the last example looks A LOT easier to understand.
You are correct. Anyhow any of those are not easy to grasp. I solved the problem for me by registering an own "icallback" rule that just inverts the result of the normal callback - resulting in the most simple code possible.
$user->addRule(
     'icallback', 'Username already taken',
     array('callback' =>  array($userservice, 'existsUser'))
);
Do you feel that the use case is too off-world, or are you accepting patches for that?
I thought about this a bit and it seems to me that 'notcallback' and probably 'notregex' rules may indeed be useful. Maybe it even makes sense to somehow add generic not_() alongside and_() and or_()? Can't come up with a good API, though. $user->addRule('callback', 'Username, etc.', array($userservice, 'existsUser'))
     ->not_();
?

« previous php.pear.general (#30511) next »