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

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路