我在这个上花了很多时间,我想我已经对代码视而不见了。表单验证在运行时总是返回 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>