Codeigniter表单验证以检查输入值是否已更改

时间:2016-03-11 14:01:29

标签: javascript php jquery codeigniter validation

我使用codeigniter 3.0.4并且现在遇到了一个基本问题。我有一个<input type='text'>,其中使用了Jquery生成的默认值。

我的问题,如何在提交表单时检查值是否已更改?

如果现在的值与默认的给定值不同,它将进行回调验证(它实际上是检查电子邮件的可用性)。否则,如果该值仍然与默认给定值相同,则它将跳过回调验证。

这是我的模态

<div class="modal-body" id="myModalBody">
   <form id="contactform" role="form" method="POST" action='<?php echo  base_url('administrator/kategori/editcategory');?>' >
       <div class="form-group">
           <label for="kategori"> Nama Kategori </label>
           <input type="hidden" name="id" class='id' id='id'>
           <input type="text" class="form-control edit-category" name='edit-category' id="edit-category" required autofocus>
       </div>
</div>
<div class="modal-footer">
    <button type="submit" class="btn btn-success confirm-edit"> Edit Data </button>
    </form>
</div>

如您所见,上面的表单将数据提交到administrator/kategori/editkategori

这是我的editkategori()函数

public function editcategory(){
 if($this->input->post()){
 $this->form_validation->set_rules('kategori', 'Kategori Baru', 'required|callback_correctcategory');

     if($this->form_validation->run() == FALSE){
        $this->data['result']   = $this->admincrud->getcategorylist();
        $this->data['categoryname'] = $this->admincrud->fetchcategoryname();
        $this->load->view($this->layout, $this->data);  

     } else {

        $tobesent = array(
           "id" => $this->input->post('id'),
           "kb" => $this->input->post('kategori')
        );

        $result = $this->admincrud->editcategory($tobesent);
        if($result){
          $this->session->set_flashdata('result', 'Kategori Sukses Diubah');
          redirect('administrator/kategori');
        }

     }

 } else {

    redirect('administrator/kategori');

 }

}

这是JQuery函数,当显示上面的模态时自动为相应的文本赋值

$('.toggle-edit-modal').click(function(){

  var id           = $(this).closest('tr').find('td:eq(0)').text();
  var categoryname = $(this).closest('tr').find('td:eq(1)').text();

  $('.id').val(id);
  $('.edit-categoryname').val(categoryname);

})

在验证规则集中,我将回调correctcategory(),这是correctcategory()函数:

public function correctcategory($str){

    $sterilizedkey  = strtoupper(trim(str_replace(" ", "", $str)));
    $tobeshown      = ucfirst(strtolower($sterilizedkey));
    $this->CI->db->select("upper(trim(REPLACE(`CategoryName`, ' ',''))) as `CategoryName`");
    $this->CI->db->from('category');
    $result = $this->CI->db->get()->result_array();
    $data   = array();
    foreach ($result as $key => $value) {
        $data[] = $value['CategoryName'];
    }
    if(in_array($sterilizedkey, $data)){
        $this->set_message('correctcategory', 'The faculty has been registered before');
        return false; 
    } else {
        return true;
    }
}

使用上面的代码,系统将评估通过表单提交的每个值。

当我打开模态时出现问题,并出现具有默认给定值的文本框。如果我直接提交表单但没有更改文本框的值,或者新值和旧值是否完全相同,我如何跳过correctcategory验证?

2 个答案:

答案 0 :(得分:0)

您必须将原始值存储在隐藏文本框中。并使用带有值的文本框进行检查。

例如:

<input type="text" id="orig" value="your email">
<input type="hidden" id="hid" value="your email">

然后在jquery上提交检查,如下所示

var original = $('#orig').val();
var static = $('#hid').val();
if(original == static){
  alert("not changed");
}else{
  alert("changed");
}

注意:此代码仅作为示例,因为您没有共享任何代码

答案 1 :(得分:0)

如果您知道Jquery设置的值,那么控制器也应该知道(猜测这是类别数据库值) 基于默认值已知的假设。 要解决此问题,控制器功能需要知道您为输入设置的默认值。

class Categories extends CI_Controller {

public function edit()
{


   // form submited?
   If ( $this->input->post()) 
   {
       $this->load->model('category_model');

       $error = null;
       $success = false ;

        $id = $this->input->post('id');
        // now you were looking for 'kategori' instead of 'edit-category'
        $posted_cat_name = $this->input->post('edit-category'); 
        // get the db record
        $category = $this->category_model->fetch( array('id' => $id ));

        if( !$category->id)
        {
           $error = 'Category not exist' ;
           // set flash and redirect
            redirect('categories/admin_list');
        }
       // category remain the same
       If( $posted_cat_name ==  $category->name)
       {
          $error = "No changes made" ;
       }
       else if( $this->category_model->update($id , $posted_cat_name) == false)
       {
          $error = 'Error occured updating category' ;
          // get the error and log_message() for debuging
       }
       else
       {
          $success = true ;
          // set flash data and redirect

        }


  }
  // do you need this remaining code? aren't you suppose to redirect back to admin list where the form was posted  ?
   $this->data['result']   = $this->admincrud->getcategorylist();
   $this->data['categoryname'] = $this->admincrud->fetchcategoryname();
  $this->load->view($this->layout, $this->data);  
  }
 }