[英]Codeigniter reformating POST data after form validation
我有一個問題,我無法想象這是怎么回事。 因此,我運行form_validation來驗證表單輸入。 之后, $_POST['user_name']
變為數組而不是字符串。
$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));
}
在啟動$this->form_validation->run()
並打印$_POST['user_name']
它返回字符串,在$this->form_validation->run()
它返回空數組。
有任何想法嗎?
編輯:
我的checkUserNameUnique方法:
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
返回一個數組,因此更改您的回調函數,如下所示:
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;
}
}
表單驗證還支持檢查唯一性:
$this->form_validation->set_rules('user_name', 'Vartotojo vardas',
'trim|required|min_length[5]|max_length[30]|alpha_dash|is_unique[users.user_name]');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.