我有这个选择菜单:
<select name="category_id" class="form-control ">
<option selected>Select Category</option>
<?php foreach($categories as $category): ?>
<option value="<?php echo $category->id; ?>"><?php echo $category->name; ?></option>
<?php endforeach ; ?>
</select>
我的控制器中有下一个规则:
$this->form_validation->set_rules('category_id','Category','required');
其他输入字段的其他一些规则,它们正在运行,但它忽略了这个下拉select
菜单。
完整表单视图:
<!-- Display Validation errors -->
<?php echo validation_errors('<p class="alert alert-dismissable alert-danger">'); ?>
<form method="post" action="<?php echo base_url() ?>admin/articles/add">
<div class="row">
<div class="col-md-6">
<h1 class="sub-header">Add Article</h1>
</div>
</div>
<ol class="breadcrumb">
<li>
<a href="<?php echo base_url();?>admin/dashboard">
Dashboard
</a>
</li>
<li>
<a href="<?php echo base_url();?>admin/articles">
Articles
</a>
</li>
<li>
Add Article
</li>
</ol>
<div class="form-group">
<label>Article Title</label>
<input type="text" class="form-control" name="title" value="<?php echo set_value('title'); ?>" placeholder="Enter Title" />
</div>
<div class="form-group">
<label>Category</label>
<select name="category_id" class="form-control ">
<option selected>Select Category</option>
<?php foreach($categories as $category): ?>
<option value="<?php echo $category->id; ?>"><?php echo $category->name; ?></option>
<?php endforeach ; ?>
</select>
</div>
<div class="form-group">
<label>Article Text</label>
<textarea class="form-control" name="body" rows="10" value="<?php echo set_value('body'); ?>" > </textarea>
</div>
<div class="form-group">
<label>Access</label>
<select name="access" class="form-control ">
<option selected>Select Group</option>
<option value="0">Everyone</option>
<?php foreach($groups as $group) : ?>
<option value="<?php echo $group->id; ?>"><?php echo $group->name; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label>Author</label>
<select name="user_id" class="form-control ">
<option selected>Select Author</option>
<?php foreach($users as $user): ?>
<option value="<?php echo $user->id; ?>"><?php echo $user->username; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label>Published</label>
<br />
<label class="radio-inline">
<input type="radio" name="is_published" value="1"> Yes
</label>
<label class="radio-inline">
<input type="radio" name="is_published" value="0"> No
</label>
</div>
<div class="form-group">
<label>Add to Navbar</label>
<br />
<label class="radio-inline">
<input type="radio" name="in_navbar" value="1"> Yes
</label>
<label class="radio-inline">
<input type="radio" name="in_navbar" value="0"> No
</label>
</div>
<div class="form-group">
<label>Order</label>
<input class="form-control" style="width:60px;" type="number" name="order" value="0" min="0" />
</div>
<div class="btn-group pull-left">
<input type="submit" name="submit" id="page_submit" class="btn btn-primary" value="Add" />
<a href="<?php echo base_url()?>admin/dashboard" class="btn btn-default">Back</a>
</div>
</form>
完全控制器方法:
public function add(){
//Validation Rules
$this->form_validation->set_rules('title','Title','trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('category_id','Category','required');
$this->form_validation->set_rules('body','Body','trim|required|xss_clean');
$this->form_validation->set_rules('is_published','Publish','required');
$data['categories'] = $this->Categories_model->get_categories();
$data['users'] = $this->User_model->get_users();
$data['groups'] = $this->Groups_model->get_groups();
//Checking if form was validated
if($this->form_validation->run() == FALSE){
//Views
$data['main_content'] = 'admin/articles/add';
$this->load->view('admin/layouts/main',$data);
} else {
//Create Articles Data Array
$data = array(
'title' => $this->input->post('title'),
'body' => $this->input->post('body'),
'category_id' => $this->input->post('category_id'),
'user_id' => $this->input->post('user_id'),
'access' => $this->input->post('access'),
'is_published' => $this->input->post('is_published'),
'in_navbar' => $this->input->post('in_navbar'),
'order' => $this->input->post('order'),
);
//Insert into Articles Table
$this->Article_model->insert($data);
//Create Notification
$this->session->set_flashdata('article_saved','Your article was saved!');
//Redirect
redirect('admin/articles');
}
}