Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The CodeIgniter set_value() not working?

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);
like image 417
Hrishav Ojha Avatar asked May 09 '14 06:05

Hrishav Ojha


People also ask

What is Set_value in codeigniter?

The set_value function just sets the value, the only benefit to using it is it simplifies setting the value to an already submitted value when redisplaying the form or showing a default value when the form has yet to be submitted.

How will you call a constructor in codeigniter?

Class Constructors If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it: parent::__construct(); The reason this line is necessary is because your local constructor will be overriding the one in the parent controller class so we need to manually call it.


1 Answers

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

like image 66
samjhana joshi Avatar answered Sep 30 '22 11:09

samjhana joshi