Question

I am developing a category module where I am using the CI validation rules. When the validation rule fails I want the form to have the same value that the user had entered. I have used set_value function to load the value in the form after the validation rules fails. But every time the validation fails, the value is set to blank.

Here goes my code. I have set the validation rules in the constructor.

$this->form_validation->set_rules($this->validation_rules);
if ($this->input->post()) {
$db_array=array(
    'CategoryName' => $this->input->post('CategoryName'),
    'ParentID' => $this->input->post('ParentID'),
    'Status' => $this->input->post('Status')
    );
if($this->form_validation->run() != false){

    if($id)
    {
        $result = $this->category_m->update($db_array,$id);
        if(isset($result['error'])&& count($result['error']>0))
        {
            foreach($result['error'] as $error)
            {   
                $this->session->set_flashdata('error',$error);
            }
        }
        if($result['success'])
        {
            $action['class'] = 'success';
            $action['msg'] = 'Data added successfully.';
            $this->session->set_flashdata('success',$action);
        }
        else
        {
            $action['class'] = 'error';
            $action['msg'] = 'Invalid values to the form!';
            $this->session->set_flashdata('error',$action);
        }
    }
    else
    {
        $result = $this->category_m->insert($db_array);
        if(isset($result['error']) && count($result['error']>0))
        {
            foreach($result['error'] as $error)
            {
                $this->session->set_flashdata('error',$error);
            }
        }
        if($result['success'])
        {
            $action['class'] = 'success';
            $action['msg'] = 'Data added successfully.';
            $this->session->set_flashdata('sucess',$action);
        }
        else
        {
            $action['class'] = 'error';
            $action['msg'] = 'Invalid values to the form!';
            $this->session->set_flashdata('error',$action);
        }
    }
    if(isset($this->input->post['frmSubmit']))
    {
        redirect('category/form/'.$result['CategoryID']);
    }
    else
    {
        redirect('category/index');
    }

} 
else 
{
    $categories = $this->category_m->getCategoryName(true);
    $data['categories'] = convert_to_dropdown($categories,'CategoryName','CategoryID','Select Parent                  Category'); 
    $data['CategoryName'] = set_value('CategoryName');
    $data['ParentID'] = set_value('ParentID');
    $data['Status'] = set_value('Status');
}
}

if($id)
{
$category = $this->category_m->getCategory($id);
$data['CategoryName'] = $category->CategoryName;
$data['ParentID'] = $category->ParentID;
$data['Status'] = $category->Status;
}
else
{ 
$data['CategoryName'] = '';
$data['ParentID'] = '';
/*foreach($this->validation_rules as $rule){
    //$data['category']->{$rule['field']} = set_value($rule['field']);
}*/
}
$categories = $this->category_m->getCategoryName(true, $id);
$data['categories'] =                                                convert_to_dropdown($categories,'CategoryName','CategoryID','Select Parent Category'); 
$this->load->view('form',$data);
Was it helpful?

Solution

change the position of this code.

else 
{
    $categories = $this->category_m->getCategoryName(true);
    $data['categories'] = convert_to_dropdown($categories,'CategoryName','CategoryID','Select Parent                  Category'); 
    $data['CategoryName'] = set_value('CategoryName');
    $data['ParentID'] = set_value('ParentID');
    $data['Status'] = set_value('Status');
}

as

if($this->input->post()) 
  {

   $categories = $this->category_m->getCategoryName(true);
   $data['categories'] = convert_to_dropdown($categories,'CategoryName','CategoryID','Select Parent Category'); 
   $data['CategoryName'] = set_value('CategoryName');
   $data['ParentID'] = set_value('ParentID');
   $data['Status'] = set_value('Status');
  }
  else if($id)
  {
   $category = $this->category_m->getCategory($id);
   $data['CategoryName'] = $category->CategoryName;
   $data['ParentID'] = $category->ParentID;
   $data['Status'] = $category->Status;
  }
  else
  { 
   $data['CategoryName'] = '';
   $data['ParentID'] = '';
  }

it will surely work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top