⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Generate eroor in callback_before_insert and show error message on add page



Ricky
  • profile picture
  • Member

Posted 13 June 2014 - 05:40 AM

Hello Friends , I am new in Grocery CRud and Codeignitor,

 

I want to Generate Error in callback_before_insert and show on add page,

 

in sale_item page i am asking for sale_item.quantity and that quantity i am comparing with item_stock.quantity if sale_item.quantity is greater than item_stock.quantity then i want to generate error in call_back_before_insert and show error to the user..

 

public function m_item_sale()
    {
        
            $this->checksession();
        
            try{
                $crud = new Extension_grocery_CRUD();
                $crud->set_theme('datatables');
                $crud->set_table('m_item_sale');                
                $data['title']="Sale";
                $crud->set_relation('item_id','m_item','name');
                $crud->display_as('item_id','Item');
                $crud->set_subject('Item Sale');
                
                $crud->callback_before_insert(array($this, '_callback_before_insert'));
                if($data['message_insert_error']=="yes")
                {
                    $crud->set_lang_string('error_message','Cannot add the record');
                }
            
        

            $output = $crud->render();
            $this->_example_output($output);
            }
            catch(Exception $e)
            {
                show_error($e->getMessage().' --- '.$e->getTraceAsString());
            }
            
    }
    
     function _callback_before_insert($post_array)
    {
     
            try{
            $post_array['date']= date('Y-m-d');
            $this->db->select('qty');
            $query = $this->db->get_where('m_item', array('id' => $post_array['item_id']));
            $acct = $query->row_array();
          
            if($acct['qty']>=$post_array['qty']){
                $data = array(
                        'qty' => $acct['qty']-$post_array['qty'],
                    );
                    
                $where = array(
                        'id' => $post_array['item_id']
                        );
                        
                $this->db->where($where);
                $this->db->update('m_item',$data);
          
                return $post_array ;
                }
                else
                {
            
                    return false ;
                }
            }
            catch(Exception $e)
            {
                
            }   
    }

 


Ricky
  • profile picture
  • Member

Posted 14 June 2014 - 04:51 AM

Currently There Is Generating an Error When Condition not satisfy.. but is show default message I want to show custom error message.

 

 

Is There any other way to do the same thing ??

please suggest me


Amit Shah
  • profile picture
  • Member

Posted 14 June 2014 - 09:22 AM

well.. my friend,

 

i will recommend you do a validation check and not a pre-insert callback.. and the validation check can be a custom one and the custom validation can have custom error message too..

http://localhost/CodeIgniter_2.1.3/user_guide/libraries/form_validation.html#callbacks

this is an example that should help u !!

 

Happy GCing :)


Ricky
  • profile picture
  • Member

Posted 14 June 2014 - 18:13 PM

Thank you Amit,

For your precisous Suggestion I have Applied it. Here Is my Code, If any one have same requirement like me..

 public function m_item_sale()
    {
        
            $this->checksession();
        
            try{
                $this->load->helper(array('form', 'url'));

                $this->load->library('form_validation');
                
                $crud = new Extension_grocery_CRUD();
                $crud->set_theme('datatables');
                $crud->set_table('m_item_sale');                
                $data['title']="Sale";
                $crud->set_relation('item_id','m_item','name');
                $crud->display_as('item_id','Item');
                $crud->set_subject('Item Sale');
                $crud->set_rules('item_id', 'Item Id', 'callback_assign_itemid');
                $crud->set_rules('qty', 'Quantity', 'callback_quantity_check');
           
                $crud->callback_before_insert(array($this, '_callback_before_insert'));
             
                $output = $crud->render();
                $this->_example_output($output,$data);
            }
            catch(Exception $e)
            {
                show_error($e->getMessage().' --- '.$e->getTraceAsString());
            }
            
    }
    public function assign_itemid($str)
    {
      
        $this->session->set_userdata('itemid', $str);
        return TRUE;
    }
    public function quantity_check($str,$item_id)
    {
            $id = $this->session->userdata('itemid');
        
            $this->db->select('qty');
            $query = $this->db->get_where('m_item', array('id' => $id));
            $acct = $query->row_array();
        
        if($acct['qty']<=$str){
            $this->form_validation->set_message('quantity_check', 'Quantity must be less than or equal to '.$acct['qty'] );
            return FALSE;
        }
        else
        {
            return TRUE;
        }
        
        
    }
     function _callback_before_insert($post_array,$data)
    {
            
            try{
            $post_array['date']= date('Y-m-d');
            $this->db->select('qty');
            $query = $this->db->get_where('m_item', array('id' => $post_array['item_id']));
            $acct = $query->row_array();
            
            if($acct['qty']>=$post_array['qty']){
                $data = array(
                        'qty' => $acct['qty']-$post_array['qty'],
                    );
                    
                $where = array(
                        'id' => $post_array['item_id']
                        );
                        
                $this->db->where($where);
                $this->db->update('m_item',$data);
            
                return $post_array ;
                }
                else
                {         
                    return false ;
                }
            }
            catch(Exception $e)
            {
                
            }
           
    }

Amit Shah
  • profile picture
  • Member

Posted 16 June 2014 - 05:23 AM

Happy GCing :)


jospalgal
  • profile picture
  • Member

Posted 25 May 2017 - 18:00 PM

Hi! I have silimar problem with this but i have a query that limits number of rows and i don't know how to make this work.

 

This is my model function:

 public function get_duplicaterow() {
              
        $query = $this->db->query('SELECT * FROM intervaloshorarios INNER JOIN citas '
                . 'ON intervaloshorarios.idIntervaloHorario = citas.idIntervaloHorario '
                . 'GROUP BY citas.cita, intervaloshorarios.idIntervaloHorario '
                . 'HAVING COUNT(*) >= 2');


        return $query->result();
    }

In the main controller function:

 $crud->set_rules('intervaloHorario', 'Franja Horaria', '_callback_doublerow_check');

In the controller:

  public function doublerow_check() {
        
        $this->Fechacita_Model->get_duplicaterow();
    }
I got this work whith duplicate rows and it don't let me add (fine at this point), but when i add non-existent row, it's not letting me add. What should i do? please help.