2017-08-16 14 views
0

CodeIgniterでフォーム検証を実装しようとしています。私の誤りは何か教えてください。私の問題は、それは常にエラーページにリダイレクトされている、と私はHTMLフォームのすべての詳細を記入した場合でも、検証は動作していない、詳細はデータベースに入力されていないことを意味します。Codeigniterフォームの検証が機能しない

function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('modeldata'); 
    // Load url helper 
    $this->load->helper('url'); 
    $this->load->helper('form'); 
    $this->load->library('form_validation'); 

} 
を次のように私はすでにヘルパーをロードしていた

<!DOCTYPE html> 
<html> 
<head> 
    <title>REGISTRATION</title> 
</head> 
<body> 
<form method="POST" action="<?php echo base_url();?>Test/studentinsert" enctype="multipart/form-data"> 
<label>Name</label> 
<input type="text" name="name" id="name"><br> 
<label>Age</label> 
<input type="number" name="age" id="age"><br> 
<label>Address</label> 
<textarea name="address" id="address"></textarea><br> 
<label>User Name</label> 
<input type="text" name="username" id="username"><br> 
<label>Password</label> 
<input type="Password" name="Password" id="Password"><br> 
<label>Gender</label> 
<input type="radio" name="gender" value="male">Male 
<input type="radio" name="gender" value="female">Female<br> 
<label>Qqualifiction</label> 
<input type="text" name="quali" id="quali"><br> 
<input type="file" name="certificate" id="certificate"><br> 
<input type="submit" name="submit" id="submit"> 

</form> 
<a href="<?php echo base_url()?>Test/login">LOGIN</a> 
</body> 
</html> 

コントローラーページコード

public function studentinsert() 
    { 
     //$this->load->helper(array('form', 'url')); 

     $this->form_validation->set_rules('name', 'Name', 'required'); 
    $this->form_validation->set_rules('age', 'age', 'required'); 
    $this->form_validation->set_rules('address', 'address', 'required'); 
    $this->form_validation->set_rules('username', 'username', 'required'); 
    $this->form_validation->set_rules('password', 'password', 'required'); 
    $this->form_validation->set_rules('gender', 'gender', 'required'); 
    $this->form_validation->set_rules('quali', 'quali', 'required'); 
    $this->form_validation->set_rules('certificate', 'certificate', 'required'); 
     $config['upload_path']   = './uploads/'; 
     $config['allowed_types']  = '*'; 
     //$config['max_size']    = 100; 
     $this->load->library('upload',$config); 
     //$this->load->initialize($config); 
     $this->upload->do_upload('certificate'); 
     $file="uploads/".$this->upload->data('file_name'); 
     $name=$this->input->post('name'); 
     $userid=mt_rand(100000, 999999); 
     $age=$this->input->post('age'); 
     $address=$this->input->post('address'); 
     $gender=$this->input->post('gender'); 
     $username=$this->input->post('username'); 
     $hashpassword=hash('md5',$this->input->post('Password')); 
     $quali=$this->input->post('quali'); 
     if ($this->form_validation->run() == FALSE) 
       { 
         $this->load->view('registration'); 
       } 
     else{ 
     $data=array('file'=>$file,'userid'=>$userid,'name'=>$name,'age'=>$age,'address'=>$address,'gender'=>$gender,'quali'=>$quali,); 
     $logdata=array('userid'=>$userid,'username'=>$username,'Password'=>$hashpassword); 
     $ok=$this->modeldata->insertdb($data); 
     $ok1=$this->modeldata->insertlogin($logdata); 
     if($ok=="success") 
     { 
      $this->load->view('registration'); 
     } 
} 

    } 

ビューページのコード(フォーム、URL、フォーム検証)構造関数のコントローラ内のクラスとコードがあります

+1

フォームの検証ルールはどこですか?魔法はありません。フォーム検証クラスでは、実行するルールが必要です。 –

+0

私も検証ルールを追加しました。しかし、それは動作しません。データはデータベースに入力されません。 –

+0

$ dataの値は、モデルに渡す直前の予想値ですか? Do print_r($ data); $ this-> modeldata-> insertdb($ data);の直前。 –

答えて

1

このコードを変更しました

public function studentinsert(){ 

    $this->form_validation->set_rules([ 
     [ 
      'field' => 'name', 
      'label' => 'Name', 
      'rules' => 'required' 
     ],[ 
      'field' => 'age', 
      'label' => 'age', 
      'rules' => 'required' 
     ],[ 
      'field' => 'username', 
      'label' => 'username', 
      'rules' => 'required' 
     ],[ 
      'field' => 'password', 
      'label' => 'password', 
      'rules' => 'required' 
     ],[ 
      'field' => 'quali', 
      'label' => 'quali', 
      'rules' => 'required' 
     ] 
    ]); 
    $this->load->library('upload',[ 
     'upload_path' => './uploads/', 
     'allowed_types'=> '*' 
    ]); 
    $this->upload->do_upload('certificate'); 
    $file="uploads/".$this->upload->data('file_name'); 
    $userid=mt_rand(100000, 999999); 
    $name=$this->input->post('name'); 
    $userid=mt_rand(100000, 999999); 
    $age=$this->input->post('age'); 
    $address=$this->input->post('address'); 
    $gender=$this->input->post('gender'); 
    $username=$this->input->post('username'); 
    $hashpassword=hash('md5',$this->input->post('Password')); 
    $quali=$this->input->post('quali'); 
    if ($this->form_validation->run() == FALSE){ 
     $this->load->view('registration'); 
    }else{ 
     $data=array('file'=>$file,'userid'=>$userid,'name'=>$name,'age'=>$age,'address'=>$address,'gender'=>$gender,'quali'=>$quali,); 
     $logdata=array('userid'=>$userid,'username'=>$username,'Password'=>$hashpassword); 
     $ok=$this->modeldata->insertdb($data); 
     $ok1=$this->modeldata->insertlogin($logdata); 
     if($ok=="success"){ 
      $this->load->view('registration'); 
     } 
    } 

} 
あなたが certificateとCodeIgniterのために設定検証ルールは(存在するが $_FILES['certificate']ないdoesntが) $_POST['certificate']があると想定してい

$this->form_validation->set_rules('certificate', 'certificate', 'required');ルールを削除し、代わりにisset($_FILES['certificate'])を使用しています。見つからない場合はエラーページにリダイレクトされる可能性があります

+0

彼の問題は実際にはモデルにあり、本当の問題が何であるかを知るために十分なデバッグを行ったことがないので、悪い質問のようなものでした。 –

関連する問題