我正在尝试将记录添加到我的数据库并验证我的表单。但是我的form_validation总是返回false。
这是我的看法。我正在使用模态来保存此表单。
<form class="form-horizontal" role="form" method="GET" action="<?php echo base_url('Contacts_/addcontact'); ?>">
<div class="form-group">
<label for="usr">First Name</label>
<input type="text" class="form-control" name="inputFirstName" placeholder="First Name">
</div>
<div class="form-group">
<label for="usr">Last Name</label>
<input type="text" class="form-control" name="inputLastName" placeholder="Last Name">
</div>
<div class="form-group">
<label for="usr">Contact Number</label>
<input type="text" class="form-control" name="inputContactNumber" placeholder="Contact Number">
</div>
<div class="form-group">
<label for="usr">Address:</label>
<input type="text" class="form-control" name="inputAddress" placeholder="Address">
</div>
<div class="form-group">
<label for="usr">Email Address:</label>
<input type="text" class="form-control" name="inputEmailAddress" placeholder="Contact Number">
</div>
<div class="form-group">
<label for="usr">Picture:</label>
<input type="file" class="form-control" name="inputPicture"></br>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="action">Add</button>
</div>
</form>
这是我的控制器
public function addcontact(){
$this->load->helper('url');
$this->load->model('contacts');
$this->load->library('form_validation');
$first_name = $this->input->get('inputFirstName');
$last_name = $this->input->get('inputLastName');
$contact_number = $this->input->get('inputContactNumber');
$address = $this->input->get('inputAddress');
$email_address = $this->input->get('inputEmailAddress');
$this->form_validation->set_rules($first_name, 'First Name', 'required|max_length[35]');
$this->form_validation->set_rules($last_name, 'Last Name', 'required|max_length[35]');
$this->form_validation->set_rules($contact_number, 'Contact Number', 'required|exact_length[11]|numeric');
$this->form_validation->set_rules($address, 'Address', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules($email_address, 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');
if($this->form_validation->run() == TRUE){
if(!isset($_GET['inputPicture'])){
$this->contacts->addContactsNoPic($first_name, $last_name, $contact_number, $address, $email_address);
}
else{
$image = 'assets/images/' . $_GET['inputPicture'];
$this->contacts->addContacts($first_name, $last_name, $contact_number, $address, $email_address, $image);
}
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts->getContacts();
$this->load->view('home', $data);
}
else{
$data['title'] = 'Address Book';
$data['err_add'] = 1;
$data['contacts_info'] = $this->contacts->getContacts();
$this->load->view('home', $data);
}
}
这是我的模特。我有两个功能,一个是你没有输入图片,一个是你没有。
function addContacts($first_name, $last_name, $contact_number, $address, $email_address, $image){
$new_contact_data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'contact_number' => $contact_number,
'address' => $address,
'email_address' => $email_address,
'picture' => $image,
);
$this->db->insert('contacts', $new_contact_data);
}
function addContactsNoPic($first_name, $last_name, $contact_number, $address, $email_address){
$new_contact_data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'contact_number' => $contact_number,
'address' => $address,
'email_address' => $email_address,
);
$this->db->insert('contacts', $new_contact_data);
}
感谢您的帮助
PS:我甚至尝试将我的所有规则设置为'required'仍然我的form_validation总是返回false,无论我输入什么。
我还检查了我的$ this-&gt; input-&gt;获取值并且它们很好,它们得到我输入的值。
答案 0 :(得分:1)
正确的语法是:
$this->form_validation->set_rules('inputFirstName', 'First Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputLastName', 'Last Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputContactNumber', 'Contact Number', 'required|exact_length[11]|numeric');
$this->form_validation->set_rules('inputAddress', 'Address', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('inputEmailAddress', 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');
http://www.codeigniter.com/user_guide/libraries/form_validation.html
答案 1 :(得分:1)
form_validation类的set_rules方法中的第一个参数应该是字段名称,但您要传递动态创建的值。你的规则应该是:
$this->form_validation->set_rules('inputFirstName', 'First Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputLastName', 'Last Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputContactNumber', 'Contact Number', 'required|exact_length[11]|numeric');
$this->form_validation->set_rules('inputAddress', 'Address', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('inputEmailAddress', 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');
此外,您的表单代码没有enctype="multipart/form-data"
,这是上传数据的表单所必需的。
第三个但最重要的是,在使用上传表单时,您希望使用POST方法。 Docs