All was working fine until upgraded to CI3.
I have a form where users have the option to choose their category from the dropdown. For ex:
If they select Cars, a second dropdown populated from database showing input text : Fuel, mileage, year.
If they choose Home, Surface, rooms,...
Before all that, they have to provide their name, address, zipcode ....
So, when the form is submited, it throws an error:
Column 'fuel' cannot be null, even Cars wasn't selected.
PHP Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
Class Biz extends CI_controller
{
function __construct()
{
parent::__construct();
}
public function add()
{
$this->load->helper('form');
$biz=$this->get_form_data();
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('','');
$this->form_validation->set_rules('city_id', 'City', 'trim|required|intval|max_length[20]|callback_city_check');
$this->form_validation->set_rules('district_id', 'District', 'trim|intval|max_length[20]|callback_city_check');
$this->form_validation->set_rules('name', 'Name', 'trim|required|max_length[200]');
$this->form_validation->set_rules('addrs', 'Address', 'trim|required|max_length[200]');
$this->form_validation->set_rules('fuel', 'Fuel', 'trim|required|max_length[200]');
// And so on
if($this->form_validation->run()) {
$biz=$this->get_form_data();
$this->load->model('Bizs');
$bizid = $this->Bizs->add($biz);
redirect('/biz/'.$bizid);
// Funtion get_form_data
private function get_form_data()
{
$biz=array(
'city_id'=>$this->input->post("city_id"),
'district_id'=>$this->input->post("district_id"),
'name'=>$this->input->post("name"),
'addrs'=>$this->input->post("addrs1"),
'catid'=>$this->input->post("catid_1"),
'catid_2'=>$this->input->post("catid_2"),
'phone'=>$this->input->post("tel"),
'about'=>$this->input->post("about"),
'fuel'=>$this->input->post("fuel"),
'mileage'=>$this->input->post("mileage"),
'year'=>$this->input->post("year"),
// And so on
);
return $biz;
}
Model
PHP Code:
public function add($data,$returnid = true)
{
if($this->db->insert($this->table_name,$data))
{
return $returnid ? $this->db->insert_id(): true;
}
else
return false;
}
Table structure:
PHP Code:
CREATE TABLE `biz` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL DEFAULT '',
`name` varchar(50) NOT NULL DEFAULT '',
`fuel` varchar(50) NOT NULL DEFAULT '',
// So on
Thanks