codeigniter扩展表单验证不起作用

时间:2017-07-21 19:49:59

标签: php codeigniter-3

我创建了一个库My_validation并扩展了form_validation。当我提交空白表单时,错误消息未显示。但如果我不扩展则其工作正常。

表单控制器

 <?php

class Form extends CI_Controller {

        public function __construct()
        {
                parent::__construct();
                $this->load->helper(array('form', 'url'));
                $this->load->library(array('form_validation','MY_validation'));
        }

        public function index()
        {
            $this->my_validation->set_rules('first_name', 'First Name', 'required|alpha'); 


                if ($this->my_validation->run() == FALSE)
                {

                    $this->load->view('myform');
                }


        }



}

myform查看

    <link rel="stylesheet" href="<?php echo base_url()?>layout/css/bootstrap.min.css ">

    <div class="container">

    <form class="well form-horizontal" action="<?php echo base_url() ?>form " method="post"  id="contact_form" enctype="multipart/form-data">

<fieldset>

<!-- Form Name -->
<legend><center><h2><b>Registration Form</b></h2></center></legend><br>

<!-- Text input-->
<?php 


echo  validation_errors(); 

?>
<div class="form-group">
  <label class="col-md-4 control-label">First Name</label>  
  <div class="col-md-4 inputGroupContainer">
  <div class="input-group">
  <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
  <input  name="first_name" placeholder="First Name" value="<?php echo set_value('first_name'); ?>" class="form-control"  type="text">
    </div>
  </div>
</div>

<!-- Text input-->

<div class="form-group">
  <label class="col-md-4 control-label" >Last Name</label> 
    <div class="col-md-4 inputGroupContainer">
    <div class="input-group">
  <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
  <input name="last_name" placeholder="Last Name" class="form-control" value="<?php echo set_value('last_name'); ?>"  type="text">
    </div>
  </div>
</div>

  <div class="form-group"> 
  <label class="col-md-4 control-label">User Type</label>
    <div class="col-md-4 selectContainer">
    <div class="input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
    <select name="user_type" class="form-control selectpicker">
      <option value="">Select your Type</option>
      <option <?php echo (set_value('user_type')=='1')?" selected=' selected'":""?> value="1">User</option>
      <option  <?php echo (set_value('user_type')=='2')?" selected=' selected'":""?> value="2">Member</option>
    </select>
  </div>
</div>
</div>

<!-- Text input-->

<div class="form-group">
  <label class="col-md-4 control-label">Username</label>  
  <div class="col-md-4 inputGroupContainer">
  <div class="input-group">
  <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
  <input  name="user_name" placeholder="Username" class="form-control" value="<?php echo set_value('user_name'); ?>"  type="text">
    </div>
  </div>
</div>

<!-- Text input-->

<div class="form-group">
  <label class="col-md-4 control-label" >Password</label> 
    <div class="col-md-4 inputGroupContainer">
    <div class="input-group">
  <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
  <input name="user_password" placeholder="Password" class="form-control"  type="password">
    </div>
  </div>
</div>

<!-- Text input-->

<div class="form-group">
  <label class="col-md-4 control-label" >Confirm Password</label> 
    <div class="col-md-4 inputGroupContainer">
    <div class="input-group">
  <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
  <input name="confirm_password" placeholder="Confirm Password" class="form-control"  type="password">
    </div>
  </div>
</div>

<!-- Text input-->
       <div class="form-group">
  <label class="col-md-4 control-label">E-Mail</label>  
    <div class="col-md-4 inputGroupContainer">
    <div class="input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
  <input name="email" placeholder="E-Mail Address" class="form-control" value="<?php echo set_value('email'); ?>"  type="text">
    </div>
  </div>
</div>


<!-- Text input-->

<div class="form-group">
  <label class="col-md-4 control-label">Contact No.</label>  
    <div class="col-md-4 inputGroupContainer">
    <div class="input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
  <input name="contact_no" placeholder="(639)" class="form-control" value="<?php echo set_value('contact_no'); ?>" type="text">
    </div>
  </div>
</div>

<div class="form-group">
  <label class="col-md-4 control-label">Upload Image.</label>  
    <div class="col-md-4 inputGroupContainer">
    <div class="input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
  <input name="upload_img"  class="form-control" type="file">
    </div>
  </div>
</div>



<!-- Button -->
<div class="form-group">
  <label class="col-md-4 control-label"></label>
  <div class="col-md-4"><br>
    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<button type="submit" class="btn btn-warning" >&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSUBMIT <span class="glyphicon glyphicon-send"></span>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</button>
  </div>
</div>

</fieldset>
</form>
</div>
    </div><!-- /.container -->

My_validation库

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_validation extends CI_Form_validation
{

    public function test()
    {
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

class MY_validation extends CI_Form_validation {

    public function __construct($config = array()){
        parent::__construct($config);
    }

    public function test(){
        return "Hello World!";
    }

}