Re: HTML_QuickForm2_Rule_Callback: inverting callback rule results

From: Date: Tue, 04 May 2010 13:48:31 +0000
Subject: Re: HTML_QuickForm2_Rule_Callback: inverting callback rule results
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-30501@lists.php.net to get a copy of this message
Hi Christian, On 04.05.2010 11:40, Christian Weiske wrote:
I am in the need to verify the user name in a registration form. Part of that process is using the existing API to check various things, including - reserved user names - already existing user names - validity of a user name Methods are something like - $userservice->isUsernameReserved() - $userservice->existsUser() - $userservice->isUsernameValid() I cannot use them with the normal callback rule, since the API methods return true when something is wrong for the name the user put into the form. There are two options now: 0. Extend the API. This is not always feasible 1. Write custom methods that invert the results of the API 2. Get the callback rule to invert the results of the method call 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.

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