表格验证码点火器不起作用

时间:2017-06-03 03:18:48

标签: php forms codeigniter validation

我是在codeigniter中使用表单验证的初学者,现在我正在尝试使用表单验证codeigniter库,为什么不执行此操作?我已将$this->load->library('form_validation');加载到控制器的顶部。我的意思是我想要输出,它将错误置于我的输入类型之下。为什么array('required' => 'Error Message on rule2 for this field_name'));中的错误不会出现在我的视图中。仅在控制台的响应中

这是我的完整代码:

查看:

<div class="modal fade large" id="admin_create_acct" role="dialog">
  <div class="modal-dialog"> 
    <div class="modal-content">
      <div class="modal-header  btn-success">
               <button type="button" class="close" data-dismiss="modal">&times;</button>
               <h3 class="modal-title">Create Account</h3>
            </div>
            <div class="modal-body">
<form class="form-horizontal" id="frm_create_user">
                <div class="form-group">
              <label for="userFName" class="control-label col-sm-4">First Name:</label>
              <div class="col-sm-6">
                <input type="text" class="form-control" name="userFName" id="userFName" placeholder="First name">
                 <div class="msg"></div><!-- form-group -->
              </div><!-- col-sm-6 -->
            </div><!-- form-group -->
            <div class="form-group">
              <label for="userMName" class="control-label col-sm-4">Middle Name:</label>
              <div class="col-sm-6">
                <input type="text" class="form-control" name="userMName" id="userMName" placeholder="Middle name">
                <div class="msg"></div><!-- form-group -->
              </div><!-- col-sm-6 -->
            </div><!-- form-group -->
 </form><!-- form-horizontal -->
<button type="button" id="btn_reg" onclick="create_User()" class="btn btn-primary">Create Account</button>

我只是尝试一种输入类型和一些验证错误,现在这是我的控制器代码:

public function reg_user(){

       $this->form_validation->set_rules('userFName', 'userFName', 'trim|required',
    array('required' => 'Error Message on First name ')
    );

    $this->form_validation->set_rules('userMName', 'userMName', 'trim|required',
    array('required' => 'Error Message on Middle Name')
    );

   if ($this->form_validation->run() == FALSE) {
        $result['type'] = 'error';
        $result['message'] = validation_errors();
    }else {
       $result['type'] = 'success';
       $result['message'] = 'Whooos..! Your Succes Message';   
    }

    echo json_encode($result);  
}

我的观点来自ajax:

function create_User() {
   $.ajax({
      url: siteurl+"admin_controls/reg_user",
      type: "POST",
      data: $("#frm_create_user").serialize(),
      dataType: "JSON",
        success: function(data) {
          alert(" User Successfully Added added! ");
          $("#frm_create_user")[0].reset();
        }
   });
}

1 个答案:

答案 0 :(得分:2)

请遵循此方法。 或

如果您只是复制并粘贴此代码,它肯定会起作用,您只需在表单操作中更改控制器名称

    public function reg_user(){

         $this->form_validation->set_rules('userFName', 'userFName', 'trim|required',
array('required' => 'Error Message on First name ')
);

          $this->form_validation->set_rules('userMName', 'userMName', 'trim|required',
array('required' => 'Error Message on Middle Name')
);
            if ($this->form_validation->run() == TRUE) {

                    $result['status'] = true;
                    $result['message'] = "Data inserted successfully.";

            }else {
                $result['status'] = false;

                $result['message'] = $this->form_validation->error_array();
            }
            echo json_encode($result);
    }

现在是Ajax部分

在Ajax中我使用FormData函数而不是使用序列化函数。如果您要上传文件,将会有所帮助。

<script type="text/javascript">
$(document).ready(function() {
    $("#form_data").submit(function(e){
        e.preventDefault();
        var formData = new FormData($("#form_data"));

        $.ajax({
            url : $("#form_data").attr('action'),
            dataType : 'json',
            type : 'POST',
            data : formData,
            contentType : false,
            processData : false,
            success: function(resp) {
                console.log(resp);
                $('.error').html('');
                if(resp.status == false) {
                  $.each(resp.message,function(i,m){
                      $('.'+i).text(m);
                  });
                 }
            }
        });
    });

    return false;
});

查看部分

<a data-toggle="modal" href='#admin_create_acct' class="list_sort">Select<a>
<div class="modal fade large" id="admin_create_acct" role="dialog">
    <div class="modal-dialog"> 
        <div class="modal-content">
            <div class="modal-header  btn-success">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h3 class="modal-title">Create Account</h3>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="<?php echo base_url('Practice/test_user')?>" method="post" id="form_data">
                    <div class="form-group">
                        username : <input type="text"  class="form-control" name="name">
                        <span class="error userFName"></span>
                    </div>
                    <div class="form-group">
                        password : <input type="text" class="form-control" name="userMName">
                        <span class="error userMName"></span>
                    </div>

                    <input type="submit" name="submit" value="submit">
                </form>    
            </div>
        </div>
    </div>
</div>