我在這一個上花了這麼多時間,我想我已經去了代碼盲目。表單驗證在運行時總是返回FALSE,我無法弄清楚原因。Codeigniter表單驗證總是返回false
new_map()函數首先顯示窗體,當提交表單時(當然在map_name字段中設置了值),表單驗證運行返回false,然後重置窗體而不顯示錯誤。我在想什麼?
控制器:
public function new_map()
{
$data['extra_assets']['js'][] = 'app/admin/maps.js';
$data['title'] = 'Add Map';
$this->form_validation->set_rules('map_name', 'Map name', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load_admin_view('admin/ae_map', $data);
}
else
{
$this->load->model('maps_model');
echo 'inside true';exit;
// Upload map
if ($this->maps_model->upload_map())
{
redirect(site_url('admin/maps'));
}
}
}
型號:
public function upload_map()
{
$config['upload_path'] = root_path() . 'space/maps/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$map_name = $this->input->post('map_name');
if (! $this->upload->do_upload('map_file'))
{
$error = array('error' => $this->upload->display_errors());
echo $this->upload->display_errors();exit;
return FALSE;
}
else
{
$data = array('upload_data' => $this->upload->data());
// Insert map into database
$options = array(
'm_file' => $data['upload_data']['file_name'],
'm_name' => $map_name
);
$this->insert_map($options);
// Create a thumbnail of this map
$this->generate_map_thumb($data['upload_data']['file_name']);
}
return TRUE;
}
查看:
<div class="modalContent" style="width: 300px; height: 250px;">
<h1><?php echo $title; ?></h1>
<?php echo validation_errors(); ?>
<div id="MAP_FORM" class="formListing">
<?php echo form_open_multipart('admin/new_map'); ?>
<ul>
<li>
<div><label>File: </label></div>
<div><span><input type="file" id="MAP_FILE" name="map_file" /></span></div>
</li>
<li>
<div><label>Name: </label></div>
<div><span><input type="text" id="MAP_NAME" name="map_name" value="" /></span></div>
</li>
</ul>
<br />
<input type="submit" value="Submit" id="SUBMIT_MAP" name="submit_map" />
<?php echo form_close(); ?>
</div>
爲什麼您的表單驗證發生在模型中?它應該在控制器中。但無論如何,讓我看看實際發生的事情!另外,爲什麼模型中有一個重定向語句。這些陳述是控制器的一部分。 – Aniket
我猜測模型和控制器代碼被標記爲錯誤的方式 - 除非在控制器中有另一個'new_map'函數? – jleft
對不起!事實上他們被貼上了相反的標籤。我會馬上切換標籤。 – PoloRM