donglin7979 2015-09-17 16:25
浏览 36
已采纳

Codeigniter form_validation始终返回false

I am trying to add a record to my database and validate my form. However my form_validation always returns false.

This is my view. I am using a modal to hold this form.

<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>

This is my controller

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);
    }

}

And this is my model. I have two functions, one if you did not input a picture and one if u didn't.

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);
}

Thank you for the help

PS: I even tried setting all my rules to just 'required' still my form_validation always returns false no matter what i input.

I also checked my $this->input->get values and they are just fine, they get the values that i inputted.

  • 写回答

2条回答 默认 最新

  • dsozqcx9668 2015-09-17 19:18
    关注

    First parameter in set_rules method of form_validation class should be name of field, but you are passing dynamically created values instead. Your rules should be like:

    $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');
    

    Also, your form tag has no enctype="multipart/form-data" which is needed for forms that uploading data.

    And third but most important, when using upload form you would like to use POST method. Docs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站