2017-07-08 23 views
-1

有人可以看看我的代碼,並告訴我什麼是做錯了!我自己的驗證方法(CodeIgniter)總是返回一個錯誤,即使我認爲它不應該

這是我的視圖文件(HTML形式)

<?php $attrib = array('class'=>'form-horizontal col-lg-6')?> 

        <?php 
         if(isset($_POST['save'])) 
         { 
          echo validation_errors('<div class="alert alert-danger">', '</div>'); 
         } 
        ?> 
        <?=form_open('basic_info/edit_basic', $attrib)?> 

        <div class="form-group"> 
         <div class="input-group"> 
         <div class="input-group-addon" id="label">First Name</div> 
         <input type="text" name="fname" class="form-control" value="<?=$fname?>"> 
         </div> 
        </div> 

        <div class="form-group"> 
         <div class="input-group"> 
         <div class="input-group-addon" id="label">Middle Name</div> 
         <input type="text" name="mname" class="form-control" value="<?=$mname?>"> 
         </div> 
        </div> 

        <div class="form-group"> 
         <div class="input-group"> 
          <div class="input-group-addon" id="label">Last Name</div> 
          <input type="text" name="lname" class="form-control" value="<?=$lname?>"> 
         </div> 
        </div> 

        <div class="form-group"> 
         <div class="input-group"> 
          <div class="input-group-addon"id="label">Date of birth</div> 
          <input type="text" name="dob" class="form-control" value="<?=$dob?>"> 
         </div> 
        </div> 

        <div class="form-group"> 
         <div class="input-group"> 
          <div class="input-group-addon" id="label">Gender</div> 
          <select name="gender" class="form-control"> 
           <?php if($gender == true){ ?> 
           <option class="form-control" value="male" selected="selected">Male</option> 
           <option class="form-control" valu="female">Female</option> 
           <?php }else{ ?> 
           <option class="form-control" valu="female" selected="selected">Female</option> 
           <option class="form-control" value="male">Male</option> 
           <?php } ?> 
          </select> 
         </div> 
        </div> 

        <div class="form-group"> 
         <div class=""> 
          <button type="submit" name="save" class="btn btn-primary">Save</button> 
         </div> 
         </div> 
        <?=form_close()?> 

這是我的控制器(形式驗證)文件 - >/basic_info/edit_basic

defined('BASEPATH') OR exit('No direct script access allowed'); 
class Basic_info extends MX_Controller { 

public function index() 
{ 
    if($this->session->userdata('is_logged')) 
    { 
     $this->load->model('basic_info/edit_account'); 

     $data = $this->edit_account->get_account_info(); 

     $data->dob = date("d-m-Y", strtotime($data->dob)); 

     $this->load->view('basic_info', $data); 

    }else{ 
     redirect('welcome'); 
    } 
} 

public function edit_basic() 
{ 
    $config = array(
     array(
      'field'=>'fname', 
      'label'=>'First name', 
      'rules'=>'alpha|min_length[2]|max_length[150]', 
      'errors'=>array(
       'alpha'=>'Sorry a name can only have alphabets', 
       'min_length'=>'Sorry a name can\'t be this short', 
       'max_length'=>'Sorry a name can\'t be this logng' 
      ), 
     ), 

     array(
      'field'=>'mname', 
      'label'=>'Middle name', 
      'rules'=>'alpha|min_length[2]|max_length[150]', 
      'errors'=>array(
       'alpha'=>'Sorry a name can only have alphabets', 
       'min_length'=>'Sorry a name can\'t be this short', 
       'max_length'=>'Sorry a name can\'t be this logng' 
      ), 
     ), 

     array(
      'field'=>'lname', 
      'label'=>'Last name', 
      'rules'=>'alpha|min_length[2]|max_length[150]', 
      'errors'=>array(
       'alpha'=>'Sorry a name can only have alphabets', 
       'min_length'=>'Sorry a name can\'t be this short', 
       'max_length'=>'Sorry a name can\'t be this logng' 
      ), 
     ), 

     array(
      'field'=>'dob', 
      'label'=>'Date of birth', 
      'rules'=>'exact_length[10]|callback_date_valid', 
      'errors'=>array(
       'exact_length'=>'Sorry! invalid length for a date', 
       'date_valid'=>'Sorry! invalid date format', 
      ), 
     ), 

     array(
      'field'=>'gender', 
      'label'=>'Gender', 
      'rules'=>'alpha|max_length[6]|min_length[4]', 
      'errors'=>array(
       'alpha'=>'Sorry! Invalid gener', 
      ), 
     ), 
    ); 

    $this->form_validation->set_rules($config); 

    if($this->form_validation->run() == FALSE) 
    {    
     $this->load->model('basic_info/edit_account'); 

     $data = $this->edit_account->get_account_info(); 

     $this->load->view('basic_info', $data); 

    }else{ 

     $this->load->model('basic_info/edit_account'); 
     $this->edit_account->edit_basic(); 

     $this->load->model('basic_info/edit_account'); 

     $data = $this->edit_account->get_account_info(); 

     $this->load->view('basic_info', $data); 
    } 
} 

public function date_valid() 
{ 
    $date = $this->input->post('dob'); 

    if(strpos($date, '/') != FALSE ) 
    { 
     list($day, $month, $year) = explode("/",$date); 

     if($this->tarehe($day, $month, $year)) 
     { 
      return true; 
     }else{ 
      return false; 
     } 

    }elseif(strpos($date, '-') != FALSE){ 

     list($day, $month, $year) = explode("-",$date); 

     if($this->tarehe($day, $month, $year)) 
     { 
      return true; 
     }else{ 
      return false; 
     } 
    } 
    elseif(strpos($date, '.') != FALSE) 
    { 

     list($day, $month, $year) = explode(".",$date); 

     if($this->tarehe($day, $month, $year)) 
     { 
      return true; 
     }else{ 
      return false; 
     } 
    }else{ 
     return false; 
    } 
} 

public function tarehe($day, $month, $year) 
{ 
    $first = $day[0]; 
    $second = $day[1]; 

    if($first < 0){ 
     $day = $second; 
    } 

    if($day > 0 && $day <= 31) 
    { 

     $first = $month[0]; 
     $second = $month[1]; 

     if($first < 0){ 
      $month = $second; 
     } 
     if($month > 0 && $month <= 12 ) 
     { 
      $limit_down = date('Y') - 17; 
      $limit_up = date('Y') - 50; 

      if($year < $limit_down && $year > $limit_up) 
      { 
       return true; 
      }else{ 
       return false; 
      } 
     }else{ 
      return false; 
     } 
    }else{ 
     return false; 
    } 
} 

}

我試圖這樣日期的許多變化,但它總是給我錯誤我很沮喪,我沒有看到該領域的問題(dob),請幫助!

+0

你的確切問題是什麼?你遇到什麼問題,你的預期結果是什麼? – Utkanos

+0

日期總是返回一個格式無效的錯誤,無論如何,我甚至試圖在回調函數中返回true,但仍然得到相同的錯誤 –

+0

您是否調試了回調函數,並且您的方法是獨立的,以確保它們是當你通過測試值時做你期望的事情? – TimBrownlaw

回答

0

以您提供的代碼和玩過的代碼。我得到以下...

我做了一些細微的修改。找出差異!

觀 - test_form.php

<?php $attrib = array('class' => 'form-horizontal col-lg-6') ?> 

<?php 
if (isset($_POST['save'])) { 
    echo validation_errors('<div class="alert alert-danger">', '</div>'); 
} 
?> 
<?= form_open('welcome/form', $attrib); ?> 
    <div class="form-group"> 
     <div class="input-group"> 
      <div class="input-group-addon" id="label">Date of birth</div> 
      <input type="text" name="dob" class="form-control" value="<?= isset($dob)?$dob:'' ?>"> 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class=""> 
      <button type="submit" name="save" class="btn btn-primary">Save</button> 
     </div> 
    </div> 
<?= form_close(); ?> 

的控制器 - 的welcome.php

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

/** 
* Class Welcome 
* 
* 
* @property CI_Form_validation form_validation 
* @property CI_Input   input 
*/ 
class Welcome extends CI_Controller { 

    public function __construct() { 
     parent::__construct(); 
     $this->load->helper('form'); 
     $this->load->library('form_validation'); 
    } 

    public function test() { 
     $date = '2017/12/01'; 
     echo $date; 
     echo '<br>'; 
     var_dump($this->date_valid($date)); // returns false 

     $date = '01/03/1970'; 
     echo $date; 
     echo '<br>'; 
     var_dump($this->date_valid($date)); // returns true 

     $date = '01-03-1970'; 
     echo $date; 
     echo '<br>'; 
     var_dump($this->date_valid($date)); // returns true 

    } 

    public function form() { 
     $config = array(
      array(
       'field' => 'dob', 
       'label' => 'Date of birth', 
       'rules' => 'exact_length[10]|callback_date_valid', 
       'errors' => array(
        'exact_length' => 'Sorry! invalid length for a date', 
        'date_valid' => 'Sorry! invalid date format', 
       ) 
      ) 
     ); 

     $this->form_validation->set_rules($config); 
     if ($this->input->post('save') !== NULL) { 
      if ($this->form_validation->run() == TRUE) { 
       echo "Updating the Database"; 
      } 
     } 

     $this->load->view('test_form'); 
    } 

    public function date_valid($date) { 
     if (strpos($date, '/') != FALSE) { 
      list($day, $month, $year) = explode("/", $date); 
      if ($this->tarehe($day, $month, $year)) { 
       return TRUE; 
      } else { 
       return FALSE; 
      } 

     } elseif (strpos($date, '-') != FALSE) { 
      list($day, $month, $year) = explode("-", $date); 
      if ($this->tarehe($day, $month, $year)) { 
       return TRUE; 
      } else { 
       return FALSE; 
      } 
     } elseif (strpos($date, '.') != FALSE) { 

      list($day, $month, $year) = explode(".", $date); 

      if ($this->tarehe($day, $month, $year)) { 
       return TRUE; 
      } else { 
       return FALSE; 
      } 
     } else { 
      return FALSE; 
     } 
    } 

    public function tarehe($day, $month, $year) { 

     $first = $day[0]; 
     $second = $day[1]; 

     if ($first < 0) { 
      $day = $second; 
     } 

     if ($day > 0 && $day <= 31) { 
      $first = $month[0]; 
      $second = $month[1]; 

      if ($first < 0) { 
       $month = $second; 
      } 

      if ($month > 0 && $month <= 12) { 
       $limit_down = date('Y') - 17; 
       $limit_up = date('Y') - 50; 
       if ($year < $limit_down && $year > $limit_up) { 

        return TRUE; 
       } else { 

        return FALSE; 
       } 
      } else { 

       return FALSE; 
      } 
     } else { 

      return FALSE; 
     } 
    } 
} 

所以我加入你的代碼,我的welcome.php控制器。

  1. 創建了一個測試方法來測試您的日期格式/首頁/測試測試你的回調代碼和tarehe方法...測試格式和值...

  2. 我有表單方法來測試你的表單與回調dob。
    歡迎/形式

你真的需要看看代碼在你的控制你的$這個 - > form_validation_> run()的部分...爲什麼兩者的真假相同的代碼條件和該代碼做什麼?

這是你真正的問題嗎?

+0

所有這些都是完全正確的謝謝你,但我的問題開始和結束在'$ this-> form_validation_> run()'因爲具有相同參數的同一個函數'date_valid()'返回兩個不同的結果,當我打電話給它時因爲它和它在'$ this-> form_validation_> run()'中調用時會導致錯誤 –

+0

我仍然不明白我做錯了什麼! –

+0

如果你不介意我問,當你說'爲什麼同樣的代碼適用於True和False條件'時,你究竟是什麼意思' –

相關問題