2012-12-21 23 views
4

我做了一個使用CI的表單,並有一個本地的form_validation()庫來驗證每個字段的輸入,我使用jQuery post回調輸入來檢查每個字段是有效,如果我希望每個錯誤填充到每個字段旁邊的form_error()而不是validation_errors()CodeIgniter單獨顯示與表單字段旁邊的ajax回調單獨的錯誤

請參考以下:

觀點:

<script> 
$("#btnregister").click(function() { 
    var parameters = $("#reg_form").serialize(); 

    $.post(baseurl+'pages/registration', parameters, function(data) { 
     if(data == "ok") { 
      //show success message 
     }else{ 

      $("#error").html(data); 

     } 
    }, "html"); 
}); 
</script> 


<div id="error"></div> 
<form id="reg_form" method="post"> 
    <p> 
    <label for="reg_username">Username</label><br /> 
    <input type="text" id="reg_username" name="reg_username" value="<?php echo set_value('reg_username'); ?>"> 
    <?php echo form_error('reg_username'); ?> 
    </p> 

    <p> 
    <label for="reg_email">Email</label><br /> 
    <input type="text" id="reg_email" name="reg_email" value="<?php echo set_value('reg_email'); ?>"> 
    <?php echo form_error('reg_email'); ?> 
    </p> 

    <p><input type="button" id="btnregister" value="Register"></p> 
</form> 
</div> 

控制器:

public function registration(){ 
    $this->load->library('form_validation'); 

    $this->form_validation->set_rules('reg_username', 'Username', 'trim|required|min_length[4]|max_length[15]|xss_clean|is_unique[users.username]'); 
    $this->form_validation->set_rules('reg_email', 'Email', 'trim|required|valid_email|is_unique[users.email]'); 


    if($this->form_validation->run() == FALSE){ 
     echo validation_errors(); 

    }else{ 
     // insert to db 
     echo "ok"; 
    } 
} 

感謝您的幫助。

回答

9

你必須建立你自己的錯誤數組。如果我們可以訪問 Form_validation的$_error_array但不幸的是它是protected而且沒有訪問方法。

我要你的控制器改變輸出JSON響應使它更容易些:

public function registration() 
{ 
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('reg_username', 'Username', 'trim|required|min_length[4]|max_length[15]|xss_clean|is_unique[users.username]'); 
    $this->form_validation->set_rules('reg_email', 'Email', 'trim|required|valid_email|is_unique[users.email]'); 
    if ($this->form_validation->run()) 
    { 
     $response['status'] = TRUE; 
    } 
    else 
    { 
     $errors = array(); 
     // Loop through $_POST and get the keys 
     foreach ($this->input->post() as $key => $value) 
     { 
      // Add the error message for this field 
      $errors[$key] = form_error($key); 
     } 
     $response['errors'] = array_filter($errors); // Some might be empty 
     $response['status'] = FALSE; 
    } 
    // You can use the Output class here too 
    header('Content-type: application/json'); 
    exit(json_encode($response)); 
} 

現在你的Ajax成功回調可讀取響應的statuserrors鍵。您可以通過data.errors循環,並添加每一個輸入字段:

$("#reg_form").submit(function() { 
    var form = $(this); 
    $.post(baseurl+'pages/registration', form.serialize(), function(data) { 
     if (data.status == true) { 
      //show success message 
     }else{ 
      $.each(data.errors, function(key, val) { 
       $('[name="'+ key +'"]', form).after(val); 
      });​ 
     } 
    }, "json"); 
}); 

另一個簡單的方法是將表格郵寄到自己,和你的Ajax響應重新加載整個形式 - 這樣的消息,並驗證過濾器將照顧服務器端。

+0

這對我來說真棒..非常感謝你@韋斯利默奇先生.. :) –