⚠ 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

Cstome Error Message On set_rule callback does NOT appear



fede72bari
  • profile picture
  • Member

Posted 06 March 2021 - 11:46 AM

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

 

 

 


fede72bari
  • profile picture
  • Member

Posted 09 March 2021 - 08:38 AM

Nobody has an idea for fixing this issue? ^_^


fede72bari
  • profile picture
  • Member

Posted 15 March 2021 - 21:43 PM

still fighting with this trouble, anybody has any idea? thanks.


web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 16 March 2021 - 06:38 AM

still fighting with this trouble, anybody has any idea? thanks.

 

Hello @fede72bari,

 

From your question it seems that you are referring to Codeigniter version 3 and Grocery CRUD version 1. Now from the code that you've sent I am seeing some mistakes:

1. Instead of using: $this->grocery_crud->getState() == 'insert' use the object of your CRUD, for example: $crud->getState() === 'insert'
2. The error Uncaught TypeError: $(...).tooltip is not a function is not an issue with Grocery CRUD it self. It is mainly because there are conflicting versions of jQuery. Use the unset_jquery() function $crud->unset_jquery(); and also checkout the video for common grocery CRUD mistakes:  https://youtu.be/X0gnDD0qTS8
3. For your initial issue, although I am not sure if this will work, have you tried using the "is_unique" set rule? For example in your case something like this: 

$crud->set_rules('vendors_id','Vendors ID','is_unique[vendors_offers.vendors_id]'); 
if this still doesn't work and you would like to use the callback then I can't find any obvious mistake on your code, if I had to blindly guess it is probably because of the JavaScript error. In order to debug it more, checkout the video tutorial at the second answer.

Regards

Johnny


fede72bari
  • profile picture
  • Member

Posted 16 March 2021 - 09:35 AM

Dear Johnny,

 

you are right Codeigniter 3 and Grocery Crud 1. I have to say this

 

- I have not bugs or mistakes, the code run correctly. The "$this->grocery_crud->getState() == 'insert'" works perfectly. "$crud->" never worked in my environment, just "$this->grocery_crud->" for every GC function. One year ago I found that many others had the same trouble, no explanations found, just one post of a person who tried in this way and it worked. So since all development in my environment worked in this way I assume that the problem is not that one. "$this->grocery_crud->getState()" returns the proper state with no errors.

 

- the control must be custom since I have to check that 2 fields in a linking (n-n relation) table are not present and no one of these fieds are the primary key of the linking table.

 

- the problem is just the message in the alert box. In case the callback gave false result I desire that the alert box message is explacative of the failure reason: "This product has been already linked to the selected vendor with an existing vendor offer." I tried to follow examples and some posts suggestion, and so

 

- defined the callback "$this->grocery_crud->set_rules('vendors_id','Vendors ID','callback__check_not_existing'); " that is working

 

- defined the function _check_not_existing, that is working

 

- defined the custom message "$this->form_validation->set_message('_check_not_existing', 'This product has been already linked to the selected vendor with an existing vendor offer.');" that actually is not working as desired, but still no error or bug appears, since a standard and generic message is appearing.

 

According to your experience is there something i mixed up or forgotten or it is a not detected conflict of GC with CI? Thank you.

 

f

 

 

 

 

 

 

 

 


web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 16 March 2021 - 14:48 PM

Dear Johnny,

 

you are right Codeigniter 3 and Grocery Crud 1. I have to say this

 

- I have not bugs or mistakes, the code run correctly. The "$this->grocery_crud->getState() == 'insert'" works perfectly. "$crud->" never worked in my environment, just "$this->grocery_crud->" for every GC function. One year ago I found that many others had the same trouble, no explanations found, just one post of a person who tried in this way and it worked. So since all development in my environment worked in this way I assume that the problem is not that one. "$this->grocery_crud->getState()" returns the proper state with no errors.

 

- the control must be custom since I have to check that 2 fields in a linking (n-n relation) table are not present and no one of these fieds are the primary key of the linking table.

 

- the problem is just the message in the alert box. In case the callback gave false result I desire that the alert box message is explacative of the failure reason: "This product has been already linked to the selected vendor with an existing vendor offer." I tried to follow examples and some posts suggestion, and so

 

- defined the callback "$this->grocery_crud->set_rules('vendors_id','Vendors ID','callback__check_not_existing'); " that is working

 

- defined the function _check_not_existing, that is working

 

- defined the custom message "$this->form_validation->set_message('_check_not_existing', 'This product has been already linked to the selected vendor with an existing vendor offer.');" that actually is not working as desired, but still no error or bug appears, since a standard and generic message is appearing.

 

According to your experience is there something i mixed up or forgotten or it is a not detected conflict of GC with CI? Thank you.

 

f

 

 

 

 

 

 

 

 

Hello @fede72bari,

 

1. If $this->grocery_crud-> is the way that it works for you then that's fine keep it  :) 
2. For the issue that you have please have in mind that a custom error message was introduced on Grocery CRUD Enterprise, so if you would like to have a custom error message this is only available for Grocery CRUD Enterprise. For example see the example here: https://new.grocerycrud.com/docs/callback-before-insert . However if you are looking for a work-around go to step 3  :) 
3. If I had to blindly guess then I think that the issue is that you are having the line:

$this->form_validation->set_message('_check_not_existing', 'This product has been already linked to the selected vendor with an existing vendor offer.');

 

inside of the callback. Try to add that at the very beginning (even before grocery_crud initial load). If this still will not work, then I have a hacky way for you, try to add your custom message here:

https://github.com/scoumbourdis/grocery-crud/blob/master/application/libraries/Grocery_CRUD.php#L5474

 

for example:
 

$ci = &get_instance();
$ci->load->library('Form_validation');
$ci->form_validation->set_message('_check_not_existing', 'This product has been already linked to the selected vendor with an existing vendor offer.');

....

Let me know if any of the above worked.

 

 


fede72bari
  • profile picture
  • Member

Posted 16 March 2021 - 16:33 PM

Dear Johnny,

 

I tried to move it in several positions even before

 

        $crud = new grocery_CRUD();
        $this->grocery_crud->set_theme('bootstrap-v4');  //flexigrid     
        
        $this->grocery_crud->set_table('vendors_offers');

 

and others before the final rendering function, but no-way it seems that GC overwrite that linking of the error message with the relative callback failure.

 

Was your other workaround acting on the source code of GC library? I had already fixed some bugs and reported on github; bugs that i don't know if they have been accepted. The more I touch the worse will be the mess in case of updating need. No other ways?

 

Thanks.

 

f


fede72bari
  • profile picture
  • Member

Posted 17 March 2021 - 16:31 PM

Dear Johnny,

 

could be possible as an alternative that avoid to modify the source code of GC library to use the existing DIV "id=report-error" sending as response before die the desired message? It is the field for erro message just above the "save an go back to list" button in the insert form page. If this is possible, how is it codify that response? Any Json format?

 

thanks