2017-07-08 1 views
-1

Könnte jemand bitte einen Blick auf meinen Code und sagen Sie mir, was ich falsch mache!Meine eigenen Validierungsmethoden (CodeIgniter) gibt immer einen Fehler zurück, auch wenn ich denke, dass es nicht

hier ist meine Ansicht Datei (HTML-Formular)

<?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()?> 

hier mein Controller ist (Formular-Validierung) File->/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; 
    } 
} 

}

Ich habe versucht, so viele Variationen von Daten, aber es gibt immer den Fehler Ich bin schon frustriert Ich sehe nicht das Problem mit diesem Feld (dob), bitte helfen!

+0

Was ist Ihre genaue Frage? Welches Problem hast du und was ist dein erwartetes Ergebnis? – Utkanos

+0

Das Datum gibt immer einen Fehler, dass das Format ungültig ist, egal was, ich habe sogar versucht, in der Callback-Funktion True zurückzugeben, aber immer noch den gleichen Fehler –

+0

Haben Sie Ihren Callback und Ihre Methode tarare unabhängig voneinander um sicherzustellen, dass sie sind tun, was Sie erwarten, wenn Sie ihnen Testwerte übergeben? – TimBrownlaw

Antwort

0

Den Code nehmen, den Sie zur Verfügung gestellt haben und damit spielen. Ich habe folgendes bekommen:

Ich habe ein paar kleine Änderungen vorgenommen. Finde die Unterschiede!

The View - 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(); ?> 

The Controller - 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; 
     } 
    } 
} 

So habe ich Ihren Code zu meinem welcome.php Controller hinzugefügt.

  1. ein Prüfverfahren Erstellt Ihre Datumsformate /welcome/test testen Sie Ihren Rückrufcode und die tarehe Methode zu testen ... die Formate und Werte zu testen ...

  2. Ich habe Die Formularmethode, um Ihr Formular für dob mit dem Rückruf zu testen.
    willkommen/form

Sie ernsthaft auf den Code in Ihrem $ this-> form_validation_> run() Abschnitt des Controllers suchen müssen ... Warum die gleiche Code für die Wahr und Falsch Bedingungen und was macht dieser Code?

Ist das Ihr echtes Problem?

+0

All dies ist völlig richtig, danke, aber mein Problem beginnt und endet in diesem $ format_validation_ run() 'weil die gleiche Funktion' date_valid() 'mit dem gleichen Argument zwei verschiedene Ergebnisse zurückgibt, wenn ich es rufe wie es und wenn es aufgerufen wird in der $ this-> form_validation_> run() ', zu dem es einen Fehler –

+0

Ich verstehe immer noch nicht, was ich falsch gemacht habe! –

+0

und wenn es dir nichts ausmacht, frage ich, was genau meinst du, wenn du sagst "Warum der gleiche Code für die Wahre und Falsche Bedingungen?" –

Verwandte Themen