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

时间:2017-07-08 16:21:31

标签: php 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()?>

这是我的控制器(表单验证)文件 - &gt; / 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),请帮忙!

1 个答案:

答案 0 :(得分: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. 创建了一种测试日期格式 / welcome / test 的测试方法来测试你的回调代码和tarehe方法......来测试格式和值......

  2. 我有使用回调测试dob表单的表单方法 的欢迎/形式

  3. 您真的需要查看控制器的$ this-&gt; form_validation_&gt; run()部分中的代码...为什么True和False条件的相同代码以及该代码的作用是什么?

    这是你真正的问题吗?