I always enjoy the logical thinking required for programming. For Symfony it is really nice how it all just flows together. I wanted to implement an ajax check for unique usernames. Something similar like how you see it on Twitter. Lets get started:
First create a validator in lib/validators/sfUniqueUserValidator.class.php
<?php
class sfUniqueUserValidator extends sfValidator
{
public function execute (&$value, &$error)
{
//check if the username exists
$c = new Criteria();
$c->add(sfGuardUserPeer::USERNAME, $value);
$user = sfGuardUserPeer::doSelect($c);
if (!empty($user))
{
$error = $this->getParameter('user_error');
return false;
}
return true;
}
public function initialize ($context, $parameters = null)
{
// Initialize parent
parent::initialize($context);
// Set default parameters value
$this->setParameter('user_error', 'This username is taken');
// Set parameters
$this->getParameterHolder()->add($parameters);
return true;
}
}
Then in your view template use:
<?php echo observe_field('rusername', array(
'update' => 'userstatus',
'url' => 'sfGuardAuth/checkuser',
'with' => "'id='+$('rusername').value",
)) ?>
this will monitor an input field called rusername, and submit its value to the sfGuardAuth/checkuser internal url.
And to glue it all together, in the actions:
public function executeCheckuser()
{
$username = $this->getRequestParameter('id');
$userValidator = new sfUniqueUserValidator();
$userValidator->initialize($this->getContext());
$error='none';
if (!$userValidator->execute($username,$error))
return $this->renderText($username.' is taken');
return $this->renderText($username.' is available');
}
Enjoy!
Ps. any tips for posting code in wordpress would be greatly appreciated, for me it does the strangest types of things.







Lucas responded on 05 Oct 2007 at 4:12 pm #
Hi, nice!! Very simple to use. I was wondering if its possible to use sfPropelUniqueValidator as already checks for primary keys on request params ?
Cheers L
tschellenbach responded on 05 Oct 2007 at 5:36 pm #
I am actually not fully sure how the propel internals work.
Could you blog an explanation?
pawel_k responded on 06 Oct 2007 at 7:30 pm #
you should think about using
‘with’ => “$(’rusername’).serialize()”
instead of
‘with’ => “‘id=’+$(’rusername’).value”
when $(’rusername’).value contains ampersand it can be misleading because in action you receive only part of value typed in input_tag
ps. of course in action you should replace
$username = $this->getRequestParameter(’id’);
with
$username = $this->getRequestParameter(’rusername’);
(see http://www.prototypejs.org/api/form/serialize)
tschellenbach responded on 06 Oct 2007 at 7:35 pm #
Great, thanks for the feedback on the serialization.
Also I should have used the sfPropelUniqueValidator, completely forgot about that one :)
PortalMods.com » Blog Archive » Ajax for unique usernames responded on 29 Oct 2007 at 2:00 am #
[...] Nice and simple to use code snippet to use ajax for validating unique user names. http://www.mellowmorning.com/2007/10/05/ajax-for-unique-usernames/ [...]
Webmaster 38 » Blog Archive » Ajax for unique usernames at ajax scripts compound responded on 04 Jan 2008 at 1:17 am #
[...] Ajax for unique usernames [...]
links for 2008-03-04 | xanders blog responded on 04 Mar 2008 at 5:22 am #
[...] Mellow Morning ยป Ajax for unique usernames (tags: ajax symfony php tts) « links for 2008-02-29 [...]
Anonymous responded on 24 Mar 2008 at 10:57 am #
Free Girls Gone Wild Uncensored Videos.com…
Free Girls Gone Wild Uncensored Videos.com…
me responded on 15 Sep 2008 at 5:29 am #
i have created a template as checknewusersuccess.php and included the above….also am doing this in sfguarduser …i have included (function executeCheckuser()) in sfgaurduser actions….but its not workin….i guess first the action executes and then the template….but the link says the other way….could any1 help me out wit this…as to how the flow goes exactly?