2015-03-19 120 views
1

似乎有可能關於表單和表單驗證的問題。Codeigniter表單驗證將不會運行

我一直有一個問題讓我的表單驗證工作。我創建了一個註冊表單,向用戶詢問各種信息。然後我創建了一個表單,允許用戶編輯他的信息。表單驗證在註冊表單上完美工作,但我無法讓它在其他表單上運行驗證。

我的觀點

<h1>Edit user</h1> 

<div id="body"> 
     <p>Edit user information.</p> 

    <?php 



    echo form_open('user_admin/user_update'); 

    echo validation_errors(); 

    echo form_hidden('id', $results->id); 

    echo "<p><lable>Email:</lable>"; 
    echo form_input('email', $results->email); 
    echo "</p>"; 


    echo "<p><lable>Name:</lable>"; 
    echo form_input('name', $results->name); 
    echo "</p>"; 

    echo "<p><lable>Last name:</lable>"; 
    echo form_input('lastname', $results->lastname); 
    echo "</p>"; 

    echo "<p><lable>Home address:</lable>"; 
    echo form_textarea('homeaddress', $results->homeaddress); 
    echo "</p>"; 

    echo "<p><lable>Postal address:</lable>"; 
    echo form_textarea('posteladdress', $results->posteladdress); 
    echo "</p>"; 

    echo "<p><lable>Mobile number:</lable>"; 
    echo form_input('mobile', $results->mobile); 
    echo "</p>"; 

    echo "<p><lable>Home telephone:</lable>"; 
    echo form_input('hometel', $results->hometel); 
    echo "</p>"; 

    echo "<p><lable>ID number:</lable>"; 
    echo form_input('idnum', $results->idnum); 
    echo "</p>"; 

    echo "<p>"; 
    echo form_submit('edit_submit', 'Update'); 
    echo "</p>"; 

    echo form_close(); 

    ?> 

我控制器

public function user_update() 
{ 
    $this->load->library('form_validation'); 
    $this->load->model('model_users'); 

    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]'); 
    $this->form_validation->set_rules('name', 'Name', 'required|trim'); 
    $this->form_validation->set_rules('lastname', 'Last name', 'required|trim'); 
    $this->form_validation->set_rules('homeaddress', 'Home address', 'required|trim'); 
    $this->form_validation->set_rules('posteladdress', 'Postel address', 'required|trim'); 
    $this->form_validation->set_rules('mobile', 'Mobile number', 'required|trim'); 
    $this->form_validation->set_rules('hometel', 'Home telphone', 'required|trim'); 
    $this->form_validation->set_rules('idnum', 'ID Number', 'required'); 


    $this->form_validation->set_message('is_unique', 'This email address has already been registered. Please try again.'); 

    if ($this->form_validation->run()==true) 
    { 

     $id = $this->input->post('id'); 
     $this->model_users->update_info(); 
     echo "The users details have been updated successfully"; 
     redirect ('user_admin/user_main'); 
    } 
    else 
    { 
     echo "The users details were not updated. Please contact the admistrator!"; 


    } 


} 

隨着當前配置控制器我可以得到數據庫通過改變if語句爲FALSE更新。所以我現在將發佈信息傳遞給數據庫並進行更新。

請有人看看我的代碼告訴我我做錯了什麼。

謝謝。

+0

當您處於編輯模式時,它不起作用?爲什麼你不使用JavaScript進行客戶端驗證? – Karthikeyani 2015-03-19 06:34:21

+0

你可以去更新按鈕點擊其他嗎? – 2015-03-19 06:51:37

+0

是的,它會運行到else語句並打印錯誤消息 – 2015-03-23 09:03:50

回答

0

嘗試從代碼中取出此行,然後重試。

$this->form_validation->set_message('is_unique', 'This email address has already been registered. Please try again.'); 

我認爲這一行應該進入您自己的驗證功能。如果確實有效,那麼只需閱讀codeigniter站點上的自定義表單驗證功能即可。

如果您使用的是HMVC,那麼這可能會在驗證中導致一些自定義函數的問題,但其中有相當簡單的方法,但您必須爲Google提供確切答案。

+0

即使我從代碼中刪除了set_message,它仍然無法驗證仍然無法運行。 – 2015-03-23 09:05:11

+0

你有沒有嘗試把<?php echo validation_errors(); ?>到你的else語句中,看看它是什麼導致了錯誤? – PrestonDocks 2015-03-25 12:20:26

+0

我做了你的建議,並放置'echo validation_errors();'。到else語句中,並返回表單中的錯誤。然而,我想要重新加載表單頁面,然後在頁面重新加載時在表單上方顯示錯誤。我在註冊頁面以這種方式工作。 – 2015-04-06 07:55:48