0

I have added two fields in a registration form of tank auth.Field with name fname and lname. I have added the same in the form_validation as you can see the controller file.But username,password,email is injected into database expect fname and lname.What is wrong in the code?I have checked if the name of the field,they are correct.The form validation is also working,but the database field name is also fname and lname.Can someone figure out the problem.have been sitting on this for a long time but couldnt find them working

The auth.php controller is

if ($use_username) {
                $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->config->item('username_min_length', 'tank_auth').']|max_length['.$this->config->item('username_max_length', 'tank_auth').']|alpha_dash');
            }
            $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length['.$this->config->item('password_min_length', 'tank_auth').']|max_length['.$this->config->item('password_max_length', 'tank_auth').']|alpha_dash');
            $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean|matches[password]');
            $this->form_validation->set_rules('fname', 'First Name', 'trim|required|xss_clean'); // I added
            $this->form_validation->set_rules('lname', 'Last Name', 'trim|required|xss_clean'); //I added


            $captcha_registration   = $this->config->item('captcha_registration', 'tank_auth');
            $use_recaptcha          = $this->config->item('use_recaptcha', 'tank_auth');
            if ($captcha_registration) {
                if ($use_recaptcha) {
                    $this->form_validation->set_rules('recaptcha_response_field', 'Confirmation Code', 'trim|xss_clean|required|callback__check_recaptcha');
                } else {
                    $this->form_validation->set_rules('captcha', 'Confirmation Code', 'trim|xss_clean|required|callback__check_captcha');
                }
            }
            $data['errors'] = array();

            $email_activation = $this->config->item('email_activation', 'tank_auth');

            if ($this->form_validation->run()) {                                // validation ok
                if (!is_null($data = $this->tank_auth->create_user(
                        $use_username ? $this->form_validation->set_value('username') : '',
                        $this->form_validation->set_value('fname'),//I added
                        $this->form_validation->set_value('lname'),
                        $this->form_validation->set_value('email'),
                        $this->form_validation->set_value('password'),
                        $email_activation)))

user.php model

function create_user($data, $activated = TRUE)
    {
        $data['created'] = date('Y-m-d H:i:s');
        $data['activated'] = $activated ? 1 : 0;

        if ($this->db->insert($this->table_name, $data)) {
            $user_id = $this->db->insert_id();
            if ($activated) $this->create_profile($user_id);
            return array('user_id' => $user_id);
        }
        return NULL;
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Piya
  • 1,134
  • 4
  • 22
  • 42

1 Answers1

0

Hello again I made this work by not editing TA model. What I did I let TA do the job and right after create_user() is done I make an update to this newly created_user with additional information I needed (first name, last name, gender...)

So, open up

/libraries/Tank_auth.php

and add this method

/**
 * Update user information on site (user_profile)
 * make sure keys of array match table fields to update
 * @param   integer
 * @param   array
 * @return  NULL
 */

function update_profile($user_id, $data)
{
    $this->ci->users->update_profile($user_id, $data);
    return NULL;
}

scroll to function create_user(...) add as last parameter $user_data so function declaration looks like this

function create_user($username, $email, $password, $email_activation, $user_data) {...}

and edit some code like this (inside create_user method)

if (!is_null($res = $this->ci->users->create_user($data, !$email_activation))) {
        $data['user_id'] = $res['user_id'];
        $data['password'] = $password;
        unset($data['last_ip']);
        $this->update_profile($data['user_id'], $user_data); //this line is important!
        return $data;
}

now save this file and go to models/tank_auth/users.php, create this method

/**
 * Update profile for a user
 *
 * @param   int
 * @param   array
 * @return  bool
 */
function update_profile($user_id, $data)
{

    $this->db->where('id', $user_id);
    $this->db->update($this->profile_table_name, $data);
}

Now go to controllers/auth.php

find register() method

and search for (it is bottom part of your code you posted here)

if ($this->form_validation->run()) {                                // validation ok
                if (!is_null($data = $this->tank_auth->create_user(
                        $use_username ? $this->form_validation->set_value('use...

and change it so it looks like this

if ($this->form_validation->run()) {                                
                $user_data = array(
                    'first_name'    => set_value('fname'),
                    'last_name' => set_value('lname'),
                    ); // <- IMPORTANT
                if (!is_null($this->data = $this->tank_auth->create_user(
                        $use_username ? $this->form_validation->set_value('username') : '',
                        $this->form_validation->set_value('email'),
                        $this->form_validation->set_value('password'),
                        $email_activation, $user_data))) {                                  

                    $this->data['site_name'] = $this->config->item('website_name', 'tank_auth');

                    if ($email_activation) {...
Kyslik
  • 8,217
  • 5
  • 54
  • 87