tags:

views:

75

answers:

1

here's the controller function register()

public function register()
{
     $userdata = array();
     $formdata = array(
      'uname' => '',
      'password' => '',
      'email' => '',
      'fname' => '',
      'lname' => ''
     );

     // copy the form as errors
     $errors = $formdata;

     // process data if form is submitted
     if (isset($_POST['submitted']))
     {
      // initialize validation library
      $post = new Validation($_POST);

      // add filters
      $post->pre_filter('trim', 'uname', 'fname', 'lname');

      // set validation rules
      $post->add_rules('uname', 'required', 'alpha_numeric');
      $post->add_rules('password', 'required', 'length[6, 12]');
      $post->add_rules('email', 'required', 'email');
      $post->add_rules('fname', 'required', 'alpha');
      $post->add_rules('lname', 'required', 'alpha');
      $post->add_callbacks('uname', array($this, ' _username_exists'));

      if($post->validate())
      {
       $userdata = array(
        'username' => $_POST['uname'],
        'email' => $_POST['email'],
        'password' => $_POST['password'],
        'firstname' => $_POST['fname'],
        'lastname' => $_POST['lname']
       );

       $this->user->register($userdata);
       // url::redirect('test/register_success');
      }
      // show form with validation errors
      else
      {
       // repopulate form fields
       $formdata = arr::overwrite($formdata, $post->as_array());

       // populate the error fields
       $errors = arr::overwrite($errors, $post->errors('register_error_messages'));
      }
     }

     // show form
     $view = new View('register');
     $view->form = $formdata;
     $view->errors = $errors;
     $view->render(true);
    }

here's the callback function

public function _username_exists(Validation $post)
    {
     // if add_rules validation found any errors, exit
     if (array_key_exists('uname', $post->errors())) return;

     $result = $this->user->check_username_exists($username);
     if (!$result)
     {
      //add error to validation object
      $post->add_error('uname', 'username_exists');
     }
    }

it works perfectly fine if I comment out the call to the callback funcion

+1  A: 

You have a space in your string of the method name in the add_callback method.

$post->add_callbacks('uname', array($this, ' _username_exists'));

should be

$post->add_callbacks('uname', array($this, '_username_exists'));

I think this will fix things up.

Patrick