我曾尝试从Codegniter的文档中复制,但我无法使表单验证回调起作用。
我添加了帮助表单,url和库form_validation。它不起作用并且总是返回“false”
控制器
public function addtest()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if ($this->form_validation->run() == TRUE)
{
die('true');
}
else
{
die('false');
}
}
$this->template
->build('myform',array());
}
public function username_check($str)
{
if ($str == 'test')
{
return TRUE;
}
else
{
return FALSE;
}
}
查看
<form method="post" action="" class="form-horizontal form-label-left">
<div class="col-xs-12 col-md-9">
<div class="x_panel">
<div class="form-group col-xs-12">
<div class="col-xs-3">
<label class="control-label">Folder name</label>
</div>
<div class="col-xs-9">
<input type="text" name="username" value="" class="form-control " id="" placeholder="">
</div>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
答案 0 :(得分:1)
在Libraries.php中扩展form_validation库
<强> MY_Form_validation.php 强>
SELECT
AVATAR.NAME
FROM
AVATAR
INNER JOIN
PLAYERAVATAR ON PLAYERAVATAR.AVATAR_ID = AVATAR.AVATAR_ID
INNER JOIN
PLAYER ON PLAYER.PLAYER_ID = PLAYERAVATAR.PLAYER_ID
WHERE
PLAYER.ACTIVE = 1
AND
PLAYER.EMAIL LIKE 'a%' ;
答案 1 :(得分:0)
我的控制器功能就像这样,它运行得很好。我已经自动加载了所有库
public function change_password()
{
if($this->isLoggedin()){
$data['title']='Change Password';
if($_POST)
{
$config=array(
array(
'field' => 'old_password',
'label' => 'Old Password',
'rules' => 'trim|required|callback_checkPassword'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required'
),
array(
'field' => 'conf_password',
'label' => 'Confirm Password',
'rules' => 'trim|required|matches[password]'
)
);
$this->form_validation->set_rules($config);
if ($this->form_validation->run() == false)
{
// if validation has errors, save those errors in variable and send it to view
$data['errors'] = validation_errors();
$this->load->view('change_password',$data);
}
else
{
// if validation passes, check for user credentials from database
$this->Login_model->updatePassword($_POST['password'],$this->session->userdata['id']);
$this->session->set_flashdata('log_success','Congratulations! Password Changed');
redirect(base_url() . 'Login/dashboard');
}
}
else
{
$this->load->view('change_password',$data);
}
}
else
{
redirect(base_url().'Login');
}
}
public function checkPassword($str)
{
$check=$this->Login_model->checkPassword($str);
if($check)
{
return true;
}
else
{
$this->form_validation->set_message('checkPassword', 'The Current Password you have provided is incorrect');
return false;
}
}
答案 2 :(得分:0)
在HTML(添加ID字段)
中<input type="text" name="username" value="" class="form-control " id="username" placeholder="">
<button type="submit" class="btn btn-success" id="submit">Submit</button>
在您的AJAX代码中
<script type="text/javascript">
$(function(){
$( "#submit" ).click(function(event)
{
event.preventDefault();
var username= $("#username").username();
$.ajax(
{
type:"post",
url: "<?php echo base_url(); ?>index.php/controller/Method",
data:{ username:username},
success:function(res)
{
}
});
});
});
</script>>
在控制器中
无需检查
if($_SERVER['REQUEST_METHOD'] == 'POST')
cz它总是来自
public function addtest()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
if ($this->form_validation->run() == TRUE)
{
die('true');
}
else
{
die('false');
}
$this->template->build('myform',array());
}
public function username_check($str)
{
if (empty($str))
{
echo "Empty";
}
else
{
if ($str == 'test')
{
return TRUE;
}
else
{
return FALSE;
}
}
}
的更多信息