Ajax for Unique Usernames

Ajax for Unique Usernames

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
400 views

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'); 

  }

Ps. any tips for posting code in wordpress would be greatly appreciated, for me it does the strangest types of things.

Source: http://www.mellowmorning.com/2007/10/05/ajax-for-unique-usernames/

http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/stumbleupon_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/delicious_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/blinklist_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/furl_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/technorati_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/magnolia_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/google_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/myspace_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/facebook_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/sphinn_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/mixx_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/twitter_32.png

Related Listings:

  1. Ajax Username Check – Using JQuery! The embedded javascript picks up the “onblur” event of the...
  2. Username availability checker – Ajax Script When you have a name as common as mine, you...
  3. TwitterGitter – mooTools Plugin Everyone loves Twitter. Everyone loves MooTools. That’s why everyone should...
  4. Ajax Login System Demo A very fast login system. Ajax Login system script does...
  5. XBalloon – AJAX baloon script – AJAX Script XBalloon is DHTML/Ajax script control that allows developers to provide...

Do you like this post?

Email:     

Tags: , , , , , ,

1 Comment »

  1. avatar comment-top

    [...] Read this article: Ajax for Unique Usernames [...]

    comment-bottom

RSS feed for comments on this post. TrackBack URL

Leave a comment



Web Design & CSS (Templates) - TOP.ORG