Codeigniter在表单验证

时间:2016-10-18 16:20:34

标签: php ajax codeigniter validation

我不明白为什么当我在表单上留下空输入时没有错误。我第一次使用表单验证,我真的很困惑如何使用AJAX提交表单验证。

我在form_validation.php路径上使用了一个名为application/config的文件,如codeigniter网页上所述,用于设置应用中每个输入的规则验证。我已经在form_validation上设置了表单助手和autoload.php库。在表单视图中,我尝试通过validation_errors()打印错误,但没有任何反应。

这是我的代码:

FORM

<?php echo form_open(null,array("class"=>"col s12","id"=>"enter_form"));?>

         <div class="row">
               <div class="col s4">
                  <span style="position:static;" class="badge yellow">Importante!!!</span>
                  <blockquote>
                    Si es primera vez que ingresas a votar, debes ingresar un correo y una contraseña, que te identifique como usuario.
                    Si ya te encuentras registrado, ingresa los mismos datos usados la primera vez que ingresaste, para realizar tu inicio de sesion.
                  </blockquote>

               </div>
                 <div class="col s8">
                   <?php

                    $errors= validation_errors('<li>','</li>');
                    if($errors != ""){
                          ?>
                              <div class="red lighten-2">
                                <ul>
                                  <?php echo $errors;?>
                                </ul>
                              </div>
                          <?php
                    }

                   ?>
                        <div class="row">
                           <div class="input-field col s6 offset-s3">
                               <input  id="enter_email" type="email" name="enter_email" class="validate">
                               <label  for="enter_email">Correo</label>
                           </div>
                        </div>
                        <div class="row">
                           <div class="input-field col s6 offset-s3">
                               <input  id="u" name="enter_password" type="password" class="validate">
                               <label  for="enter_password">Contraseña</label>
                           </div>
                        </div>

                       <input type="hidden" name="update_politic_hide" id="update_politic_hdn" value="">


                 </div>
           </div>
           <div class="modal-footer" style="position:fixed !important;">
                <div class="col s6 push-s5">
                   <button class="center-align btn waves-effect waves-light light-blue darken-4" type="submit" name="action" id="enter_submit">A votar!</button>
               </div>
           </div>

         <?php echo form_close();?>

CONTROLLER

public function verification()
        {
          $this->load->model("votes");
            if ($this->input->is_ajax_request()) {

            if ($this->form_validation->run('enter_form')) {
                   //some code
            }
             //else should display validation_errors() on form view   

            $params["email"] = $this->input->post('enter_email');
            $params["password"] = $this->input->post('enter_password');

            $result = $this->votes->verification($params);

            if ($result==null) {
              $params["status"]= 0;
              $success=  $this->votes->user_record($params);
              return $success;
            }
            else {
              $params["status"]= 1;
              $success=  $this->votes->user_record($params);
              return $success;
            }

            }
        }

JAVASCRIPT

$("#enter_form").submit(function(event) {

      event.preventDefault();

      var data = $(this).serialize();
      alert(data);

      $.post(baseurl+"votos/verification",
        data,
       function(data) {

         if (data == 1) {
           alert("registrado");
         }
         else {
           alert("No estabas registrado, ahora si");
         }
      }

    );

    });

FORM_VALIDATION.PHP(输入规则)

'enter_form'
    => array(

        array('field' => 'enter_email','label' => 'Email','rules' => 'required|is_string|valid_email'),
        array('field' => 'enter_password','label' => 'Password','rules' => 'required|is_string'),

    ),

1 个答案:

答案 0 :(得分:0)

这是因为您通过ajax请求发送表单。 提交表单后,此代码不会运行。

 $errors= validation_errors('<li>','</li>');
                if($errors != ""){
                      ?>
                          <div class="red lighten-2">
                            <ul>
                              <?php echo $errors;?>
                            </ul>
                          </div>
                      <?php
                }

您应该在服务器上捕获错误并将其发送回来:

    if ($this->form_validation->run('enter_form') === true) {
        //some code
    }else{
        $errors = validation_errors();
        echo json_encode(array("success"=>false, "message"=>$errors));
        die();
    }

然后在ajax回调中,将消息放入div并显示它们。