我无法在CodeIgniter中运行表单验证。它不会返回任何错误或任何提交到数据库。
当我使用
时 $this->form_validation->set_rules('subemail', 'Email', 'required|valid_email');
我的代码被执行但不是在下面的情况下。为什么呢?
控制器名称Form_sub:
class Form_sub extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->library('form_validation');
#loading model
$this->load->model('form_model');
$this->load->helper(array("form",'url'));
}#EOF CONSTRUCTOR
public function index(){
$this->load->view('form.php');
}//default page
public function subscribe(){
echo "hello1";
$config = array(
'field' => 'email',
'label' => 'email id',
'rules' => 'required|max_length[50]|min_length[5]|valid_email|trim|htmlspecialchars',
'errors'=>[
'required' => '* You must provide an %s',
'max_length' => '* %s should be of maximum 50 characters',
'min_length' => '* %s should be of minimum 5 characters',
'valid_email' => '* You must provide a valid %s'
]
)
$this->form_validation->set_rules($config);
echo "hello2";
if($this->form_validation->run() == false){
$this->load->view('form.php');
echo "hello3";
}
查看名称form.php:
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-8 col-lg-8">
<?php echo form_open('form_sub/subscribe'); ?>
<input name="email" id="" type="text" value="<?php echo set_value('email');?>" class="normal" placeholder="Email Address">
<?php echo form_error('email'); ?>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4 button">
<!-- <input type="submit" id="subsubmit" name="send" class="button" value="Subscribe">-->
<input type="submit" class="button" value="Subscribe">
<?php echo form_close(); ?>
</div>
</div>
型号名称form_model:
class form_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function subscribe_insert($data)
{
if($this->db->insert('subscribe',$data))
{
return true;
}
else
{
echo "something went wrong in the database";
}
}
}