简体   繁体   中英

Something wrong with Codeigniter Callback validation

So I am creating a web app, it has this upload form where you can upload android APKs, this part of upload form is specifically for uploading a revised version of the old APK(In others words new version of the application).

So I am doing all this in PHP, using CodeIgniter 2.x Since user is uploading a new version of old Application, I want the website to make sure the application being uploaded for this old application by user has same package name (Android Package file name) eg com.example.something

When the file upload is initiated, the file is parsed and the package name is put into the database, and a random generated hash of this file is stored in the session data as 'new_app_hash', the random hash of old file is in hidden input field called 'app_id'.

Now coming to the code: My controller has this in place to do the callback thing for app_id, and compare its information with 'new_app_hash' which is in the session. So, this callback is mainly for getting row information where the hash is 'app_id' and 'new_app_hash' in my database table, then compare the package names.

    $app_id = $this->input->post('app_id',TRUE);

    if($app_id!='0'){ //only if app_id is not 0, else I dont want this to happen
      $this->form_validation->set_rules('app_id','New Package','required|callback_packagerevname');
    }

Model function used in callback:

public function check_checkpackagerevname($app_id,$sessionhash){
    if($app_id!='0'){
      $old_package = $this->getPackageInfo($app_id);
      $new_package = $this->getPackageInfo($sessionhash);
      if($old_package['package_name']==$new_package['package_name']){
      ///ok fine they are same, return true.
        return TRUE;
      }else{
        return FALSE;
      } 
    }else{
      return TRUE;
    }
  }

GetPackageInfo():

public function getPackageInfo($hash){

    $this->db->select('*');
    $this->db->from('appstore_packages');
    $this->db->where('hash',$hash);
    $query = $this->db->get();
    return $query->row_array();
}

My callback is as follows:

  function callback_packagerevname($app_id){
    $sessionAppHash  = $this->session->userdata('new_hash_app');
    $checking = $this->storeM->check_checkpackagerevname($app_id,$sessionAppHash);

    if($checking==TRUE){
        return TRUE;    
    }else{
      $this->form_validation->set_message('packagerevname','The uploaded APK must have same package name as the parent app that you are trying to upload.');
      return FALSE; ////// new apk doesnt have same package name.
    }
  }

I have tried checking if the form_validation rule is working, when i add some other sort of rule like greater_than[0] it does show me errors, but the callback doesn't work when form is submitted, though the callback function seems to work fine, when i visit it directly from the URL, with the 'new_app_hash' set in the session and putting app_id in URL, it does return true or false accordingly.

What could be wrong?

PS If the question is a little confusing please comment, I am working on the project for like past 12 hours continuously, so I am a little you know. :P

Edit: Ok it seems 12 hours really had it on me. Thanks to praveen, I had named the callback method wrongly, instead of :

  function callback_packagerevname($app_id){
}

It obviously should have been:

function packagerevname($app_id){
}

You dont need callback_functionname as callback event just remove callback_ and will work fine...

function packagerevname()
{
$app_id = $this->input->post('app_id');
//so on....
}

you can use set flash data

    /* $this->form_validation->set_message('checkpackagerevname',
'The uploaded APK must have same package name as the parent app that you are trying to upload.'); */

    $this->session->set_flashdata('checkpackagerevname', 
'The uploaded APK must have same package name as the parent app that you are trying to upload.');

and can print

echo $this->session->flashdata('checkpackagerevname');

for more visit https://ellislab.com/codeigniter/user-guide/libraries/sessions.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM