dongnai1876 2017-07-08 16:21
浏览 34
已采纳

我自己的验证方法(CodeIgniter)总是会返回错误,即使我认为它不应该

Could someone please take a look at my code and tell me what am doing wrong!

here is my view file ( html form )

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

here is my controller ( form validation ) 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;
    }
}

}

I tried so many variations of dates but it always gives the error i'm frustrated already i don't see the problem with that field ( dob ), please help!

  • 写回答

1条回答 默认 最新

  • dswu26846 2017-07-08 18:12
    关注

    Taking the code you have provided and having a play with it. I got the following...

    I have made some slight modifications. Spot the differences!

    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 I have added your code to my welcome.php controller.

    1. Created a test method to test your date formats /welcome/test to test your callback code and the tarehe method... to test the formats and values...

    2. I have the form method to test your form for dob with the callback.
      welcome/form

    You seriously need to look at the code in your $this->form_validation_>run() section of your controller... Why the same code for both the True and False conditions and what does that code do?

    Is that your real problem?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi