Hi all,
setting a rule for checking that a record is not already present in the DB and preventing double insertion
if( $this->grocery_crud->getState() == 'insert'){ log_message('error', "INSERT STATE FOR NEW VENDOR OFFER"); // check that the offer is not existing, but just when trying to insert a new offer // obviusly when updateing the offer that offeris already existing and the following control // shall be avoided $this->grocery_crud->set_rules('vendors_id','Vendors ID','callback__check_not_existing'); } else{ log_message('error', "NOT INSERT STATE: ".$this->grocery_crud->getState()); }
and the callback
function _check_not_existing(){ $form_variables = $this->input->post(); $query = "SELECT id FROM vendors_offers WHERE vendors_id = ". $form_variables['vendors_id'] ." AND products_id = ". $form_variables['products_id']; $query_res = $this->db->query($query); $query_rows = $query_res->result_array(); log_message('error', "vendors_id: ".$form_variables['vendors_id']." - products_is: ".$form_variables['products_id']); if(count($query_rows) > 0){ log_message('error', "offers already inserted"); //print_r(json_encode(['success'=>false, 'error_message'=>'This product has been already linked to the selected vendor with an existing vendor offer.'])); $this->form_validation->set_message('_check_not_existing', 'This product has been already linked to the selected vendor with an existing vendor offer.'); return false; } else{ return true; } }
everything work properly, but the error message. I get an alert box with this text "An error has occurred on insert." insted the desired "This product has been already linked to the selected vendor with an existing vendor offer.". What have I misunderstood? Thank you all.
PS: in the browser consol I have these warnings, but maybe they are not linked to the problem:
Uncaught TypeError: $(...).tooltip is not a function at pageSetUp (app.min.js:1) at HTMLDocument.<anonymous> (form_validate.js:3) at j (jquery-1.11.1.min.js:2) at Object.fireWith [as resolveWith] (jquery-1.11.1.min.js:2) at Function.ready (jquery-1.11.1.min.js:2) at HTMLDocument.J (jquery-1.11.1.min.js:2) Grammarly-check.js:2 Uncaught TypeError: Cannot read property 'dataset' of null at Object.t.init (Grammarly-check.js:2) at Function.t.start (Grammarly-check.js:2) at Grammarly-check.js:2 at Grammarly-check.js:2 at Grammarly-check.js:2