• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Twitter-like Registration with DX Auth

#1
[eluser]K.Brown[/eluser]
I'm using DX Auth, and I'd like to to behave like twitter's registration--in the sense that it will check the database via ajax to make sure the username/email doesn't conflict...

Has anybody done this?

If I have to pick a js library, I'd use jQuery!

#2
[eluser]xzela[/eluser]
Actually, I've done something very similar to what you've described. I didn't use the Auth library you supplied but my method should work nearly the same way.

I created a Callback using the form validation library. however, i didn't call it via a typical post request. I called it with a jQuery ajax.post request. If the call back found a match for that username, it would respond with a message.

See jQuery post documentation here:
http://docs.jquery.com/Post

Registration controller:
Code:
$this->load->library('form_validation');
$this->form_validation->set_rules('user_name', 'User Name', 'trim|required|callback_CB_testUserName);
$this->form_validation->set_message('test_username', 'That username is already taken. Please choose another one');
// more code here ...
// ...
$json = array();
$json['validation'] = validation_errors();
echo json_enchode($json);


callback function
Code:
function CB_testUserName($str) {
  $this->load->model('register_model');
   return $this->register_model->testUsername($str);
}

registration model
Code:
function testUsername($name) {
  $b = true;
  $this->db->from('user_table');
  $this->db->where('user_name', $name);
  $query = $this->db->get();
  if($query->num_rows > 0) {
    $b = false;
  }
  return $b;
}

that should do it.

#3
[eluser]K.Brown[/eluser]
Alright. Here's where I’m at:

I have the validation working!

All I need to do is get it jquery-ed up!

#4
[eluser]juliano.ma[/eluser]
Hello,

I am trying to implement this validation in real time on my form, but I must be missing something.

I have installed the JSON.

You could post an full example in the controller and view, please?

Code:
<?php
class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('validation');
        
        
        $rules['username'] = "callback_chk_username";
        
        $this->validation->set_rules($rules);

                
        if ($this->validation->run() == FALSE)
        {    
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }


    
    function chk_username($name)
    {
        $b = true;
        $this->db->from('user');
        $this->db->where('username', $name);
        $query = $this->db->get();
        
        if($query->num_rows > 0)
        {
            $this->validation->set_message('chk_username', 'Not available');
            $b = false;
        }
        return $b;
    }


}
?>

Where i use this below, in controller or view?:

Code:
$json = array();
            $json['validation'] = validation_errors();
            echo json_enchode($json);


Look my view:

Code:
<?=$this->validation->error_string; ?>

<?=form_open('register');?>

<?
$dados = array(
              'class'       => 'username',
              'name'        => 'username',
              'id'          => 'username',
              'value'       => '',
              'size'        => '20',
            );

echo form_input($dados);
?>

<?

$dados_submit = array(
                    'type' => 'submit',
                    'name' => 'submit_user',
                    'value' => 'Send',
                    'id'   =>  'submit',
                    );
                    
echo form_submit($dados_submit);
?>

#5
[eluser]jcavard[/eluser]
DX Auth already has a built-in function to check username availability.
Code:
$result = $this->dx_auth->is_username_available($username); // bool
You can also integrate it in the validation rules

#6
[eluser]juliano.ma[/eluser]
Thanks for the response jcavard, but I would do it in real time, using the onKeyUp javascript event with jquery.

Someone have some example?

#7
[eluser]jcavard[/eluser]
I guess you just bind a ajax call to onKeyUp of the username textbox, then you can a script that execute the is_username_available().


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.