2016-10-03 33 views
0
$this->form_validation->set_rules('username', 'User Name', 'required|min_length[4]|is_unique[users.username]'); 
$this->form_validation->set_rules('fname', 'Full Name', 'required|min_length[4]'); 
$this->form_validation->set_rules('user_password', 'Password', 'required|matches[confirm_password]'); 
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required'); 
$this->form_validation->set_rules('cname', 'College Name', 'required'); 
$this->form_validation->set_rules('dob', 'Date of Birth', 'required|callback_valid_date'); 
$this->form_validation->set_rules('addr', 'Address', 'required'); 
$this->form_validation->set_rules('contact', 'Contact', 'required|min_length[10]|max_length[10]'); 
$this->form_validation->set_rules('education', 'Education', 'required'); 
$this->form_validation->set_rules('user_email', 'Email', 'required|valid_email|is_unique[users.email]'); 
if ($this->form_validation->run() == FALSE) 
    { 
     $this->session->set_flashdata('result', validation_errors()); 
     redirect('login/register'); 

    } 
    else 
    { 

     $rr = $this->user->register_user(); 
     $this->session->set_flashdata('result', $rr); 
    redirect('login/register'); 
    } 
} 

基本上使用$ this-> session-> flashdata(「result」);顯示結果,但我希望每個字段單獨驗證我已經嘗試過form_error(「字段名稱」);但它不起作用顯示空的結果如何顯示codeigniter中的每個字段上的錯誤

+0

相反重定向嘗試,如果有任何驗證錯誤加載相同的形式的。 –

回答

0

而不是重定向,如果有任何驗證錯誤嘗試加載相同的形式。

在你的控制器頁面。

function your_function(){ 
     // load form helper 
     $this->load->helper(array('form', 'url')); 
     // load form validation library 
     $this->load->library('form_validation'); 
     // validation rules 
     $this->form_validation->set_rules('username', 'User Name', 'required|min_length[4]|is_unique[users.username]'); 
     $this->form_validation->set_rules('fname', 'Full Name', 'required|min_length[4]'); 
       // check form for validations 
       if ($this->form_validation->run() == FALSE){ 
         // load the same form/view 
         $this->load->view('myform'); 
       } else{ 
        // redirect to success page 
       } 
} 

鑑於頁:可以使用form_error()函數來顯示錯誤消息旁邊每個表單字段

<?php echo form_error('username'); ?> 
<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" /> 

<?php echo form_error('fname'); ?> 
<input type="text" name="fname" value="<?php echo set_value('fname'); ?>" size="50" /> 
+0

謝謝,它的工作原理 –

相關問題