来自配置文件的Codeigniter 3验证规则,仅在一个函数中起作用

时间:2015-12-04 14:26:20

标签: codeigniter validation config

抱歉英语不好。

我试图验证两个功能' dayInsert'和' dayUpdate'。功能' dayInsert'验证很好,但是当我尝试验证' dayUpdate'什么都不会发生,而不是错误只是回声是假的。

配置/ form_validation:

$config = array(
    'insertValidation' => array(
                                array(
                                    'field' => 'day',
                                    'label' => 'Vairavimo diena.',
                                    'rules' => 'required|dayValidation')
                                ),
    'updateValidation' => array(
                                array(
                                    'field' => 'day',
                                    'label' => 'Vairavimo diena.',
                                    'rules' => 'required|dayValidation')
                                )
);

CONTROLER:

public function dayInsert(){
    $this->config->load('form_validation');
    $this->form_validation->set_rules($this->config->item('insertValidation'));
    if($this->form_validation->run() == FALSE){
        echo ' false';
    } else {
        echo 'true';
    }
}

public function dayUpdate(){
    $this->config->load('form_validation');
    $this->form_validation->set_rules($this->config->item('updateValidation'));
    if($this->form_validation->run() == FALSE){
        echo ' false';
    } else {
        echo 'true';
    }
}

form_validation_lang:

$lang['form_validation_dayValidation']  = 'Death to you bi***.';

查看/用户/ dayInsert:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<?php
    $attributes = array('id' => 'dayEditForm');
    /*var_dump($result);
    echo $result['0']['day'];
    */
    $dayId = $this->uri->segment(3);
    echo form_open('form/dayUpdate/'. $dayId, $attributes);
    echo validation_errors();
    echo '
            <table>
                <tr>
                    <td rowspan="4"><input class="form-control text-center height-100"
                                                    name="day"
                                                    value="'. $day .'"
                                                    type="text"
                                                    placeholder="Pvz: 2015-10-12"></td>
                </tr>
                <tr>
                    <td><input class="form-control text-center"
                                                    name="timeStart"
                                                    value="'. $timeStart .'"
                                                    type="text"
                                                    placeholder="Pvz: 43:29"></td>
                    <td><input class="form-control text-center"
                                                    name="milageStart"
                                                    value="'. $milageStart .'"
                                                    type="text"
                                                    placeholder="Pvz: 321922"></td>
                    <td><input class="form-control text-center"
                                                    name="dayTimeStart"
                                                    value="'. $dayTimeStart .'"
                                                    type="text"
                                                    placeholder="Pvz: 2015-11-04 06:45"></td>
                </tr>
                <tr>
                    <td><input class="form-control text-center"
                                                    name="timeEnd"
                                                    value="'. $timeEnd .'"
                                                    type="text"
                                                    placeholder="Pvz: 52:29"></td>
                    <td><input class="form-control text-center"
                                                    name="milageEnd"
                                                    value="'. $milageEnd .'"
                                                    type="text"
                                                    placeholder="Pvz: 324922"></td>
                    <td><input class="form-control text-center"
                                                    name="dayTimeEnd"
                                                    value="'. $dayTimeEnd .'"
                                                    type="text"
                                                    placeholder="Pvz: 2015-11-04 23:45"></td>
                </tr>
                <tr>
                    <td><input class="form-control text-center"
                                                    name="timeResult"
                                                    value="'. $timeResult .'"
                                                    type="text"
                                                    placeholder="Iki vairavimo pabaigos liko" disabled></td>
                    <td><input class="form-control text-center"
                                                    name="milageResult"
                                                    value="'. $milageResult .'"
                                                    type="text"
                                                    placeholder="Šiandien nuvažiuota" disabled></td>
                    <td><input class="form-control text-center"         
                                                    name="dayTimeRusult"
                                                    value="'. $dayTimeResult .'"
                                                    type="text"
                                                    placeholder="Šiandien išdirbta" disabled></td>
                </tr>
                <tr>
                    <input type="hidden" name="dayId" value="'.$id.'">
                    <td colspan="4"><input class="btn btn-primary pull-right mt5" type="submit" name="updateSubmit" value="Siųsti duomenis"></td>
                </tr>
            </table>

    ';
    echo form_close(); 
?>





<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<?php
    $attributes = array('id' => 'postDayDataForm');
    echo form_open('form/dayInsert', $attributes);
    echo validation_errors();
    echo '
            <table>
                <tr>
                    <td rowspan="4"><input class="form-control text-center height-100"
                                                    name="day"
                                                    value="'. $day .'"
                                                    type="text"
                                                    placeholder="Pvz: 2015-10-12"></td>
                </tr>
                <tr>
                    <td><input class="form-control text-center"
                                                    name="timeStart"
                                                    value="'. $timeStart .'"
                                                    type="text"
                                                    placeholder="Pvz: 43:29"></td>
                    <td><input class="form-control text-center"
                                                    name="milageStart"
                                                    value="'. $milageStart .'"
                                                    type="text"
                                                    placeholder="Pvz: 321922"></td>
                    <td><input class="form-control text-center"
                                                    name="dayTimeStart"
                                                    value="'. $dayTimeStart .'"
                                                    type="text"
                                                    placeholder="Pvz: 2015-11-04 06:45"></td>
                </tr>
                <tr>
                    <td><input class="form-control text-center"
                                                    name="timeEnd"
                                                    value="'. $timeEnd .'"
                                                    type="text"
                                                    placeholder="Pvz: 52:29"></td>
                    <td><input class="form-control text-center"
                                                    name="milageEnd"
                                                    value="'. $milageEnd .'"
                                                    type="text"
                                                    placeholder="Pvz: 324922"></td>
                    <td><input class="form-control text-center"
                                                    name="dayTimeEnd"
                                                    value="'. $dayTimeEnd .'"
                                                    type="text"
                                                    placeholder="Pvz: 2015-11-04 23:45"></td>
                </tr>
                <tr>
                    <td><input class="form-control text-center"
                                                    name="timeResult"
                                                    value="'. $timeResult .'"
                                                    type="text"
                                                    placeholder="Iki vairavimo pabaigos liko" disabled></td>
                    <td><input class="form-control text-center"
                                                    name="milageResult"
                                                    value="'. $milageResult .'"
                                                    type="text"
                                                    placeholder="Šiandien nuvažiuota" disabled></td>
                    <td><input class="form-control text-center"         
                                                    name="dayTimeResult"
                                                    value="'. $dayTimeResult .'"
                                                    type="text"
                                                    placeholder="Šiandien išdirbta" disabled></td>
                </tr>
                <tr>
                    <td colspan="5"><input class="btn btn-primary pull-right mt5" type="submit" name="ifSubmit" value="Siųsti duomenis"></td>
                </tr>
            </table>

    ';
    if($status == true){
        echo $title."<br>";
        echo $message."<br>";
    }
    echo form_close(); 
?>

2 个答案:

答案 0 :(得分:0)

您的表单在哪里,您可以在此处发布表单吗? 您的字段为day,两个字段。我不明白。 在codeigniter中

$config = array(
        array(
                'field' => 'username',
                'label' => 'Username',
                'rules' => 'required'
        ),
        array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required',
                'errors' => array(
                        'required' => 'You must provide a %s.',
                ),
        ),
        array(
                'field' => 'passconf',
                'label' => 'Password Confirmation',
                'rules' => 'required'
        ),
        array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required'
        )
);

答案 1 :(得分:0)

public function dayUpdate(){ $this->config->load('form_validation'); $this->form_validation->set_rules($this->config->item('updateValidation')); if($this->form_validation->run() == FALSE){ echo ' false'; } else { echo 'true'; } }

问题是我没有在form_validation

中插入视图
public function dayUpdate(){ $this->config->load('form_validation'); $this->form_validation->set_rules($this->config->item('updateValidation')); if($this->form_validation->run() == FALSE){ $this->load->view('file/file.php') } else { echo 'true'; } }