I do not know why my callback_insert does not work. It does not seem to get called

117 Views Asked by At

I'm using the Grocery Crud and Ion Auth libraries in Codeigniter. The callback_insert does not get called when I try to create a user. callback_insert and callback_update work

The Chrome console does not display any messages. When I put print_r ($post_array) in the _create_user function the following message appears:

Uncaught SyntaxError: Unexpected token A in JSON at position 0.

I already tried to rename the function and it did not work.

function __construct() {
    parent::__construct();
}

function _render_output($output = null) {
    $data['title'] = 'Gerir utilizadores';
    $output->data = $data;

    $this->load->view('includes/header_crud', (array) $output);
    $this->load->view('crud_view', (array) $output);
    $this->load->view('includes/footer_crud', (array) $output);
}

function gerir() {
    try {
        $crud = new grocery_CRUD();
        $crud->set_table('users');
        $crud->set_subject('Utilizador');

        //FORM LAYOUT
        $crud->unset_export();
        $crud->unset_print();
        $crud->columns('username', 'email', 'active', 'last_login');
        $crud->display_as('username', 'Nome')
                ->display_as('active', 'Estado')
                ->display_as('first_name', 'Primeiro nome')
                ->display_as('last_name', 'Último nome')
                ->display_as('last_login', 'Último login')
                ->display_as('password_confirm', 'Confirmar password');
        $crud->add_fields('first_name', 'last_name', 'email', 'password', 'password_confirm');
        $crud->edit_fields('first_name', 'last_name', 'email');

        //VALIDATION
        $crud->required_fields('first_name', 'last_name', 'email', 'password', 'password_confirm');
        $crud->set_rules('email', 'E-mail', 'required|valid_email');
        $crud->set_rules('password', 'Password', 'required|matches[password_confirm]');

        //FIELD TYPES
        $crud->change_field_type('last_login', 'readonly');
        $crud->change_field_type('last_login', 'readonly');
        $crud->change_field_type('password', 'password');
        $crud->change_field_type('password_confirm', 'password');

        //CALLBACKS
        $crud->callback_insert(array($this, '_create_user'));
        $crud->callback_update(array($this, 'edit_user'));
        $crud->callback_delete(array($this, 'delete_user'));

        //OUTPUT
        $output = $crud->render();
        $this->_render_output($output);
    } catch (Exception $e) {
        show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
    }
}

function _create_user($post_array) {
    try {

        $username = $post_array['first_name'] . ' ' . $post_array['last_name'];
        $password = $post_array['password'];
        $email = $post_array['email'];
        $data = array(
            'first_name' => $post_array['first_name'],
            'last_name' => $post_array['last_name'],
        );

        $this->ion_auth_model->register($username, $password, $email, $data);

        return $this->db->insert_id();
    } catch (Exception $e) {
        show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
    }
}

function delete_user($primary_key) {
    if ($this->ion_auth_model->delete_user($primary_key)) {
        return true;
    } else {
        return false;
    }
}

function edit_user($post_array, $primary_key = null) {
    $username = $post_array['first_name'] . ' ' . $post_array['last_name'];
    $email = $post_array['email'];
    $data = array(
        'username' => $username,
        'email' => $email,
        'first_name' => $post_array['first_name'],
        'last_name' => $post_array['last_name'],
    );

    $this->ion_auth_model->update($primary_key, $data);

    return true;
}

I want to use Ion Auth model to create a user

0

There are 0 best solutions below