0

我想验证 startdate 和 endate codeigniter,如果 stardate > enddate,不保存在我的数据库中,或者像这样显示错误消息我的控制器

function buat_lagi() {
    if ($this->input->post('IDKategori')) {
        $this->MKalender->addEvents();
        $id = $_POST[IDKategori];
        $this->session->set_flashdata('message', 'Agenda Pimpinan baru telah dibuat !');
        redirect('admin/kalenderkategori/lihat/' . $id . '', 'refresh');
    } else {
        $data['title'] = "Tambah Agenda Pimpinan";
        $data['kategori'] = $this->MKalenderKategori->getKategoriDropDown();
        $data['main'] = 'admin/kalender/kalender_buat_lagi';
        $this->load->vars($data);
        $this->load->view('layout/dashboard');
    }
}

我的模型是这样的

function addEvents() {
    $data = array(
        'IDKategori' => $this->input->post('IDKategori'),
        'TanggalMulai' => $this->input->post('TanggalMulai'),
        'TanggalAkhir' => $this->input->post('TanggalAkhir'),
        'judul' => $this->input->post('judul'),
        'konten' => $this->input->post('konten'),
        'create_by' => $_SESSION['username'],
    );
    $this->db->insert('kalender', $data);
}

我的表格是这样的

<form action="<?= base_url(); ?>index.php/admin/kalender/buat_lagi/" method="post" enctype="multipart/form-data" name="form" id="form">
<?php
echo "<label for='ptitle'>Kegiatan / Lokasi :</label><br/>";
$data = array('class' => 'validate[required] text-input', 'name' => 'judul', 'id' => 'ptitle', 'size' => 80);
echo form_input($data);

echo "<p><label for='long'>Uraian Kegiatan / Keterangan / Catatan :</label><br/>";
$data = array('class' => 'validate[required] text-input', 'name' => 'konten', 'rows' => '13', 'cols' => '60', 'style' => 'width: 60%');
echo form_textarea($data) . "</p>";

echo "<p><label for='ptitle'>Waktu Mulai :</label><br/>";
$data = array('class' => 'validate[required] text-input', 'name' => 'TanggalMulai', 'id' => 'basic_example_1');
echo form_input($data) . "</p>";

echo "<p><label for='ptitle'>Waktu Akhir :</label><br/>";
$data = array('class' => 'validate[required] text-input', 'name' => 'TanggalAkhir', 'id' => 'basic_example_2', 'onblur' => 'function compareDate()');
echo form_input($data) . "</p>";

echo form_hidden('IDKategori', $this->uri->segment(4));
echo form_submit('submit', 'Tambah Even');
?>
&nbsp;&nbsp;<input type="button" value="Kembali" onClick="javascript: history.go(-1)" />

如何验证我的问题?

4

1 回答 1

1

您还没有给出日期的格式,甚至没有给出日期字段的格式(请记住,这里的大多数用户主要讲英语)您也没有说明您想要这个检查服务器端还是客户端。通过在您的表单中调用的比较日期函数,我假设您至少需要客户端,但我建议尽管表单验证也需要在服务器端完成。客户端非常适合立即通知用户,但对于实际保护表单服务器端的输入是无用的。

客户端的 jQuery(因为我不确定哪个字段是伪代码:

function compareDate()
    {
var startDate = Date.parse($("#startDate).val()");
var endDate = Date.parse($("#endDate).val()");
if(startDate>endDate)
{
alert("Your start date must be earlier than your end date.");
}
    }

Codeigniter 函数(回调应该可以工作,但我还没有测试过。)您实际上只需要在日期字段之一上运行该函数,您只想返回一个错误,并且返回它无关紧要,因为它正在比较二。

//validation rule
$this->form_validtion->set_rules('endDate', 'End Date','trim|callback_compareDates');


function compareDates()
{
$start = strtotime($this->input->post('startDate'));
$end = strtotime($this->input->post('endDate'));
if($start > $end)
{
    $this->form_validation->set_message('compareDates','Your start date must be earlier than your end date');
    return false;
}
于 2013-01-27T17:12:01.883 回答