简体   繁体   中英

Codeigniter reformating POST data after form validation

I have a problem and I can't imagine how this is going on. So I run form_validation to validate my form inputs. After that, $_POST['user_name'] becomes array instead of string.

$this->form_validation->set_rules('user_name', 'Vartotojo vardas',
        'trim|required|min_length[5]|max_length[30]|alpha_dash|callback_checkUserNameUnique');
    $this->form_validation->set_rules('email', 'El. pašto adresas',
        'trim|required|valid_email|callback_checkEmailUnique');
    $this->form_validation->set_rules('password', 'Slaptažodis',
        'trim|required|min_length[5]|max_length[60]');
    $this->form_validation->set_rules('password_2', 'Slaptažodžio pakartojimas',
        'trim|required|min_length[5]|max_length[60]|matches[password]');
    $this->form_validation->set_rules('phone_number', 'Telefono numeris',
        'trim|required|callback_checkIfPhoneGood');

    $this->setFormMessages();


    if ( $this->form_validation->run() == false ) {
        $data['json'] = array(
            'failed' => true,
            'errors' => $this->form_validation->error_array()
        );
    } else {
        print_r($_POST['user_name']);
        print_r($this->input->post('user_name', true));
    }

before launching $this->form_validation->run() and printing $_POST['user_name'] it returns string, after $this->form_validation->run() it returns empty array.

Any ideas?

EDIT:

my checkUserNameUnique method:

function checkUserNameUnique($userName)
{
    return $this->cache->model('system_model', '_getCustomTableData', array(
        'users', array(array('user_name' => strtolower($userName))), 'id DESC', FALSE, 1
    ), CACHE_USER_CHECK_INFO_TIME);
}

_getCustomTableData returns an array, so change your callback function like this:

function checkUserNameUnique($userName)
{
    if (empty($this->cache->model('system_model', '_getCustomTableData', array(
        'users', array(array('user_name' => strtolower($userName))), 'id DESC', FALSE, 1
        ), CACHE_USER_CHECK_INFO_TIME)))
    {
        return TRUE;
    }
    else
    {
        $this->form_validation->set_message('username_unique', 'The %s field must be unique.');
        return FALSE;
    }    
}

Form validation also supports checking uniqueness:

$this->form_validation->set_rules('user_name', 'Vartotojo vardas',
    'trim|required|min_length[5]|max_length[30]|alpha_dash|is_unique[users.user_name]');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM