消息:未定义属性:CI_Form_validation :: $ kilometer_error

时间:2013-07-09 14:48:53

标签: php forms codeigniter validation

我对这个问题感到困惑。请有人教我如何做对。

这是我在控制器中的验证规则和验证字段:

function _set_fields(){
    $fields['id'] = 'id';
    $fields['tanggal'] = 'tanggal';
    $fields['kilometer'] = 'kilometer';
    $fields['merek_oli'] = 'merek_oli';
    $fields['harga_oli'] = 'harga_oli';

    //$this->form_validation->set_fields($fields);
}

// validation rules
function _set_rules(){
    //$rules['tanggal'] = 'trim|required|callback_valid_date';
    //$rules['kilometer'] = 'trim|required';
    //$rules['merek_oli'] = 'trim|required';
    //$rules['harga_oli'] = 'trim|required';

    //$this->form_validation->set_rules($rules);
    $this->form_validation->set_rules('tanggal', 'Tanggal', 'trim|required|callback_valid_date');
    $this->form_validation->set_rules('kilometer', 'Kilometer', 'trim|required');
    $this->form_validation->set_rules('merekoli', 'Merek Oli', 'trim|required');
    $this->form_validation->set_rules('hargaoli', 'Harga Oli', 'trim|required');

    $this->form_validation->set_message('required', '* required');
    $this->form_validation->set_message('isset', '* required');
    $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
}

这是我的观点:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Database Service Ganti Oli</title>

<link href="<?php echo base_url(); ?>style/style.css" rel="stylesheet" type="text/css" />

<link href="<?php echo base_url(); ?>style/calendar.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<?php echo base_url(); ?>script/calendar.js"></script>

</head>
<body>
    <div class="content">
        <h1><?php echo $title; ?></h1>
        <?php echo $message; ?>
        <form method="post" action="<?php echo $action; ?>">
        <div class="data">
        <table>
            <tr>
                <td width="30%">ID</td>
                <td><input type="text" name="id" disabled="disable" class="text" value="<?php echo $this->form_validation->id; ?>"/></td>
                <input type="hidden" name="id" value="<?php echo $this->form_validation->id; ?>"/>
            </tr>
            <tr>
                <td valign="top">Tanggal ganti oli (dd-mm-yyyy)<span style="color:red;">*</span></td>
                <td><input type="text" name="tanggal" onclick="displayDatePicker('tanggal');" class="text" value="<?php echo $this->form_validation->tanggal; ?>"/>
                <a href="javascript:void(0);" onclick="displayDatePicker('tanggal');"><img src="<?php echo base_url(); ?>style/images/calendar.png" alt="calendar" border="0"></a>
                <?php echo $this->form_validation->tanggal_error; ?></td>
            </tr>
            <tr>
                <td valign="top">Kilometer<span style="color:red;">*</span></td>
                <td><input type="text" name="kilometer" class="text" value="<?php echo $this->form_validation->kilometer; ?>"/>
                <?php echo $this->form_validation->kilometer_error; ?></td>
            </tr>
            <tr>
                <td valign="top">Merek Oli<span style="color:red;">*</span></td>
                <td><input type="text" name="merekoli" class="text" value="<?php echo $this->form_validation->merek_oli; ?>"/>
                <?php echo $this->form_validation->merekoli_error; ?></td>
            </tr>
            <tr>
                <td valign="top">Harga Oli<span style="color:red;">*</span></td>
                <td><input type="text" name="hargaoli" class="text" value="<?php echo $this->form_validation->harga_oli; ?>"/>
                <?php echo $this->form_validation->hargaoli_error; ?></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" value="Save"/></td>
            </tr>
        </table>
        </div>
        </form>
        <br />
        <?php echo $link_back; ?>
    </div>
</body>
</html>

这是我的表单验证:

类MY_Form_validation扩展了CI_Form_validation {

function MY_Form_validation($config = array())
{
    parent::CI_Form_validation($config);
}

// date_validation callback
function valid_date($str)
{
    if(!ereg("^(0[1-9]|1[0-9]|2[0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})$", $str))
    {
        $this->form_validation->set_message('valid_date', 'date format is not valid. dd-mm-yyyy');
        return false;
    }
    else
    {
        return true;
    }
}

}

0 个答案:

没有答案